Completed
Push — master ( de9ece...1b5216 )
by Gabriel
05:03
created

AbstractParser::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sportic\Timing\RaceTecClient\Parsers;
4
5
use Sportic\Timing\RaceTecClient\Models\AbstractModel;
6
use Sportic\Timing\RaceTecClient\Scrapers\AbstractScraper;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
/**
10
 * Class AbstractParser
11
 * @package Sportic\Timing\RaceTecClient\Parsers
12
 */
13
abstract class AbstractParser
14
{
15
16
    /**
17
     * @var AbstractScraper
18
     */
19
    protected $scraper;
20
21
    /**
22
     * @var Crawler
23
     */
24
    protected $crawler;
25
26
    /**
27
     * @var null|boolean
28
     */
29
    protected $isValidContent = null;
30
31
    /**
32
     * @var null|array
33
     */
34
    protected $contents = null;
35
36
    /**
37
     * @return mixed
38
     */
39
    public function getContent()
40
    {
41
        if ($this->contents === null) {
42
            if ($this->isValidContent()) {
43
                $this->contents = $this->generateContent();
44
            } else {
45
                $this->contents = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type null|array of property $contents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
            }
47
        }
48
49
        return $this->contents;
50
    }
51
52
    abstract protected function generateContent();
53
54
    /**
55
     * @return bool|null
56
     */
57
    public function isValidContent()
58
    {
59
        if ($this->isValidContent == null) {
60
            $this->doValidation();
61
            $this->isValidContent = true;
62
        }
63
64
        return $this->isValidContent;
65
    }
66
67
    /**
68
     * @return void
69
     */
70
    protected function doValidation()
71
    {
72
    }
73
74
    /**
75
     * @return AbstractScraper
76
     */
77
    public function getScraper()
78
    {
79
        return $this->scraper;
80
    }
81
82
    /**
83
     * @param AbstractScraper $scraper
84
     */
85
    public function setScraper($scraper)
86
    {
87
        $this->scraper = $scraper;
88
    }
89
90
    /**
91
     * @return Crawler
92
     */
93
    public function getCrawler(): Crawler
94
    {
95
        return $this->crawler;
96
    }
97
98
    /**
99
     * @param Crawler $crawler
100
     */
101
    public function setCrawler(Crawler $crawler)
102
    {
103
        $this->crawler = $crawler;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getArray()
110
    {
111
        return $this->getContent();
112
    }
113
114
    /**
115
     * @return AbstractModel
116
     */
117
    public function getModel()
118
    {
119
        $model = $this->getNewModel();
120
        $parameters = $this->getContent();
121
        $model->setParameters($parameters);
122
        return $model;
123
    }
124
125
    /**
126
     * @return AbstractModel
127
     */
128
    public function getNewModel()
129
    {
130
        $className = $this->getModelClassName();
131
        $model = new $className();
132
        return $model;
133
    }
134
135
    abstract public function getModelClassName();
136
}
137