Completed
Push — master ( 9e3cb6...9c9a79 )
by Ivan
05:34
created

CrawlerSession::saveHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace SP\Spiderling;
4
5
use Psr\Http\Message\UriInterface;
6
use GuzzleHttp\Psr7\Uri;
7
8
/**
9
 * @author    Ivan Kerin <[email protected]>
10
 * @copyright 2015, Clippings Ltd.
11
 * @license   http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class CrawlerSession
14
{
15
    use TraverseTrait;
16
17
    /**
18
     * CrawlerInterface
19
     */
20
    private $crawler;
21
22
    /**
23
     * @param CrawlerInterface $crawler
24
     */
25 1
    public function __construct(CrawlerInterface $crawler)
26
    {
27 1
        $this->crawler = $crawler;
28 1
    }
29
30
    /**
31
     * @param  Query\AbstractQuery $query
32
     * @return array
33
     */
34 1
    public function queryIds(Query\AbstractQuery $query)
35
    {
36 1
        return $this->crawler->queryIds($query);
37
    }
38
39
    /**
40
     * @return CrawlerInterface
41
     */
42 1
    public function getCrawler()
43
    {
44 1
        return $this->crawler;
45
    }
46
47
    /**
48
     * @param  string $uri
49
     * @return self
50
     */
51 1
    public function open($uri)
52
    {
53 1
        $uri = \GuzzleHttp\Psr7\uri_for($uri);
54
55 1
        $this->crawler->open($uri);
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * @return UriInterface
62
     */
63 1
    public function getUri()
64
    {
65 1
        return $this->crawler->getUri();
66
    }
67
68
    /**
69
     * @return string
70
     */
71 1
    public function getHtml()
72
    {
73 1
        return $this->crawler->getFullHtml();
74
    }
75
76
    /**
77
     * Save the HTML of the session into a file
78
     * Optionally resolve all the links with a base uri
79
     *
80
     * @param  string              $filename
81
     * @param  UriInterface|string $base
82
     */
83 1
    public function saveHtml($filename, $base = null)
84
    {
85 1
        $html = new Html($this->getHtml());
86
87 1
        if (null !== $base) {
88 1
            $html->resolveLinks(\GuzzleHttp\Psr7\uri_for($base));
89 1
        }
90
91 1
        file_put_contents($filename, $html->get());
92 1
    }
93
}
94