CrawlerSession::__construct()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace SP\Spiderling;
4
5
use Psr\Http\Message\UriInterface;
6
use GuzzleHttp\Psr7\Uri;
7
use InvalidArgumentException;
8
9
/**
10
 * @author    Ivan Kerin <[email protected]>
11
 * @copyright 2015, Clippings Ltd.
12
 * @license   http://spdx.org/licenses/BSD-3-Clause
13
 */
14
class CrawlerSession
15
{
16
    use TraverseTrait;
17
18
    /**
19
     * CrawlerInterface
20
     */
21
    private $crawler;
22
23
    /**
24
     * @param CrawlerInterface $crawler
25
     */
26
    public function __construct(CrawlerInterface $crawler)
27
    {
28
        $this->crawler = $crawler;
29
    }
30
31
    /**
32
     * @param  Query\AbstractQuery $query
33
     * @return array
34
     */
35 1
    public function queryIds(Query\AbstractQuery $query)
36
    {
37
        return $this->crawler->queryIds($query);
38 1
    }
39
40
    /**
41
     * @return CrawlerInterface
42
     */
43 1
    public function getCrawler()
44
    {
45 1
        return $this->crawler;
46
    }
47
48
    /**
49
     * @param  string $uri
50
     * @return self
51
     */
52
    public function open($uri)
53
    {
54
        $uri = \GuzzleHttp\Psr7\uri_for($uri);
55
56
        $this->crawler->open($uri);
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return UriInterface
63
     */
64
    public function getUri()
65
    {
66
        return $this->crawler->getUri();
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getHtml()
73
    {
74
        return $this->crawler->getFullHtml();
75
    }
76
77
    /**
78
     * Save the HTML of the session into a file
79
     * Optionally resolve all the links with a base uri
80
     *
81
     * @param  string              $filename
82
     * @throws InvalidArgumentException if directory doesnt exist or is not writable
83
     * @param  UriInterface|string $base
84
     */
85 1
    public function saveHtml($filename, $base = null)
86
    {
87
        $this->ensureWritableDirectory(dirname($filename));
88
89
        $html = new Html($this->getHtml());
90
91 1
        if (null !== $base) {
92
            $html->resolveLinks(\GuzzleHttp\Psr7\uri_for($base));
93
        }
94
95
        file_put_contents($filename, $html->get());
96
97 1
        return $this;
98
    }
99
100
    /**
101
     * @param  string  $directory
102
     * @return boolean
103
     */
104
    public function isWritableDirectory($directory)
105
    {
106
        return is_dir($directory) && is_writable($directory);
107
    }
108
109
    /**
110
     * @param  string $directory
111
     * @throws InvalidArgumentException if directory doesnt exist or is not writable
112
     */
113 1
    public function ensureWritableDirectory($directory)
114
    {
115
        if (false === $this->isWritableDirectory($directory)) {
116
            throw new InvalidArgumentException(sprintf(
117 1
                'Make sure directory %s exists and is writable',
118
                $directory
119
            ));
120
        }
121 1
    }
122
}
123