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

Preview::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 8
Bugs 0 Features 4
Metric Value
c 8
b 0
f 4
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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