Cockroach   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 42
c 2
b 0
f 0
dl 0
loc 145
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getLogger() 0 3 1
A deliver() 0 21 1
A deliverIteration() 0 39 2
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\SanityBundle\Vacancy\Indexer;
17
18
use Closure;
19
use Psr\Log\LoggerInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
use Veslo\AnthillBundle\Dto\Vacancy\Collector\AcceptanceDto;
22
use Veslo\AnthillBundle\Entity\Repository\VacancyRepository;
23
use Veslo\AnthillBundle\Entity\Vacancy;
24
use Veslo\AppBundle\Workflow\Vacancy\PitInterface;
25
use Veslo\AppBundle\Workflow\Vacancy\Worker\Iteration;
26
use Veslo\AppBundle\Workflow\Vacancy\WorkerInterface;
27
use Veslo\SanityBundle\Vacancy\AnalyserInterface;
28
use Veslo\SanityBundle\Vacancy\Index\Creator as IndexCreator;
29
30
/**
31
 * Delivers a vacancy to the Ministry of Truth for analysis and sanity index calculation
32
 *
33
 *           .--.       .--.
34
 *       _  `    \     /    `  _
35
 *        `\.===. \.^./ .===./`
36
 *               \/`"`\/
37
 *            ,  |     |  ,
38
 *           / `\|`-.-'|/` \
39
 *          /    |  \  |    \
40
 *       .-' ,-'`|   ; |`'-, '-.
41
 *           |   |    \|   |
42
 *           |   |    ;|   |
43
 *           |   \    //   |
44
 *           |    `._//'   |
45
 *          .'             `.
46
 *       _,'                 `,_
47
 *       `                     `
48
 */
49
class Cockroach implements WorkerInterface
50
{
51
    /**
52
     * Logger
53
     *
54
     * @var LoggerInterface
55
     */
56
    private $logger;
57
58
    /**
59
     * Vacancy repository
60
     *
61
     * @var VacancyRepository
62
     */
63
    private $vacancyRepository;
64
65
    /**
66
     * Performs a contextual analysis of vacancy data
67
     *
68
     * @var AnalyserInterface
69
     */
70
    private $vacancyAnalyser;
71
72
    /**
73
     * Creates sanity index entities in the local storage by dto provided by analysers
74
     *
75
     * @var IndexCreator
76
     */
77
    private $sanityIndexCreator;
78
79
    /**
80
     * Converts a sanity index data to array format for third-party services (e.g. logger)
81
     *
82
     * @var NormalizerInterface
83
     */
84
    private $sanityIndexNormalizer;
85
86
    /**
87
     * Cockroach constructor.
88
     *
89
     * @param LoggerInterface     $logger                Logger
90
     * @param VacancyRepository   $vacancyRepository     Vacancy repository
91
     * @param AnalyserInterface   $vacancyAnalyser       Performs a contextual analysis of vacancy data
92
     * @param IndexCreator        $sanityIndexCreator    Creates sanity index entities in the local storage
93
     * @param NormalizerInterface $sanityIndexNormalizer Converts a sanity index data to array for third-party services
94
     */
95
    public function __construct(
96
        LoggerInterface $logger,
97
        VacancyRepository $vacancyRepository,
98
        AnalyserInterface $vacancyAnalyser,
99
        IndexCreator $sanityIndexCreator,
100
        NormalizerInterface $sanityIndexNormalizer
101
    ) {
102
        $this->logger                = $logger;
103
        $this->vacancyRepository     = $vacancyRepository;
104
        $this->vacancyAnalyser       = $vacancyAnalyser;
105
        $this->sanityIndexCreator    = $sanityIndexCreator;
106
        $this->sanityIndexNormalizer = $sanityIndexNormalizer;
107
    }
108
109
    /**
110
     * Performs vacancy indexation iteration
111
     *
112
     * @param PitInterface $pit        Vacancy indexation queue
113
     * @param int          $iterations Indexing iterations count, at least one expected
114
     *
115
     * @return int
116
     */
117
    public function deliver(PitInterface $pit, int $iterations = 1): int
118
    {
119
        $sourceName = get_class($pit);
120
121
        $this->logger->debug('Indexing started.', ['source' => $sourceName, 'iterations' => $iterations]);
122
123
        $iteration     = Closure::fromCallable([$this, 'deliverIteration']);
124
        $iterationLoop = new Iteration\Loop($this, $iteration, 'An error has been occurred during vacancy indexing.');
125
126
        $successfulIterations = $iterationLoop->execute($pit, $iterations);
127
128
        $this->logger->debug(
129
            'Indexing completed.',
130
            [
131
                'source'     => $sourceName,
132
                'iterations' => $iterations,
133
                'successful' => $successfulIterations,
134
            ]
135
        );
136
137
        return $successfulIterations;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getLogger(): ?LoggerInterface
144
    {
145
        return $this->logger;
146
    }
147
148
    /**
149
     * Returns positive if vacancy is successfully indexed
150
     *
151
     * @param PitInterface $pit Vacancy indexation queue
152
     *
153
     * @return bool Positive, if vacancy data was successfully indexed, negative otherwise
154
     */
155
    private function deliverIteration(PitInterface $pit): bool
156
    {
157
        $sourceName = get_class($pit);
158
159
        /** @var AcceptanceDto $acceptance */
160
        $acceptance = $pit->poll();
161
162
        if (!$acceptance instanceof AcceptanceDto) {
0 ignored issues
show
introduced by
$acceptance is always a sub-type of Veslo\AnthillBundle\Dto\...Collector\AcceptanceDto.
Loading history...
163
            $this->logger->debug(
164
                'No more vacancies to index.',
165
                [
166
                    'source' => $sourceName,
167
                    'input'  => gettype($acceptance),
168
                ]
169
            );
170
171
            return false;
172
        }
173
174
        $vacancyId = $acceptance->getVacancyId();
175
176
        /** @var Vacancy $vacancy */
177
        $vacancy = $this->vacancyRepository->require($vacancyId);
178
179
        $indexDto    = $this->vacancyAnalyser->analyse($vacancy);
180
        $sanityIndex = $this->sanityIndexCreator->createByDto($indexDto);
181
182
        $sanityIndexNormalized = $this->sanityIndexNormalizer->normalize($sanityIndex);
183
184
        $this->logger->info(
185
            'Vacancy indexed.',
186
            [
187
                'source'    => $sourceName,
188
                'vacancyId' => $vacancyId,
189
                'index'     => $sanityIndexNormalized,
190
            ]
191
        );
192
193
        return true;
194
    }
195
}
196