Completed
Push — master ( 7eb291...e8990c )
by Joseph
03:42
created

Preview   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 15
c 4
b 0
f 1
lcom 1
cbo 7
dl 0
loc 132
ccs 52
cts 52
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 14 1
A title() 0 4 1
A metaDescription() 0 4 1
A metaKeywords() 0 11 1
A metaTitle() 0 4 1
A meta() 0 9 2
A images() 0 10 2
A render() 0 12 1
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 27
    public function __construct(Client $client)
22
    {
23 27
        $this->client = $client;
24 27
    }
25
26
    /**
27
     * @param string $url
28
     * @return Document
29
     */
30 27
    public function fetch($url)
31
    {
32 27
        $this->url = $url;
33
34 27
        $this->urlComponents = parse_url($url);
35
36 27
        $this->result = $this->client->request('GET', $url);
37
38 24
        $body = $this->result->getBody()->getContents();
39
40 24
        $this->document = new Document($body);
41
42 24
        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 6
    public function metaDescription()
57 3
    {
58 6
        return $this->document->querySelector('meta[name="description"]')->attr('content');
59 3
    }
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 6
    public function metaTitle()
80
    {
81 6
        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 3
        }
94 3
        return $values;
95
    }
96
97
    /**
98
     * @return array
99
     */
100 6
    public function images()
101
    {
102 6
        $images = $this->document->querySelectorAll('img');
103 6
        $urls = $images->attr('src');
104 6
        $result = [];
105 6
        foreach ($urls as $url) {
106 6
            $result[] = $this->formatUrl($url);
107 6
        }
108 6
        return $result;
109
    }
110
111 3
    public function render($type = 'card')
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113 3
        $title = $this->metaTitle();
114
115 3
        $image = $this->images()[0];
116
117 3
        $description = $this->metaDescription();
118
119 3
        $templates = new Engine(__DIR__ . '/Templates');
120
121 3
        return $templates->render('card', [ 'title' => $title, 'image' => $image, 'body' => $description, 'url' => $this->url ]);
122
    }
123
124
    /**
125
     * @param string $url
126
     * @return array
127
     */
128 6
    private function formatUrl($url)
129
    {
130 6
        $path = array_key_exists('path', $this->urlComponents) ? $this->urlComponents['path'] : '';
131
132 6
        if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
133 6
            return $url;
134
        }
135 6
        if (substr($url, 0, 1) === '/') {
136 6
            return 'http://' . $this->urlComponents['host'] . $url;
137
        }
138 6
        return 'http://' . $this->urlComponents['host'] .$path . '/' . $url;
139
    }
140
}
141