HeadHunter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 1
b 0
f 0
dl 0
loc 98
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A ensureParameters() 0 4 2
A save() 0 8 1
A getParameters() 0 5 1
A apply() 0 3 1
A setParameters() 0 5 1
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\Roadmap\Configuration;
17
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Veslo\AnthillBundle\Entity\Repository\Vacancy\Roadmap\Configuration\Parameters\HeadHunterRepository as HeadHunterParametersRepository;
21
use Veslo\AnthillBundle\Entity\Vacancy\Roadmap\Configuration\Parameters\HeadHunter as HeadHunterParameters;
22
use Veslo\AnthillBundle\Exception\Vacancy\Roadmap\ConfigurationNotFoundException;
23
use Veslo\AnthillBundle\Vacancy\Roadmap\ConfigurationInterface;
24
25
/**
26
 * Represents configuration of vacancy search algorithms for HeadHunter
27
 */
28
class HeadHunter implements ConfigurationInterface
29
{
30
    /**
31
     * Converts an object into a set of arrays/scalars
32
     *
33
     * @var NormalizerInterface
34
     */
35
    private $normalizer;
36
37
    /**
38
     * Logger as it is
39
     *
40
     * @var LoggerInterface
41
     */
42
    private $logger;
43
44
    /**
45
     * Repository for HeadHunter search parameters
46
     *
47
     * @var HeadHunterParametersRepository
48
     */
49
    private $parametersRepository;
50
51
    /**
52
     * Parameters for vacancy search on HeadHunter website
53
     *
54
     * @var HeadHunterParameters
55
     */
56
    private $_parameters;
57
58
    /**
59
     * HeadHunter constructor.
60
     *
61
     * @param NormalizerInterface            $normalizer           Converts an object into a set of arrays/scalars
62
     * @param LoggerInterface                $logger               Logger as it is
63
     * @param HeadHunterParametersRepository $parametersRepository Repository for HeadHunter search parameters
64
     */
65
    public function __construct(
66
        NormalizerInterface $normalizer,
67
        LoggerInterface $logger,
68
        HeadHunterParametersRepository $parametersRepository
69
    ) {
70
        $this->normalizer           = $normalizer;
71
        $this->logger               = $logger;
72
        $this->parametersRepository = $parametersRepository;
73
        $this->_parameters          = null;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function apply(string $configurationKey): void
80
    {
81
        $this->_parameters = $this->parametersRepository->requireByConfigurationKey($configurationKey);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getParameters(): ParametersInterface
88
    {
89
        $this->ensureParameters();
90
91
        return $this->_parameters;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function setParameters(ParametersInterface $parameters): void
98
    {
99
        $this->_parameters = $parameters;
0 ignored issues
show
Documentation Bug introduced by
$parameters is of type Veslo\AnthillBundle\Vaca...ion\ParametersInterface, but the property $_parameters was declared to be of type Veslo\AnthillBundle\Enti...n\Parameters\HeadHunter. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
100
101
        $this->ensureParameters();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function save(): void
108
    {
109
        $this->ensureParameters();
110
111
        $this->parametersRepository->save($this->_parameters);
112
113
        $normalizedParameters = $this->normalizer->normalize($this->_parameters);
114
        $this->logger->debug('Roadmap configuration changed.', ['parameters' => $normalizedParameters]);
115
    }
116
117
    /**
118
     * Throws exception if configuration parameters have not been applied
119
     *
120
     * @return void
121
     */
122
    private function ensureParameters(): void
123
    {
124
        if (!$this->_parameters instanceof HeadHunterParameters) {
0 ignored issues
show
introduced by
$this->_parameters is always a sub-type of Veslo\AnthillBundle\Enti...n\Parameters\HeadHunter.
Loading history...
125
            throw new ConfigurationNotFoundException();
126
        }
127
    }
128
}
129