Completed
Push — master ( a6519a...822e25 )
by Joseph
02:30
created

Preview   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 152
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.55%

Importance

Changes 8
Bugs 0 Features 3
Metric Value
wmc 19
c 8
b 0
f 3
lcom 1
cbo 7
dl 16
loc 152
ccs 68
cts 69
cp 0.9855
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() 8 8 2
A metaKeywords() 0 11 1
A metaTitle() 8 8 2
A meta() 0 9 2
A images() 0 10 2
B render() 0 24 3
A formatUrl() 0 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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