<commit_before><?php
/**
 * @file
 */

namespace CultuurNet\UDB3\UDB2\Place;

use Broadway\Repository\RepositoryInterface;
use CultuurNet\UDB3\Place\Place;
use CultuurNet\UDB3\UDB2\ActorCdbXmlServiceInterface;
use CultuurNet\UDB3\UDB2\EventCdbXmlServiceInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
 * Imports places from UDB2 into UDB3 based on cdbxml.
 */
class PlaceCdbXmlImporter implements PlaceImporterInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @var ActorCdbXmlServiceInterface
     */
    protected $cdbXmlService;

    /**
     * @var EventCdbXmlServiceInterface
     */
    protected $eventCdbXmlService;

    /**
     * @var RepositoryInterface
     */
    protected $repository;

    /**
     * @param ActorCdbXmlServiceInterface $cdbXmlService
     * @param RepositoryInterface         $repository
     * @param EventCdbXmlServiceInterface $eventCdbXmlService
     */
    public function __construct(
        ActorCdbXmlServiceInterface $cdbXmlService,
        RepositoryInterface $repository,
        EventCdbXmlServiceInterface $eventCdbXmlService
    ) {
        $this->cdbXmlService = $cdbXmlService;
        $this->repository = $repository;
        $this->eventCdbXmlService = $eventCdbXmlService;
    }

    /**
     * @param string $placeId
     * @return Place|null
     */
    public function updatePlaceFromUDB2($placeId)
    {

    }

    /**
     * @param string $placeId
     * @return Place|null
     */
    public function createPlaceFromUDB2($placeId)
    {
        $sources = [
            'Actor' => function ($placeId) {
                $placeXml = $this->cdbXmlService->getCdbXmlOfActor($placeId);

                $place = Place::importFromUDB2Actor(
                    $placeId,
                    $placeXml,
                    $this->cdbXmlService->getCdbXmlNamespaceUri()
                );

                return $place;
            },
            'Event' => function ($placeId) {
                $placeXml = $this->eventCdbXmlService->getCdbXmlOfEvent($placeId);

                $place = Place::importFromUDB2Event(
                    $placeId,
                    $placeXml,
                    $this->eventCdbXmlService->getCdbXmlNamespaceUri()
                );

                return $place;
            }
        ];

        $place = null;
        foreach($sources as $type => $source) {
            try {
                $place = $source($placeId);

                if($place) {
                    break;
                }
            } catch (\Exception $e) {
                if ($this->logger) {
                    $this->logger->notice(
                        "Place creation in UDB3 failed with an exception",
                        [
                            'exception' => $e,
                            'placeId' => $placeId
                        ]
                    );
                }
            }
        }

        try {
            if($place) {
                $this->repository->save($place);
            }
        } catch (\Exception $e) {
            if ($this->logger) {
                $this->logger->notice(
                    "Place creation in UDB3 failed with an exception",
                    [
                        'exception' => $e,
                        'placeId' => $placeId
                    ]
                );
            }
        }

        return $place;
    }
}<commit_msg>III-648: coding standards<commit_after><?php
/**
 * @file
 */

namespace CultuurNet\UDB3\UDB2\Place;

use Broadway\Repository\RepositoryInterface;
use CultuurNet\UDB3\Place\Place;
use CultuurNet\UDB3\UDB2\ActorCdbXmlServiceInterface;
use CultuurNet\UDB3\UDB2\EventCdbXmlServiceInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
 * Imports places from UDB2 into UDB3 based on cdbxml.
 */
class PlaceCdbXmlImporter implements PlaceImporterInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @var ActorCdbXmlServiceInterface
     */
    protected $cdbXmlService;

    /**
     * @var EventCdbXmlServiceInterface
     */
    protected $eventCdbXmlService;

    /**
     * @var RepositoryInterface
     */
    protected $repository;

    /**
     * @param ActorCdbXmlServiceInterface $cdbXmlService
     * @param RepositoryInterface         $repository
     * @param EventCdbXmlServiceInterface $eventCdbXmlService
     */
    public function __construct(
        ActorCdbXmlServiceInterface $cdbXmlService,
        RepositoryInterface $repository,
        EventCdbXmlServiceInterface $eventCdbXmlService
    ) {
        $this->cdbXmlService = $cdbXmlService;
        $this->repository = $repository;
        $this->eventCdbXmlService = $eventCdbXmlService;
    }

    /**
     * @param string $placeId
     * @return Place|null
     */
    public function updatePlaceFromUDB2($placeId)
    {

    }

    /**
     * @param string $placeId
     * @return Place|null
     */
    public function createPlaceFromUDB2($placeId)
    {
        $sources = [
            'Actor' => function ($placeId) {
                $placeXml = $this->cdbXmlService->getCdbXmlOfActor($placeId);

                $place = Place::importFromUDB2Actor(
                    $placeId,
                    $placeXml,
                    $this->cdbXmlService->getCdbXmlNamespaceUri()
                );

                return $place;
            },
            'Event' => function ($placeId) {
                $placeXml = $this->eventCdbXmlService->getCdbXmlOfEvent($placeId);

                $place = Place::importFromUDB2Event(
                    $placeId,
                    $placeXml,
                    $this->eventCdbXmlService->getCdbXmlNamespaceUri()
                );

                return $place;
            }
        ];

        $place = null;
        foreach ($sources as $type => $source) {
            try {
                $place = $source($placeId);

                if ($place) {
                    break;
                }
            } catch (\Exception $e) {
                if ($this->logger) {
                    $this->logger->notice(
                        "Place creation in UDB3 failed with an exception",
                        [
                            'exception' => $e,
                            'placeId' => $placeId
                        ]
                    );
                }
            }
        }

        try {
            if ($place) {
                $this->repository->save($place);
            }
        } catch (\Exception $e) {
            if ($this->logger) {
                $this->logger->notice(
                    "Place creation in UDB3 failed with an exception",
                    [
                        'exception' => $e,
                        'placeId' => $placeId
                    ]
                );
            }
        }

        return $place;
    }
}
{'fraction_non_alphanumeric': 0.10241573846376392, 'fraction_numerical': 0.004773614928395776, 'mean_word_length': 1.4422465559872837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2211233', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1838, 'n_words': 327}