PreviewManager::cache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Jclyons52\PagePreview;
4
5
use Jclyons52\PagePreview\Cache\Cache;
6
use Jclyons52\PHPQuery\Document;
7
use Psr\Cache\CacheItemPoolInterface;
8
9
class PreviewManager
10
{
11
    /**
12
     * @var Crawler
13
     */
14
    private $crawler;
15
16
    /**
17
     * @var Cache
18
     */
19
    private $cache;
20
21
    /**
22
     * Url object
23
     * @var Url
24
     */
25
    private $url;
26
27
    /**
28
     * @var HttpInterface
29
     */
30
    protected $http;
31
32
33 99
    public function __construct(HttpInterface $http, CacheItemPoolInterface $cache = null)
34
    {
35 99
        $this->http = $http;
36 99
        if ($cache !== null) {
37 3
            $this->cache = new Cache($cache);
38 3
        }
39 99
    }
40
41
    /**
42
     * Instantiate class with dependencies
43
     * @param CacheItemPoolInterface $cache
44
     * @return static
45
     */
46 6
    public static function create(CacheItemPoolInterface $cache = null)
47
    {
48 6
        $http = new Http();
49 6
        return new PreviewManager($http, $cache);
50
    }
51
52
    /**
53
     * @param string $url
54
     * @return Preview
55
     * @throws \Exception
56
     */
57 57
    public function fetch($url = null)
58
    {
59 57
        if ($url instanceof Url) {
60 3
            $this->url = $url;
61 3
        }
62 57
        if (is_string($url)) {
63 48
            $this->url = new Url($url);
64 45
        }
65 54
        if ($this->url === null) {
66 3
            throw new \Exception('no url provided');
67
        }
68
69 51
        $body = $this->http->get($this->url->original);
70
71 51
        if ($body === false) {
72 3
            throw new \Exception('failed to load page');
73
        }
74
75 48
        return $this->getPreview($body);
76
    }
77
78 3
    public function findUrl($text)
79
    {
80 3
        $this->url = Url::findFirst($text);
81
82 3
        return $this;
83
    }
84
85 6
    public function findOrFetch($url)
86
    {
87 6
        if ($this->cache === null) {
88 3
            return $this->fetch($url);
89
        }
90
91 3
        return $this->cache->get($url);
92
    }
93
94 3
    public function cache(Preview $preview, $expiresAt = null)
95
    {
96 3
        $this->cache->set($preview, $expiresAt);
97 3
    }
98
    /**
99
     * returns an instance of Preview
100
     * @return Preview
101
     */
102 48
    private function getPreview($body)
103
    {
104 48
        $document = new Document($body);
105
106 48
        $this->crawler = new Crawler($document);
107
108 48
        $data =$this->crawler->getPreviewData($this->url);
109
110 48
        $media = new Media($this->http);
111
112 48
        return new Preview($media, $data);
113
    }
114
}
115