Test Setup Failed
Push — master ( b97929...aafad9 )
by Gabriel
02:07
created

AbstractScraper::getCrawlerUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sportic\Timing\RaceTecClient\Scrapers;
4
5
use ByTIC\GouttePhantomJs\Clients\ClientFactory;
6
use Sportic\Timing\RaceTecClient\Parsers\AbstractParser;
7
use Goutte\Client;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
/**
11
 * Class AbstractScraper
12
 * @package Sportic\Timing\RaceTecClient\Scrapers
13
 */
14
abstract class AbstractScraper
15
{
16
17
    /**
18
     * @var Crawler
19
     */
20
    protected $crawler = null;
21
22
    /**
23
     * @var Client
24
     */
25
    protected $client = null;
26
27
    /**
28
     * @return AbstractParser
29
     */
30
    public function execute()
31
    {
32
        $crawler = $this->getCrawler();
33
        $parser = $this->getNewParser();
34
        $parser->setCrawler($crawler);
35
36
        return $parser;
37
    }
38
39
    /**
40
     * @return Crawler
41
     */
42 4
    public function getCrawler()
43
    {
44 4
        if (!$this->crawler) {
45 4
            $this->initCrawler();
46
        }
47
48 4
        return $this->crawler;
49
    }
50
51 4
    protected function initCrawler()
52
    {
53 4
        $this->crawler = $this->generateCrawler();
54 4
    }
55
56
    /**
57
     * @return Crawler
58
     */
59
    abstract protected function generateCrawler();
60
61
    /**
62
     * @return Client
63
     */
64 4
    public function getClient()
65
    {
66 4
        if ($this->client == null) {
67 4
            $this->initClient();
68
        }
69
70 4
        return $this->client;
71
    }
72
73 4
    protected function initClient()
74
    {
75 4
        $client = $this->generateClient();
76 4
        $this->setClient($client);
77 4
    }
78
79
    /**
80
     * @param Client $client
81
     */
82 4
    public function setClient($client)
83
    {
84 4
        $this->client = $client;
85 4
    }
86
87
    /**
88
     * @return Client
89
     */
90 4
    protected function generateClient()
91
    {
92 4
        return ClientFactory::getPhantomJsClient();
93
    }
94
95
    /**
96
     * @return AbstractParser
97
     */
98
    protected function getNewParser()
99
    {
100
        $class = $this->getParserName();
101
        /** @var AbstractParser $parser */
102
        $parser = new $class();
103
        $parser->setScraper($this);
104
105
        return $parser;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    protected function getParserName()
112
    {
113
        $fullClassName = get_class($this);
114
        $partsClassName = explode('\\', $fullClassName);
115
        $classFirstName = array_pop($partsClassName);
116
        $classNamespacePath = implode('\\', $partsClassName);
117
        $classNamespacePath = str_replace('\Scrapers', '', $classNamespacePath);
118
119
        return $classNamespacePath . '\Parsers\\' . $classFirstName;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getCrawlerUri()
126
    {
127
        return $this->getCrawlerUriHost();
128
    }
129
130
    /**
131
     * @return string
132
     */
133 4
    protected function getCrawlerUriHost()
134
    {
135 4
        return 'http://cronometraj.racetecresults.com';
136
    }
137
}
138