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); |
|
|
|
|
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); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function cache(Preview $preview) |
84
|
|
|
{ |
85
|
3 |
|
$this->cache->set($preview); |
|
|
|
|
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
|
|
|
|
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..