Preview   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 19
Bugs 3 Features 8
Metric Value
wmc 10
c 19
b 3
f 8
lcom 1
cbo 3
dl 0
loc 96
ccs 42
cts 42
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setViewPath() 0 4 1
A __construct() 0 20 2
A render() 0 14 3
A toArray() 0 20 3
A toJson() 0 4 1
1
<?php
2
3
namespace Jclyons52\PagePreview;
4
5
use League\Plates\Engine;
6
7
class Preview
8
{
9
    public $url;
10
    
11
    public $title;
12
    
13
    public $description;
14
    
15
    public $images;
16
17
    public $meta;
18
    
19
    public $media = false;
20
    
21
    private $viewPath;
22
    
23 75
    public function __construct(Media $mediaObj, $data)
24
    {
25 75
        $this->url = $data['url'];
26
        
27 75
        $this->title = $data['title'];
28
        
29 75
        $this->description = $data['description'];
30
        
31 75
        $this->images = $data['images'];
32
        
33 75
        $this->meta = new Meta($data['meta']);
34
35 75
        $media = $mediaObj->get($data['url']);
36
        
37 75
        if ($media !== []) {
38 6
            $this->media = $media;
0 ignored issues
show
Documentation Bug introduced by
It seems like $media of type array is incompatible with the declared type boolean of property $media.

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...
39 6
        }
40
        
41 75
        $this->viewPath = __DIR__ . '/Templates';
42 75
    }
43
44
    /**
45
     * @param string $viewPath
46
     */
47 3
    public function setViewPath($viewPath)
48
    {
49 3
        $this->viewPath = $viewPath;
50 3
    }
51
52
    /**
53
     * @param string $type
54
     * @return string
55
     */
56 18
    public function render($type = null)
57 3
    {
58 18
        $templates = new Engine($this->viewPath);
59
60 18
        if ($type === null) {
61 12
            if ($this->media === false) {
62 9
                $type = 'media';
63 9
            } else {
64 3
                $type = 'thumbnail';
65
            }
66 12
        }
67
68 18
        return $templates->render($type, $this->toArray());
69
    }
70
71
    /**
72
     * @return array
73
     */
74 21
    public function toArray()
75
    {
76
77 21
        $title = $this->title;
78
79 21
        if (array_key_exists('title', $this->meta)) {
80 18
            $title = $this->meta['title'];
81 18
        }
82 21
        if (count($this->images) < 1) {
83 3
            $this->images[0] = null;
84 3
        }
85
        return [
86 21
            'title' => $title,
87 21
            'images' => $this->images,
88 21
            'description' => $this->description,
89 21
            'url' => $this->url,
90 21
            'meta' => $this->meta,
91 21
            'media' => $this->media,
92 21
        ];
93
    }
94
95
    /**
96
     * @return string
97
     */
98 3
    public function toJson()
99
    {
100 3
        return json_encode($this->toArray());
101
    }
102
}
103