<commit_before><?php

/**
 * This file is part of Transfer.
 *
 * For the full copyright and license information, please view the LICENSE file located
 * in the root directory.
 */
namespace Transfer\EzPlatform\Repository\Manager;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\Location;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Transfer\Data\ObjectInterface;
use Transfer\Data\ValueObject;
use Transfer\EzPlatform\Exception\ObjectNotFoundException;
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException;
use Transfer\EzPlatform\Repository\Values\ContentObject;
use Transfer\EzPlatform\Repository\Values\LocationObject;
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;

/**
 * Location manager.
 *
 * @internal
 */
class LocationManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface
{
    /**
     * @var LoggerInterface Logger
     */
    protected $logger;

    /**
     * @var Repository
     */
    private $repository;

    /**
     * @var LocationService Location service
     */
    private $locationService;

    /**
     * @var ContentService Content service
     */
    private $contentService;

    /**
     * @param Repository $repository
     */
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
        $this->locationService = $repository->getLocationService();
        $this->contentService = $repository->getContentService();
    }

    /**
     * {@inheritdoc}
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function find(ValueObject $object)
    {
        try {
            if (isset($object->data['remote_id'])) {
                $location = $this->locationService->loadLocationByRemoteId($object->data['remote_id']);
            } elseif ($object->getProperty('id')) {
                $location = $this->locationService->loadLocation($object->getProperty('id'));
            } elseif (isset($object->data['content_id'])) {
                $contentInfo = $this->contentService->loadContentInfo($object->data['content_id']);
                $locations = $this->locationService->loadLocations($contentInfo);
                foreach ($locations as $loc) {
                    if ($loc->parentLocationId === $object->data['parent_location_id']) {
                        $location = $loc;
                        break;
                    }
                }
            }
        } catch (NotFoundException $notFoundException) {
            // We'll throw our own exception later instead.
        }

        if (!isset($location)) {
            throw new ObjectNotFoundException(Location::class, array('remote_id', 'id'));
        }

        return $location;
    }

    /**
     * Shortcut to find, mainly for locating parents.
     *
     * @param int $id
     *
     * @return Location
     *
     * @throws ObjectNotFoundException
     */
    public function findById($id)
    {
        return $this->find(new ValueObject([], ['id' => $id]));
    }

    /**
     * {@inheritdoc}
     */
    public function create(ObjectInterface $object)<commit_msg>Reduced conditions in LocationManager::find()<commit_after><?php

/**
 * This file is part of Transfer.
 *
 * For the full copyright and license information, please view the LICENSE file located
 * in the root directory.
 */
namespace Transfer\EzPlatform\Repository\Manager;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\Location;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Transfer\Data\ObjectInterface;
use Transfer\Data\ValueObject;
use Transfer\EzPlatform\Exception\ObjectNotFoundException;
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException;
use Transfer\EzPlatform\Repository\Values\ContentObject;
use Transfer\EzPlatform\Repository\Values\LocationObject;
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;

/**
 * Location manager.
 *
 * @internal
 */
class LocationManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface
{
    /**
     * @var LoggerInterface Logger
     */
    protected $logger;

    /**
     * @var Repository
     */
    private $repository;

    /**
     * @var LocationService Location service
     */
    private $locationService;

    /**
     * @var ContentService Content service
     */
    private $contentService;

    /**
     * @param Repository $repository
     */
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
        $this->locationService = $repository->getLocationService();
        $this->contentService = $repository->getContentService();
    }

    /**
     * {@inheritdoc}
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function find(ValueObject $object)
    {
        try {
            if (isset($object->data['remote_id'])) {
                $location = $this->locationService->loadLocationByRemoteId($object->data['remote_id']);
            } elseif ($object->getProperty('id')) {
                $location = $this->locationService->loadLocation($object->getProperty('id'));
            } elseif (isset($object->data['content_id'])) {
                $location = $this->getLocationByObject($object);
            }
        } catch (NotFoundException $notFoundException) {
            // We'll throw our own exception later instead.
        }

        if (!isset($location)) {
            throw new ObjectNotFoundException(Location::class, array('remote_id', 'id'));
        }

        return $location;
    }

    /**
     * Get location by content_id and parent_location_id
     *
     * @param ValueObject $object
     *
     * @return Location|null
     */
    private function getLocationByObject(ValueObject $object)
    {
        $contentInfo = $this->contentService->loadContentInfo($object->data['content_id']);
        $locations = $this->locationService->loadLocations($contentInfo);
        foreach ($locations as $loc) {
            if ($loc->parentLocationId === $object->data['parent_location_id']) {
                return $loc;
            }
        }
        return null;
    }

    /**
     * Shortcut to find, mainly for locating parents.
     *
     * @param int $id
     *
     * @return Location
     *
     * @throws ObjectNotFoundException
     */
    public function findById($id)
    {
        return $this->find(new ValueObject([], ['id' => $id]));
    }

    /**
     * {@inheritdoc}
     */
    public function create(ObjectInterface $object)
{'fraction_non_alphanumeric': 0.12182878849545516, 'fraction_numerical': 0.0, 'mean_word_length': 2.7516539440203562, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4263164', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1945, 'n_words': 390}