Completed
Push — master ( f1ece3...e7fa22 )
by Joseph
02:19
created

PreviewBuilder::metaKeywords()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Jclyons52\PagePreview;
4
5
use Jclyons52\PHPQuery\Document;
6
7
class PreviewBuilder
8
{
9
10
    /**
11
     * PHPQuery document object that will be used to select elements
12
     * @var \Jclyons52\PHPQuery\Document
13
     */
14
    public $crawler;
15
16
    /**
17
     * Url object
18
     * @var Url
19
     */
20
    public $url;
21
22
    /**
23
     * destructured array of url components from parse_url
24
     * @var array
25
     */
26
    protected $urlComponents;
27
28
    /**
29
     * @var HttpInterface
30
     */
31
    protected $http;
32
33
    
34 81
    public function __construct(HttpInterface $http)
35
    {
36 81
        $this->http = $http;
37 81
    }
38
39
    /**
40
     * Instantiate class with dependencies
41
     * @return static
42
     */
43 3
    public static function create()
44
    {
45 3
        $http = new Http();
46 3
        return new PreviewBuilder($http);
47
    }
48
    
49
    /**
50
     * @param string $url
51
     * @return Preview
52
     * @throws \Exception
53
     */
54 45
    public function fetch($url)
55
    {
56 45
        $this->url = new Url($url);
57
58 42
        $body = $this->http->get($url);
59
60 42
        if ($body === false) {
61 3
            throw new \Exception('failed to load page');
62
        }
63
64 39
        $document = new Document($body);
65
66 39
        $this->crawler = new Crawler($document);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Jclyons52\PagePreview\Crawler($document) of type object<Jclyons52\PagePreview\Crawler> is incompatible with the declared type object<Jclyons52\PHPQuery\Document> of property $crawler.

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

Loading history...
67
68 39
        return $this->getPreview();
69
    }
70
71
    /**
72
     * returns an instance of Preview
73
     * @return Preview
74
     */
75 39
    public function getPreview()
76
    {
77 39
        $title = $this->crawler->title();
0 ignored issues
show
Bug introduced by
The method title() does not seem to exist on object<Jclyons52\PHPQuery\Document>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
79 39
        $images = $this->images();
80
81 39
        $description = $this->crawler->meta('description');
0 ignored issues
show
Bug introduced by
The method meta() does not seem to exist on object<Jclyons52\PHPQuery\Document>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83 39
        $meta = $this->crawler->meta();
0 ignored issues
show
Bug introduced by
The method meta() does not seem to exist on object<Jclyons52\PHPQuery\Document>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
85 39
        $keywords =  $this->crawler->metaKeywords();
0 ignored issues
show
Bug introduced by
The method metaKeywords() does not seem to exist on object<Jclyons52\PHPQuery\Document>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
87 39
        if ($keywords !== []) {
88 33
            $meta['keywords'] = $keywords;
89 22
        }
90
91 39
        $media = new Media($this->http);
92
        
93 39
        return new Preview($media, [
94 39
            'title' => $title,
95 39
            'images' => $images,
96 39
            'description' => $description,
97 39
            'url' => $this->url->original,
98 39
            'meta' => $meta,
99 26
        ]);
100
    }
101
102 39
    public function images()
103
    {
104 39
        $urls = $this->crawler->images();
0 ignored issues
show
Bug introduced by
The method images() does not seem to exist on object<Jclyons52\PHPQuery\Document>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105 39
        $result = [];
106 39
        foreach ($urls as $url) {
107 36
            $result[] = $this->url->formatRelativeToAbsolute($url);
108 26
        }
109 39
        return $result;
110
    }
111
}
112