Passed
Push — master ( 015d2a...5a3c63 )
by Jon
01:44
created

Article   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 147
rs 10
c 0
b 0
f 0
wmc 25

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplate() 0 3 1
A __construct() 0 4 1
A setComponents() 0 14 5
A getSubTitle() 0 3 1
A getCategory() 0 3 1
A getMetaData() 0 3 1
A setTemplate() 0 3 1
A getComponents() 0 12 3
A getTitle() 0 3 1
A setTitle() 0 2 1
A getJson() 0 14 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 $status;
9
    private $articleId;
10
    private $title;
11
    private $subTitle;
12
    private $language = 'en';
13
    private $category;
14
    private $template;
15
    private $metaData;
16
    private $allowedMetaKeys = array(
17
        'authors',
18
        'canonicalURL',
19
        'coverArt',
20
        'dateCreated',
21
        'dateModified',
22
        'datePublished',
23
        'excerpt',
24
        'keywords',
25
        'thumbnailURL',
26
        'transparentToolbar',
27
        'videoURL'
28
    );
29
30
    public function __construct($articleId, $status = 'draft')
31
    {
32
        $this->articleId = $articleId;
33
        $this->status    = $status;
34
    }
35
36
    public function setTitle($title) {
37
        $this->title = $title;
38
    }
39
40
    public function getTitle()
41
    {
42
        return $this->title;
43
    }
44
45
    public function setSubTitle($subTitle) {
46
        $this->subTitle = $subTitle;
47
    }
48
49
    public function getSubTitle()
50
    {
51
        return $this->subTitle;
52
    }
53
54
    public function setLanguage($language)
55
    {
56
        $this->language = $language;
57
    }
58
59
    public function getLanguage()
60
    {
61
        return $this->language;
62
    }
63
64
    public function setCategory($category)
65
    {
66
        $this->category = $category;
67
    }
68
69
    public function getCategory()
70
    {
71
        return $this->category;
72
    }
73
74
    public function setTemplate($template)
75
    {
76
        $this->template = $template;
77
    }
78
79
    public function getTemplate()
80
    {
81
        return $this->template;
82
    }
83
84
    public function setMetaData($metaData)
85
    {
86
        $metaObj = new \stdClass();
87
        if (is_array($metaData)) {
88
            foreach ($metaData as $key => $value) {
89
                if (in_array($key, $this->allowedMetaKeys)) {
90
                    $metaObj->{$key} = $value;
91
                }
92
            }
93
        }
94
95
        $this->metaData = $metaObj;
96
    }
97
98
    public function getMetaData()
99
    {
100
        return $this->metaData;
101
    }
102
103
    public function getJson()
104
    {
105
        $article             = new \stdClass();
106
        $article->articleId  = $this->articleId;
107
        $article->language   = $this->getLanguage();
108
        $article->status     = $this->status;
109
        $article->title      = $this->getTitle();
110
        $article->subTitle   = $this->getSubTitle();
111
        $article->category   = $this->getCategory();
112
        $article->template   = $this->getTemplate();
113
        $article->metaData   = $this->getMetaData();
114
        $article->components = $this->getComponents();
115
116
        return json_encode($article);
117
    }
118
119
    public function getComponents($format = null)
120
    {
121
        $output = array();
122
        foreach ($this->components as $component) {
123
            array_push($output, $component->getComponent());
124
        }
125
126
        if ($format === 'json') {
127
            return json_encode($output);
128
        }
129
130
        return $output;
131
    }
132
133
    /**
134
     * @param AbstractComponent $component
135
     * @throws \ErrorException
136
     * @return void
137
     */
138
    public function setComponents($components)
139
    {
140
        if (is_array($components)) {
141
            foreach ($components as $component) {
142
                if ($component instanceof AbstractComponent) {
143
                    $component->updateStyles('root');
144
                    array_push($this->components, $component);
145
                }
146
            }
147
        } else if ($components instanceof AbstractComponent) {
148
            $components->updateStyles('root');
149
            array_push($this->components, $components);
150
        } else {
151
            throw new \ErrorException('Invalid component supplied.');
152
        }
153
    }
154
}
155