Passed
Push — master ( 70f9dc...bcb616 )
by Jon
01:45
created

Article   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 20

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubTitle() 0 3 1
A getCategory() 0 3 1
A getMetaData() 0 3 1
A getComponents() 0 12 3
A getTitle() 0 3 1
A setTitle() 0 2 1
A addComponent() 0 7 2
A getJson() 0 12 1
A setLanguage() 0 3 1
A getLanguage() 0 3 1
A setCategory() 0 3 1
A setSubTitle() 0 2 1
A setMetaData() 0 12 4
1
<?php namespace FlatPlan;
2
3
use FlatPlan\Components\AbstractComponent;
4
5
class Article {
6
7
    private $components = array();
8
    private $defer;
9
    private $articleId;
10
    private $title;
11
    private $subTitle;
12
    private $language;
13
    private $category;
14
    private $metaData;
15
    private $allowedMetaKeys = array(
16
        'authors',
17
        'canonicalURL',
18
        'coverArt',
19
        'dateCreated',
20
        'dateModified',
21
        'datePublished',
22
        'excerpt',
23
        'keywords',
24
        'thumbnailURL',
25
        'transparentToolbar',
26
        'videoURL'
27
    );
28
29
    public function __construct($articleId, $defer = false)
30
    {
31
        $this->articleId = $articleId;
32
        $this->defer     = $defer;
33
    }
34
35
    public function setTitle($title) {
36
        $this->title = $title;
37
    }
38
39
    public function getTitle()
40
    {
41
        return $this->title;
42
    }
43
44
    public function setSubTitle($subTitle) {
45
        $this->subTitle = $subTitle;
46
    }
47
48
    public function getSubTitle()
49
    {
50
        return $this->subTitle;
51
    }
52
53
    public function setLanguage($language)
54
    {
55
        $this->language = $language;
56
    }
57
58
    public function getLanguage()
59
    {
60
        return $this->language;
61
    }
62
63
    public function setCategory($category)
64
    {
65
        $this->category = $category;
66
    }
67
68
    public function getCategory()
69
    {
70
        return $this->category;
71
    }
72
73
    public function setMetaData($metaData)
74
    {
75
        $metaObj = new \stdClass();
76
        if (is_array($metaData)) {
77
            foreach ($metaData as $key => $value) {
78
                if (in_array($key, $this->allowedMetaKeys)) {
79
                    $metaObj->{$key} = $value;
80
                }
81
            }
82
        }
83
84
        $this->metaData = $metaObj;
85
    }
86
87
    public function getMetaData()
88
    {
89
        return $this->metaData;
90
    }
91
92
    public function getJson()
93
    {
94
        $article             = new \stdClass();
95
        $article->articleId  = $this->articleId;
96
        $article->defer      = $this->defer;
97
        $article->title      = $this->getTitle();
98
        $article->subTitle   = $this->getSubTitle();
99
        $article->category   = $this->getCategory();
100
        $article->metaData   = $this->getMetaData();
101
        $article->components = $this->getComponents();
102
103
        return json_encode($article);
104
    }
105
106
    public function getComponents($format = null)
107
    {
108
        $output = array();
109
        foreach ($this->components as $component) {
110
            array_push($output, $component->getComponent());
111
        }
112
113
        if ($format === 'json') {
114
            return json_encode($output);
115
        }
116
117
        return $output;
118
    }
119
120
    /**
121
     * @param AbstractComponent $component
122
     * @throws \ErrorException
123
     * @return void
124
     */
125
    public function addComponent($component)
126
    {
127
        if ($component instanceof AbstractComponent) {
0 ignored issues
show
introduced by
$component is always a sub-type of FlatPlan\Components\AbstractComponent.
Loading history...
128
            $component->updateStyles('root');
129
            array_push($this->components, $component);
130
        } else {
131
            throw new \ErrorException('Invalid component supplied.');
132
        }
133
    }
134
}
135