ConveyorAwareRoadmap::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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\Roadmap;
17
18
use Veslo\AnthillBundle\Dto\Vacancy\ConfigurableRoadmapDto;
19
use Veslo\AnthillBundle\Dto\Vacancy\Roadmap\ConfigurationDto;
20
use Veslo\AnthillBundle\Dto\Vacancy\LocationDto;
21
use Veslo\AnthillBundle\Dto\Vacancy\Roadmap\StrategyDto;
22
use Veslo\AnthillBundle\Dto\Vacancy\RoadmapDto;
23
use Veslo\AnthillBundle\Vacancy\ConfigurableRoadmapInterface;
24
use Veslo\AnthillBundle\Vacancy\RoadmapInterface;
25
26
/**
27
 * Wrapper for a roadmap that provides meta information for conveyor process
28
 */
29
class ConveyorAwareRoadmap
30
{
31
    /**
32
     * Actual roadmap instance that holds context and parsing plan for specific site
33
     *
34
     * @var RoadmapInterface
35
     */
36
    private $roadmap;
37
38
    /**
39
     * Wrapped roadmap name
40
     *
41
     * @var string
42
     */
43
    private $name;
44
45
    /**
46
     * ConveyorAwareRoadmap constructor.
47
     *
48
     * @param RoadmapInterface $roadmap Actual roadmap instance that holds context and parsing plan for specific site
49
     * @param string           $name    Wrapped roadmap name
50
     */
51
    public function __construct(RoadmapInterface $roadmap, string $name)
52
    {
53
        $this->roadmap = $roadmap;
54
        $this->name    = $name;
55
    }
56
57
    /**
58
     * Returns name of wrapped roadmap for conveyor workflow
59
     *
60
     * @return string
61
     */
62
    public function getName(): string
63
    {
64
        $name = $this->name;
65
66
        if (!$this->roadmap instanceof ConfigurableRoadmapInterface) {
67
            return $name;
68
        }
69
70
        $configuration    = $this->roadmap->getConfiguration();
71
        $parameters       = $configuration->getParameters();
72
        $configurationKey = $parameters->getConfigurationKey();
73
74
        return "$name.$configurationKey";
75
    }
76
77
    /**
78
     * Returns positive whenever roadmap has available vacancy for parsing
79
     *
80
     * @return bool
81
     *
82
     * @see RoadmapInterface::hasNext()
83
     */
84
    public function hasNext(): bool
85
    {
86
        return $this->roadmap->hasNext();
87
    }
88
89
    /**
90
     * Returns URL that contains vacancy for parsing with meta information about roadmap for conveyor workflow
91
     *
92
     * @return LocationDto|null
93
     *
94
     * @see RoadmapInterface::next()
95
     */
96
    public function next(): ?LocationDto
97
    {
98
        $vacancyUrl = $this->roadmap->next();
99
100
        if (empty($vacancyUrl)) {
101
            return null;
102
        }
103
104
        $roadmapDto = new RoadmapDto();
105
        $roadmapDto->setName($this->name);
106
107
        if ($this->roadmap instanceof ConfigurableRoadmapInterface) {
108
            $roadmapDto = $this->upgradeToConfigurableRoadmapDto($roadmapDto);
109
        }
110
111
        $locationDto = new LocationDto();
112
        $locationDto->setRoadmap($roadmapDto);
113
        $locationDto->setVacancyUrl($vacancyUrl);
114
115
        return $locationDto;
116
    }
117
118
    /**
119
     * Builds and returns configurable roadmap dto by specified base roadmap dto
120
     *
121
     * @param RoadmapDto $roadmapDto Base roadmap dto
122
     *
123
     * @return ConfigurableRoadmapDto
124
     */
125
    private function upgradeToConfigurableRoadmapDto(RoadmapDto $roadmapDto): ConfigurableRoadmapDto
126
    {
127
        $configurableRoadmapDto = new ConfigurableRoadmapDto();
128
        $configurableRoadmapDto->setName($roadmapDto->getName());
129
130
        $strategy     = $this->roadmap->getStrategy();
0 ignored issues
show
Bug introduced by
The method getStrategy() does not exist on Veslo\AnthillBundle\Vacancy\RoadmapInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Veslo\AnthillBundle\Vacancy\RoadmapInterface. ( Ignorable by Annotation )

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

130
        /** @scrutinizer ignore-call */ 
131
        $strategy     = $this->roadmap->getStrategy();
Loading history...
131
        $strategyName = substr(get_class($strategy), stripos(get_class($strategy), 'Strategy\\'));
132
133
        $strategyDto = new StrategyDto();
134
        $strategyDto->setName($strategyName);
135
        $configurableRoadmapDto->setStrategy($strategyDto);
136
137
        /** @var ConfigurationInterface $configuration */
138
        $configuration = $this->roadmap->getConfiguration();
0 ignored issues
show
Bug introduced by
The method getConfiguration() does not exist on Veslo\AnthillBundle\Vacancy\RoadmapInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Veslo\AnthillBundle\Vacancy\RoadmapInterface. ( Ignorable by Annotation )

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

138
        /** @scrutinizer ignore-call */ 
139
        $configuration = $this->roadmap->getConfiguration();
Loading history...
139
140
        $parameters       = $configuration->getParameters();
141
        $configurationKey = $parameters->getConfigurationKey();
142
143
        $configurationDto = new ConfigurationDto();
144
        $configurationDto->setKey($configurationKey);
145
        $configurableRoadmapDto->setConfiguration($configurationDto);
146
147
        return $configurableRoadmapDto;
148
    }
149
}
150