<commit_before><?php

namespace AppBundle\Queue\Consumer;

use AppBundle\Model\Document\City\CityModel;
use AppBundle\Model\Document\Note\NoteModel;
use AppBundle\Model\Document\Publish\Record\RecordModel;
use AppBundle\Model\Logic\Publisher\PublisherFactory;
use AppBundle\Queue\Message\CollectMessage;
use AppBundle\Queue\Message\PublishMessage;
use Monolog\Logger;
use Schema\City\City;

class PublishConsumer
{
    private $logger;
    private $model_record;
    private $model_note;
    private $model_city;
    private $publisher_factory;

    /**
     * PublishConsumer constructor.
     * @param PublisherFactory $publisher_factory
     * @param RecordModel      $model_record
     * @param NoteModel        $model_note
     * @param Logger           $logger
     */
    public function __construct(
        PublisherFactory $publisher_factory,
        RecordModel $model_record,
        NoteModel $model_note,
        CityModel $model_city,
        Logger $logger
    )
    {
        $this->model_record      = $model_record;
        $this->model_note        = $model_note;
        $this->model_city        = $model_city;
        $this->logger            = $logger;
        $this->publisher_factory = $publisher_factory;
    }

    /**
     * @param PublishMessage $message
     * @return bool
     */
    public function handle(PublishMessage $message)
    {
        try {

            $this->logger->debug('Handle message', [
                'external_id' => $message->getId(),
                'city'        => $message->getSource()->getCity()
            ]);

            $city = $this->model_city->findOneByShortName($message->getSource()->getCity());

            $note = $message->getNote();

            if (empty($note->getSubways()) && $city->hasSubway()) {
                $this->logger->debug('There are no subways', [
                    'note_id'          => $note->getId(),
                    'note_external_id' => $note->getExternalId(),
                    'city'             => $message->getSource()->getCity()
                ]);

                return false;
            }

            if (empty($note->getPrice())) {
                $this->logger->debug('There is no price', [
                    'note_id'          => $note->getId(),
                    'note_external_id' => $note->getExternalId(),
                    'city'             => $message->getSource()->getCity()
                ]);

                return false;
            }

            if (count($note->getPhotos()) < 3) {

                $this->logger->debug('There are no photos', [
                    'note_id'          => $note->getId(),
                    'note_external_id' => $note->getExternalId(),
                    'city'             => $message->getSource()->getCity()
                ]);

                return false;
            }

            $record = $this->model_record->findOneByCity((new City())->setShortName($message->getSource()->getCity()));

            if (null === $record) {
                $this->logger->error('There is no publish record for city', [
                    'message_id' => $message->getId(),
                    'city'       => $message->getSource()->getCity()
                ]);

                return false;
            }

            $publisher = $this->publisher_factory->init($record);

            $publisher->publish($note);

            $note<commit_msg>publish record active<commit_after><?php

namespace AppBundle\Queue\Consumer;

use AppBundle\Model\Document\City\CityModel;
use AppBundle\Model\Document\Note\NoteModel;
use AppBundle\Model\Document\Publish\Record\RecordModel;
use AppBundle\Model\Logic\Publisher\PublisherFactory;
use AppBundle\Queue\Message\CollectMessage;
use AppBundle\Queue\Message\PublishMessage;
use Monolog\Logger;
use Schema\City\City;

class PublishConsumer
{
    private $logger;
    private $model_record;
    private $model_note;
    private $model_city;
    private $publisher_factory;

    /**
     * PublishConsumer constructor.
     * @param PublisherFactory $publisher_factory
     * @param RecordModel      $model_record
     * @param NoteModel        $model_note
     * @param Logger           $logger
     */
    public function __construct(
        PublisherFactory $publisher_factory,
        RecordModel $model_record,
        NoteModel $model_note,
        CityModel $model_city,
        Logger $logger
    )
    {
        $this->model_record      = $model_record;
        $this->model_note        = $model_note;
        $this->model_city        = $model_city;
        $this->logger            = $logger;
        $this->publisher_factory = $publisher_factory;
    }

    /**
     * @param PublishMessage $message
     * @return bool
     */
    public function handle(PublishMessage $message)
    {
        try {

            $this->logger->debug('Handle message', [
                'external_id' => $message->getId(),
                'city'        => $message->getSource()->getCity()
            ]);

            $city = $this->model_city->findOneByShortName($message->getSource()->getCity());

            $note = $message->getNote();

            if (empty($note->getSubways()) && $city->hasSubway()) {
                $this->logger->debug('There are no subways', [
                    'note_id'          => $note->getId(),
                    'note_external_id' => $note->getExternalId(),
                    'city'             => $message->getSource()->getCity()
                ]);

                return false;
            }

            if (empty($note->getPrice())) {
                $this->logger->debug('There is no price', [
                    'note_id'          => $note->getId(),
                    'note_external_id' => $note->getExternalId(),
                    'city'             => $message->getSource()->getCity()
                ]);

                return false;
            }

            if (count($note->getPhotos()) < 3) {

                $this->logger->debug('There are no photos', [
                    'note_id'          => $note->getId(),
                    'note_external_id' => $note->getExternalId(),
                    'city'             => $message->getSource()->getCity()
                ]);

                return false;
            }

            $record = $this->model_record->findOneByCity((new City())->setShortName($message->getSource()->getCity()));

            if (!$record->isActive()) {
                $this->logger->error('Publish record is not active for city', [
                    'message_id' => $message->getId(),
                    'city'       => $message->getSource()->getCity()
                ]);

                return false;
            }

            if (null === $record) {
                $this->logger->error('There is no publish record for city', [
                    'message_id' => $message->getId(),
                    'city'       => $message->getSource()->getCity()
                ]);

                return false;
            }

            $publisher = $this->publisher_factory->init($record);

            $publisher->publish($note);

            $note
{'fraction_non_alphanumeric': 0.13707644917390086, 'fraction_numerical': 0.0002800336040324839, 'mean_word_length': 1.6613263785394934, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 137, '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': '7052184', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1847, 'n_words': 326}