Completed
Push — master ( 1b7a6e...a6519a )
by Joseph
02:41
created

Preview   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.61%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 16
c 7
b 0
f 2
lcom 1
cbo 7
dl 0
loc 140
ccs 57
cts 59
cp 0.9661
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 14 1
A metaDescription() 0 4 1
A metaKeywords() 0 11 1
A metaTitle() 0 4 1
A meta() 0 9 2
A title() 0 4 1
A images() 0 10 2
A render() 0 20 2
A formatUrl() 0 12 4
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 30
    public function __construct(Client $client)
22
    {
23 30
        $this->client = $client;
24 30
    }
25
26
    /**
27
     * @param string $url
28
     * @return Document
29
     */
30 30
    public function fetch($url)
31
    {
32 30
        $this->url = $url;
33
34 30
        $this->urlComponents = parse_url($url);
35
36 30
        $this->result = $this->client->request('GET', $url);
37
38 27
        $body = $this->result->getBody()->getContents();
39
40 27
        $this->document = new Document($body);
41
42 27
        return $this->document;
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48 3
    public function title()
49
    {
50 3
        return $this->document->querySelector('title')->text();
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56 9
    public function metaDescription()
57 2
    {
58 9
        return $this->document->querySelector('meta[name="description"]')->attr('content');
59 2
    }
60
61
    /**
62
     * @return mixed
63
     */
64 3
    public function metaKeywords()
65
    {
66 3
        $keywordString = $this->document->querySelector('meta[name="keywords"]')->attr('content');
67
68 3
        $keywords = explode(',', $keywordString);
69
70 3
        return array_map(function ($word) {
71 3
            return trim($word);
72
73 3
        }, $keywords);
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79 9
    public function metaTitle()
80
    {
81 9
        return $this->document->querySelector('meta[name="title"]')->attr('content');
82
    }
83
84
    /**
85
     * @return array
86
     */
87 3
    public function meta()
88
    {
89 3
        $metaTags = $this->document->querySelectorAll('meta');
90 3
        $values = [];
91 3
        foreach ($metaTags as $meta) {
92 3
            $values[$meta->attr('name')] = $meta->attr('content');
93 2
        }
94 3
        return $values;
95
    }
96
97
    /**
98
     * @return array
99
     */
100 9
    public function images()
101
    {
102 9
        $images = $this->document->querySelectorAll('img');
103 9
        $urls = $images->attr('src');
104 9
        $result = [];
105 9
        foreach ($urls as $url) {
106 9
            $result[] = $this->formatUrl($url);
107 6
        }
108 9
        return $result;
109
    }
110
111 8
    public function render($type = 'media')
112
    {
113 6
        $title = $this->metaTitle();
114
115 6
        if ($title === null) {
116
            $title = $this->title();
117
        }
118
119 6
        $image = $this->images()[0];
120
121 6
        $description = $this->metaDescription();
122
123 6
        $templates = new Engine(__DIR__ . '/Templates');
124
125 6
        return $templates->render($type, [
126 7
            'title' => $title,
127 6
            'image' => $image,
128 7
            'body' => $description, 'url' => $this->url
129 4
        ]);
130
    }
131
132
    /**
133
     * @param string $url
134
     * @return array
135
     */
136 9
    private function formatUrl($url)
137
    {
138 9
        $path = array_key_exists('path', $this->urlComponents) ? $this->urlComponents['path'] : '';
139
140 9
        if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
141 9
            return $url;
142
        }
143 9
        if (substr($url, 0, 1) === '/') {
144 9
            return 'http://' . $this->urlComponents['host'] . $url;
145
        }
146 9
        return 'http://' . $this->urlComponents['host'] .$path . '/' . $url;
147
    }
148
}
149