MultistrategicScanner   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A scan() 0 15 3
A __construct() 0 4 1
A chooseStrategy() 0 3 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\Scanner;
17
18
use Veslo\AnthillBundle\Dto\Vacancy\RawDto;
19
use Veslo\AnthillBundle\Exception\Vacancy\Scanner\InputDataEmptyException;
20
use Veslo\AnthillBundle\Exception\Vacancy\Scanner\StrategyNotChosenException;
21
use Veslo\AnthillBundle\Vacancy\Scanner\StrategyPool\OneToOneStrategyPool;
22
use Veslo\AnthillBundle\Vacancy\ScannerInterface;
23
24
/**
25
 * Vacancy scanner that supports multiple parsing strategies based on vacancy location
26
 */
27
class MultistrategicScanner implements ScannerInterface
28
{
29
    /**
30
     * Establishes relation between vacancy search strategies and suitable parse strategies
31
     *
32
     * @var OneToOneStrategyPool
33
     */
34
    private $strategyPool;
35
36
    /**
37
     * Parse algorithm chosen for execution
38
     *
39
     * @var StrategyInterface
40
     */
41
    private $_strategy;
42
43
    /**
44
     * MultistrategicScanner constructor.
45
     *
46
     * @param OneToOneStrategyPool $strategyPool Establishes relation between vacancy search and parse strategies
47
     */
48
    public function __construct(OneToOneStrategyPool $strategyPool)
49
    {
50
        $this->strategyPool = $strategyPool;
51
        $this->_strategy    = null;
52
    }
53
54
    /**
55
     * Marks specific parse algorithm for execution based on vacancy search algorithm
56
     *
57
     * @param string $roadmapStrategyName Roadmap search strategy name
58
     *
59
     * @return void
60
     */
61
    public function chooseStrategy(string $roadmapStrategyName): void
62
    {
63
        $this->_strategy = $this->strategyPool->choose($roadmapStrategyName);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @throws StrategyNotChosenException
70
     */
71
    public function scan(string $vacancyUrl): RawDto
72
    {
73
        if (!$this->_strategy instanceof StrategyInterface) {
0 ignored issues
show
introduced by
$this->_strategy is always a sub-type of Veslo\AnthillBundle\Vaca...anner\StrategyInterface.
Loading history...
74
            throw StrategyNotChosenException::withVacancyUrl($vacancyUrl);
75
        }
76
77
        $data = $this->_strategy->fetch($vacancyUrl);
78
79
        if (empty($data)) {
80
            throw InputDataEmptyException::withVacancyUrl($vacancyUrl);
81
        }
82
83
        $vacancyData = $this->_strategy->tokenize($data);
84
85
        return $vacancyData;
86
    }
87
}
88