Completed
Push — master ( 9938ea...d13685 )
by Felix
07:08
created

Crawler::getUrl()   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 Felix\Scraper;
4
5
class Crawler
6
{
7
    protected $timeout = 60;
8
    protected $url = '';
9
    protected $xpath = '';
10
    protected $html = '';
11
    protected $content = '';
12
13 1
    public function start(string $url, string $xpath)
14
    {
15 1
        $html = $this->getHtml($url);
16
17 1
        $this->setContent($html, $xpath);
18
19 1
        return $this->content;
20
    }
21
22
    /**
23
     * Obtener el contenido HTML de una página.
24
     *
25
     *
26
     * @param string $url Url de la página a scrapear
27
     *
28
     * @return string
29
     */
30 2
    public function getHtml(string $url)
31
    {
32 2
        $this->html = @file_get_contents($url);
33
34 2
        return $this->html;
35
    }
36
37
    /**
38
     * Almacenar el codigo HTML.
39
     *
40
     * @param string $html
41
     * @param string $xpath
42
     */
43 1
    public function setContent($html, $xpath)
44
    {
45 1
        $crawler = new \Symfony\Component\DomCrawler\Crawler($html);
46 1
        $this->content = $crawler->evaluate($xpath);
0 ignored issues
show
Documentation Bug introduced by
It seems like $crawler->evaluate($xpath) of type object<Symfony\Component\DomCrawler\Crawler> or array<integer,?,{"0":"?"}> is incompatible with the declared type string of property $content.

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...
47 1
    }
48
49
    /**
50
     * Obtner el contenido.
51
     */
52
    public function getContent()
53
    {
54
        return $this->content;
55
    }
56
57
    /**
58
     * Obtner la URL.
59
     */
60
    public function getUrl()
61
    {
62
        return $this->url;
63
    }
64
65
    /**
66
     * Guardar el tiempo de espera de una página.
67
     */
68
    public function setTimeout(int $timeout)
69
    {
70
        $this->timeout = $timeout;
71
    }
72
}
73