DungBeetle   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 35
c 1
b 0
f 0
dl 0
loc 114
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dig() 0 18 1
A getLogger() 0 3 1
A digInternal() 0 19 4
A digIteration() 0 18 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\AnthillBundle\Vacancy\Digger;
17
18
use Exception;
19
use Psr\Log\LoggerInterface;
20
use Veslo\AnthillBundle\Vacancy\DiggerInterface;
21
use Veslo\AnthillBundle\Vacancy\Roadmap\ConveyorAwareRoadmap;
22
use Veslo\AppBundle\Workflow\Vacancy\PitInterface;
23
use Veslo\AppBundle\Workflow\Vacancy\WorkerInterface;
24
25
/**
26
 * Digs some dung (vacancies) from internet and sends to conveyor for processing
27
 * It wants some roadmap, so it can know what to dig and where to dig
28
 *
29
 *              _,=(_)=,_
30
 *           ,;`         `;,
31
 *          \\    (\^/)    //
32
 *           \\   <( )>   //
33
 *            <`-'`"""`'-`>
34
 *           _/           \_
35
 *         _(_\           /_)_
36
 *        /|` |`----.----`| `|\
37
 *        |/  |     |     |  \|
38
 *       />   |     |     |   <\
39
 *           _;     |     ;_
40
 *         <`_\     |     /_`>
41
 *         |\  `._  |  _.'  /|
42
 *         \|     `"""`    |/
43
 *          |\            /|
44
 *           \\          //
45
 *           /_>        <_\
46
 *
47
 * Note: if you are a staff member of any job website - don't stop this creature from doing its work,
48
 * angry owner will come and implement a proxy bypass. You've been warned.
49
 *
50
 * @see RoadmapInterface
51
 */
52
class DungBeetle implements WorkerInterface, DiggerInterface
53
{
54
    /**
55
     * Logger as it is
56
     *
57
     * @var LoggerInterface
58
     */
59
    private $logger;
60
61
    /**
62
     * Storage for fresh dung (vacancies)
63
     *
64
     * @var PitInterface
65
     */
66
    private $destination;
67
68
    /**
69
     * DungBeetle constructor.
70
     *
71
     * @param LoggerInterface $logger      Logger as it is
72
     * @param PitInterface    $destination Storage for digged dung (vacancies)
73
     */
74
    public function __construct(LoggerInterface $logger, PitInterface $destination)
75
    {
76
        $this->logger      = $logger;
77
        $this->destination = $destination;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function dig(ConveyorAwareRoadmap $roadmap, int $iterations = 1): int
84
    {
85
        $roadmapName = $roadmap->getName();
86
87
        $this->logger->debug('Digging started.', ['roadmap' => $roadmapName, 'iterations' => $iterations]);
88
89
        $successfulIterations = $this->digInternal($roadmap, $iterations);
90
91
        $this->logger->debug(
92
            'Digging completed.',
93
            [
94
                'roadmap'    => $roadmapName,
95
                'iterations' => $iterations,
96
                'successful' => $successfulIterations,
97
            ]
98
        );
99
100
        return $successfulIterations;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getLogger(): ?LoggerInterface
107
    {
108
        return $this->logger;
109
    }
110
111
    /**
112
     * Performs actual dung (vacancies) digging loop
113
     *
114
     * @param ConveyorAwareRoadmap $roadmap    Provides URL of vacancies
115
     * @param int                  $iterations Digging iterations count
116
     *
117
     * @return int Successful dig attempts count
118
     */
119
    private function digInternal(ConveyorAwareRoadmap $roadmap, int $iterations): int
120
    {
121
        $iterationRemains = max(1, $iterations);
122
        $iterationSuccess = 0;
123
124
        while ($iterationRemains > 0) {
125
            try {
126
                if ($this->digIteration($roadmap)) {
127
                    ++$iterationSuccess;
128
                }
129
            } catch (Exception $e) {
130
                $context = ['roadmap' => $roadmap->getName(), 'message' => $e->getMessage()];
131
                $this->logger->error('An error has been occurred during vacancy digging.', $context);
132
            }
133
134
            --$iterationRemains;
135
        }
136
137
        return $iterationSuccess;
138
    }
139
140
    /**
141
     * Returns positive if vacancy is successfully found by specified roadmap
142
     * Builds and sends a payload to conveyor for further processing according to configured workflow
143
     *
144
     * @param ConveyorAwareRoadmap $roadmap Provides URL of vacancies
145
     *
146
     * @return bool Positive, if new vacancy has been successfully found, negative otherwise
147
     */
148
    private function digIteration(ConveyorAwareRoadmap $roadmap): bool
149
    {
150
        $roadmapName = $roadmap->getName();
151
152
        if (!$roadmap->hasNext()) {
153
            $this->logger->debug('No more vacancies.', ['roadmap' => $roadmapName]);
154
155
            return false;
156
        }
157
158
        $locationDto = $roadmap->next();
159
160
        $vacancyUrl = $locationDto->getVacancyUrl();
161
        $this->logger->info('Vacancy found.', ['roadmap' => $roadmapName, 'vacancyUrl' => $vacancyUrl]);
162
163
        $this->destination->offer($locationDto);
0 ignored issues
show
Bug introduced by
It seems like $locationDto can also be of type null; however, parameter $dto of Veslo\AppBundle\Workflow...y\PitInterface::offer() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
        $this->destination->offer(/** @scrutinizer ignore-type */ $locationDto);
Loading history...
164
165
        return true;
166
    }
167
}
168