LicensePlateFactory::getResults()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace PBurggraf\LicensePlate;
4
5
use PBurggraf\LicensePlate\Detector\AbstractDetector;
6
use PBurggraf\LicensePlate\Response\LicensePlateResponse;
7
8
/**
9
 * @author Philip Burggraf <[email protected]>
10
 */
11
class LicensePlateFactory
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $licensePlate;
17
18
    /**
19
     * @var AbstractDetector[]
20
     */
21
    protected $detectorTypes = [];
22
23
    /**
24
     * @param string $licensePlate
25
     */
26
    public function __construct($licensePlate)
27
    {
28
        $this->licensePlate = $licensePlate;
29
    }
30
31
    /**
32
     * @param $detectorType
33
     *
34
     * @throws \RuntimeException
35
     */
36
    public function addDetectorType($detectorType)
37
    {
38
        if (!class_exists($detectorType)) {
39
            throw new \RuntimeException('Detector not found');
40
        }
41
42
        $this->detectorTypes[] = $detectorType;
43
    }
44
45
    /**
46
     * @return LicensePlateResponse[]
47
     */
48
    public function getResults()
49
    {
50
        $results = [];
51
52
        foreach ($this->detectorTypes as $detectorType) {
53
            /** @var AbstractDetector $detector */
54
            $detector = new $detectorType($this->licensePlate);
55
56
            $response = $detector->getResponse();
57
58
            if ($response->isValid()) {
59
                $results[] = $response;
60
            }
61
        }
62
63
        return $results;
64
    }
65
66
    /**
67
     * @param string $licensePlate
68
     * @param array  $detectorTypes
69
     *
70
     * @throws \RuntimeException
71
     *
72
     * @return LicensePlateResponse[]
73
     */
74
    public static function fromString($licensePlate, array $detectorTypes)
75
    {
76
        $results = [];
77
78
        foreach ($detectorTypes as $detectorType) {
79
            if (!class_exists($detectorType)) {
80
                throw new \RuntimeException('Detector not found');
81
            }
82
83
            /** @var AbstractDetector $detector */
84
            $detector = new $detectorType($licensePlate);
85
86
            $response = $detector->getResponse();
87
88
            if ($response->isValid()) {
89
                $results[] = $response;
90
            }
91
        }
92
93
        return $results;
94
    }
95
}
96