Completed
Push — master ( df288a...fc83fd )
by Joseph
9s
created

PreviewBuilder::cache()   A

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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 PreviewBuilder
10
{
11
    /**
12
     * @var Crawler
13
     */
14
    private $crawler;
15
16
    /**
17
     * @var CacheItemPoolInterface
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 87
    public function __construct(HttpInterface $http, CacheItemPoolInterface $cache = null)
34
    {
35 87
        $this->http = $http;
36 87
        if ($cache !== null) {
37 3
            $this->cache = new Cache($cache);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Jclyons52\PagePreview\Cache\Cache($cache) of type object<Jclyons52\PagePreview\Cache\Cache> is incompatible with the declared type object<Psr\Cache\CacheItemPoolInterface> of property $cache.

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...
38 2
        }
39 87
    }
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 PreviewBuilder($http, $cache);
50
    }
51
    
52
    /**
53
     * @param string $url
54
     * @return Preview
55
     * @throws \Exception
56
     */
57 48
    public function fetch($url)
58
    {
59 48
        $this->url = new Url($url);
60
61 45
        $body = $this->http->get($url);
62
63 45
        if ($body === false) {
64 3
            throw new \Exception('failed to load page');
65
        }
66
67 42
        $document = new Document($body);
68
69 42
        $this->crawler = new Crawler($document);
70
71 42
        return $this->getPreview();
72
    }
73
74 6
    public function findOrFetch($url)
75
    {
76 6
        if ($this->cache === null) {
77 3
            return $this->fetch($url);
78
        }
79
80 3
        return $this->cache->get($url);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Psr\Cache\CacheItemPoolInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
    }
82
83 3
    public function cache(Preview $preview)
84
    {
85 3
        $this->cache->set($preview);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Psr\Cache\CacheItemPoolInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86 3
    }
87
    /**
88
     * returns an instance of Preview
89
     * @return Preview
90
     */
91 42
    private function getPreview()
92
    {
93 42
        $title = $this->crawler->title();
94
95 42
        $images = $this->images();
96
97 42
        $description = $this->crawler->meta('description');
98
99 42
        $meta = $this->crawler->meta();
100
101 42
        $keywords =  $this->crawler->metaKeywords();
102
103 42
        if ($keywords !== []) {
104 36
            $meta['keywords'] = $keywords;
105 24
        }
106
107 42
        $media = new Media($this->http);
108
        
109 42
        return new Preview($media, [
110 42
            'title' => $title,
111 42
            'images' => $images,
112 42
            'description' => $description,
113 42
            'url' => $this->url->original,
114 42
            'meta' => $meta,
115 28
        ]);
116
    }
117
118 42
    private function images()
119
    {
120 42
        $urls = $this->crawler->images();
121 42
        $result = [];
122 42
        foreach ($urls as $url) {
123 39
            $result[] = $this->url->formatRelativeToAbsolute($url);
124 28
        }
125 42
        return $result;
126
    }
127
}
128