1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jclyons52\PagePreview; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Jclyons52\PHPQuery\Document; |
7
|
|
|
use League\Plates\Engine; |
8
|
|
|
|
9
|
|
|
class Preview |
10
|
|
|
{ |
11
|
|
|
protected $result; |
12
|
|
|
|
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
public $document; |
16
|
|
|
|
17
|
|
|
public $url; |
18
|
|
|
|
19
|
|
|
protected $urlComponents; |
20
|
|
|
|
21
|
33 |
|
public function __construct(Client $client) |
22
|
|
|
{ |
23
|
33 |
|
$this->client = $client; |
24
|
33 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $url |
28
|
|
|
* @return Document |
29
|
|
|
*/ |
30
|
33 |
|
public function fetch($url) |
31
|
|
|
{ |
32
|
33 |
|
$this->url = $url; |
33
|
|
|
|
34
|
33 |
|
$this->urlComponents = parse_url($url); |
35
|
|
|
|
36
|
33 |
|
$this->result = $this->client->request('GET', $url); |
37
|
|
|
|
38
|
30 |
|
$body = $this->result->getBody()->getContents(); |
39
|
|
|
|
40
|
30 |
|
$this->document = new Document($body); |
41
|
|
|
|
42
|
30 |
|
return $this->document; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
6 |
|
public function title() |
49
|
|
|
{ |
50
|
6 |
|
return $this->document->querySelector('title')->text(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
12 |
View Code Duplication |
public function metaDescription() |
|
|
|
|
57
|
2 |
|
{ |
58
|
12 |
|
$description = $this->document->querySelector('meta[name="description"]'); |
59
|
12 |
|
if ($description === null) { |
60
|
5 |
|
return null; |
61
|
4 |
|
} |
62
|
9 |
|
return $description->attr('content'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
3 |
|
public function metaKeywords() |
69
|
|
|
{ |
70
|
3 |
|
$keywordString = $this->document->querySelector('meta[name="keywords"]')->attr('content'); |
71
|
|
|
|
72
|
3 |
|
$keywords = explode(',', $keywordString); |
73
|
|
|
|
74
|
3 |
|
return array_map(function ($word) { |
75
|
3 |
|
return trim($word); |
76
|
|
|
|
77
|
3 |
|
}, $keywords); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
12 |
View Code Duplication |
public function metaTitle() |
|
|
|
|
84
|
|
|
{ |
85
|
12 |
|
$title = $this->document->querySelector('meta[name="title"]'); |
86
|
12 |
|
if ($title === null) { |
87
|
3 |
|
return null; |
88
|
|
|
} |
89
|
9 |
|
return $title->attr('content'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
3 |
|
public function meta() |
96
|
|
|
{ |
97
|
3 |
|
$metaTags = $this->document->querySelectorAll('meta'); |
98
|
3 |
|
$values = []; |
99
|
3 |
|
foreach ($metaTags as $meta) { |
100
|
3 |
|
$values[$meta->attr('name')] = $meta->attr('content'); |
101
|
2 |
|
} |
102
|
3 |
|
return $values; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
12 |
|
public function images() |
109
|
|
|
{ |
110
|
12 |
|
$images = $this->document->querySelectorAll('img'); |
111
|
12 |
|
$urls = $images->attr('src'); |
112
|
12 |
|
$result = []; |
113
|
12 |
|
foreach ($urls as $url) { |
114
|
12 |
|
$result[] = $this->formatUrl($url); |
115
|
8 |
|
} |
116
|
12 |
|
return $result; |
117
|
|
|
} |
118
|
|
|
|
119
|
11 |
|
public function render($type = 'media') |
120
|
|
|
{ |
121
|
9 |
|
$title = $this->metaTitle(); |
122
|
|
|
|
123
|
9 |
|
if ($title === null) { |
124
|
3 |
|
$title = $this->title(); |
125
|
2 |
|
} |
126
|
10 |
|
$images = $this->images(); |
127
|
9 |
|
if (count($images) > 0) { |
128
|
10 |
|
$image = $images[0]; |
129
|
6 |
|
} else { |
130
|
|
|
$image = '#'; |
131
|
|
|
} |
132
|
|
|
|
133
|
9 |
|
$description = $this->metaDescription(); |
134
|
|
|
|
135
|
9 |
|
$templates = new Engine(__DIR__ . '/Templates'); |
136
|
|
|
|
137
|
9 |
|
return $templates->render($type, [ |
138
|
9 |
|
'title' => $title, |
139
|
9 |
|
'image' => $image, |
140
|
9 |
|
'body' => $description, 'url' => $this->url |
141
|
6 |
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $url |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
12 |
|
private function formatUrl($url) |
149
|
|
|
{ |
150
|
12 |
|
$path = array_key_exists('path', $this->urlComponents) ? $this->urlComponents['path'] : ''; |
151
|
|
|
|
152
|
12 |
|
if (filter_var($url, FILTER_VALIDATE_URL) !== false) { |
153
|
12 |
|
return $url; |
154
|
|
|
} |
155
|
12 |
|
if (substr($url, 0, 1) === '/') { |
156
|
12 |
|
return 'http://' . $this->urlComponents['host'] . $url; |
157
|
|
|
} |
158
|
12 |
|
return 'http://' . $this->urlComponents['host'] .$path . '/' . $url; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.