Completed
Push — master ( b623ba...fc6e2c )
by Joseph
02:37
created

Preview   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 1 Features 5
Metric Value
wmc 7
c 14
b 1
f 5
lcom 1
cbo 1
dl 0
loc 78
ccs 31
cts 31
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A setViewPath() 0 4 1
A render() 0 6 1
A toArray() 0 18 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
    private $viewPath;
20
    
21 42
    public function __construct($data)
22
    {
23 42
        $this->url = $data['url'];
24
        
25 42
        $this->title = $data['title'];
26
        
27 42
        $this->description = $data['description'];
28
        
29 42
        $this->images = $data['images'];
30
        
31 42
        $this->meta = $data['meta'];
32
        
33 42
        $this->viewPath = __DIR__ . '/Templates';
34 42
    }
35
36
    /**
37
     * @param string $viewPath
38
     */
39 3
    public function setViewPath($viewPath)
40
    {
41 3
        $this->viewPath = $viewPath;
42 3
    }
43
44
    /**
45
     * @param string $type
46
     * @return string
47
     */
48 15
    public function render($type = 'media')
49
    {
50 15
        $templates = new Engine($this->viewPath);
51
52 15
        return $templates->render($type, $this->toArray());
53
    }
54
55
    /**
56
     * @return array
57
     */
58 18
    public function toArray()
59 3
    {
60
61 18
        $title = $this->title;
62 18
        if (array_key_exists('title', $this->meta)) {
63 15
            $title = $this->meta['title'];
64 15
        }
65 18
        if (count($this->images) < 1) {
66 3
            $this->images[0] = null;
67 3
        }
68
        return [
69 18
            'title' => $title,
70 18
            'images' => $this->images,
71 18
            'description' => $this->description,
72 18
            'url' => $this->url,
73 18
            'meta' => $this->meta,
74 18
        ];
75
    }
76
77
    /**
78
     * @return string
79
     */
80 3
    public function toJson()
81
    {
82 3
        return json_encode($this->toArray());
83
    }
84
}
85