Passed
Push — master ( 13c815...1a2524 )
by Jon
01:52
created

Article::setMetaData()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 67
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 46
nc 4
nop 1
dl 0
loc 67
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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' => 'array',
18
        'canonicalURL' => 'uri',
19
        'coverArt' => 'array',
20
        'dateCreated' => 'datetime',
21
        'dateModified' => 'datetime',
22
        'datePublished' => 'datetime',
23
        'excerpt' => 'string',
24
        'keywords' => 'array',
25
        'links' => 'array',
26
        'thumbnailURL' => 'uri',
27
        'transparentToolbar' => 'boolean',
28
        'videoURL' => 'uri',
29
        'accessoryText' => 'string',
30
        'isCandidateToBeFeatured' => 'boolean',
31
        'isHidden' => 'boolean',
32
        'isPreview' => 'boolean',
33
        'isSponsored' => 'boolean',
34
        'maturityRating' => 'string'
35
    );
36
37
    public function __construct($articleId, $status = 'draft')
38
    {
39
        $this->articleId = $articleId;
40
        $this->status    = $status;
41
    }
42
43
    public function setTitle($title) {
44
        $this->title = $title;
45
    }
46
47
    public function getTitle()
48
    {
49
        return $this->title;
50
    }
51
52
    public function setSubTitle($subTitle) {
53
        $this->subTitle = $subTitle;
54
    }
55
56
    public function getSubTitle()
57
    {
58
        return $this->subTitle;
59
    }
60
61
    public function setLanguage($language)
62
    {
63
        $this->language = $language;
64
    }
65
66
    public function getLanguage()
67
    {
68
        return $this->language;
69
    }
70
71
    public function setCategory($category)
72
    {
73
        $this->category = $category;
74
    }
75
76
    public function getCategory()
77
    {
78
        return $this->category;
79
    }
80
81
    public function setTemplate($template)
82
    {
83
        $this->template = $template;
84
    }
85
86
    public function getTemplate()
87
    {
88
        return $this->template;
89
    }
90
91
    public function setMetaData($metaData)
92
    {
93
        $metaObj = new \stdClass();
94
        $errors  = array();
95
        if (is_array($metaData)) {
96
            foreach ($metaData as $key => $value) {
97
                if (isset($this->allowedMetaKeys[$key])) {
98
                    $type = gettype($value);
99
                    switch ($this->allowedMetaKeys[$key]) {
100
                        case 'uri':
101
                            if (filter_var($value, FILTER_VALIDATE_URL)) {
102
                                $metaObj->{$key} = $value;
103
                            } else {
104
                                $errors[] = array(
105
                                    'key' => $key,
106
                                    'message' => 'Not a valid URI'
107
                                );
108
                            }
109
                            break;
110
111
                        case 'datetime':
112
                            if ($value instanceof \DateTime) {
113
                                $metaObj->{$key} = $value;
114
                            } else {
115
                                $errors[] = array(
116
                                    'key' => $key,
117
                                    'message' => 'Not a valid DateTime object'
118
                                );
119
                            }
120
                            break;
121
122
                        case 'array':
123
                            if (is_array($value)) {
124
                                $metaObj->{$key} = $value;
125
                            } else {
126
                                $errors[] = array(
127
                                    'key' => $key,
128
                                    'message' => 'Not a valid Array'
129
                                );
130
                            }
131
                            break;
132
133
                        default:
134
                            if ($type === $this->allowedMetaKeys[$key]) {
135
                                $metaObj->{$key} = $value;
136
                            } else {
137
                                $errors[] = array(
138
                                    'key' => $key,
139
                                    'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type
140
                                );
141
                            }
142
                            break;
143
                    }
144
                } else {
145
                    $errors[] = array(
146
                        'key' => $key,
147
                        'message' => 'Not a valid MetaData key'
148
                    );
149
                }
150
            }
151
        }
152
153
        if (!empty($errors)) {
154
            throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true));
155
        }
156
157
        $this->metaData = $metaObj;
158
    }
159
160
    public function getMetaData()
161
    {
162
        return $this->metaData;
163
    }
164
165
    public function getJson()
166
    {
167
        $article             = new \stdClass();
168
        $article->articleId  = $this->articleId;
169
        $article->language   = $this->getLanguage();
170
        $article->status     = $this->status;
171
        $article->title      = $this->getTitle();
172
        $article->subTitle   = $this->getSubTitle();
173
        $article->category   = $this->getCategory();
174
        $article->template   = $this->getTemplate();
175
        $article->metadata   = $this->getMetaData();
176
        $article->components = $this->getComponents();
177
178
        return json_encode($article, JSON_UNESCAPED_UNICODE);
179
    }
180
181
    public function getComponents($format = null)
182
    {
183
        $output = array();
184
        foreach ($this->components as $component) {
185
            array_push($output, $component->getComponent());
186
        }
187
188
        if ($format === 'json') {
189
            return json_encode($output, JSON_UNESCAPED_UNICODE);
190
        }
191
192
        return $output;
193
    }
194
195
    /**
196
     * @param AbstractComponent $component
197
     * @throws \ErrorException
198
     * @return void
199
     */
200
    public function setComponents($components)
201
    {
202
        if (is_array($components)) {
203
            foreach ($components as $component) {
204
                if ($component instanceof AbstractComponent && !is_null($component->getRole())) {
205
                    $component->updateStyles('root');
206
                    array_push($this->components, $component);
207
                }
208
            }
209
        } else if ($components instanceof AbstractComponent && !is_null($components->getRole())) {
210
            $components->updateStyles('root');
211
            array_push($this->components, $components);
212
        }
213
    }
214
}
215