ObjectBase   F
last analyzed

Complexity

Total Complexity 63

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Test Coverage

Coverage 68.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 149
dl 0
loc 321
ccs 88
cts 129
cp 0.6822
rs 3.36
c 1
b 0
f 0
wmc 63

2 Methods

Rating   Name   Duplication   Size   Complexity  
F getProperties() 0 69 17
D assignProperties() 0 121 46

How to fix   Complexity   

Complex Class

Complex classes like ObjectBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ObjectBase, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Mpyw\OpenGraph\Objects;
4
5
use DateTimeInterface;
6
use Mpyw\OpenGraph\Elements\Audio;
7
use Mpyw\OpenGraph\Elements\Image;
8
use Mpyw\OpenGraph\Elements\Video;
9
use Mpyw\OpenGraph\Exceptions\UnexpectedValueException;
10
use Mpyw\OpenGraph\GenericHelper;
11
use Mpyw\OpenGraph\Property;
12
13
/**
14
 * Abstract base class for all Open Graph objects (website, video, ...)
15
 */
16
abstract class ObjectBase
17
{
18
    use GenericHelper;
19
20
    /**
21
     * An array of audio resources attached to the object.
22
     *
23
     * @var Audio[]
24
     */
25
    public $audios = [];
26
27
    /**
28
     * A short description of the object.
29
     *
30
     * @var null|string
31
     */
32
    public $description;
33
34
    /**
35
     * The word that appears before the object's title in a sentence. This is an list of words from 'a', 'an', 'the',
36
     * ' "" ', or 'auto'. If 'auto' is chosen, the consumer of the object will chose between 'a' or 'an'. The default is
37
     * the blank, "".
38
     *
39
     * @var null|string
40
     */
41
    public $determiner;
42
43
    /**
44
     * An array of images attached to the object.
45
     *
46
     * @var Image[]
47
     */
48
    public $images = [];
49
50
    /**
51
     * The locale that the object's tags are marked up in, in the format language_TERRITORY.
52
     *
53
     * @var null|string
54
     */
55
    public $locale;
56
57
    /**
58
     * An array of alternate locales in which the resource is available.
59
     *
60
     * @var string[]
61
     */
62
    public $localeAlternate = [];
63
64
    /**
65
     * The price amount specified.
66
     *
67
     * @var null|string
68
     */
69
    public $priceAmount;
70
71
    /**
72
     * The currency code specified.
73
     *
74
     * @var null|string
75
     */
76
    public $priceCurrency;
77
    
78
    /**
79
     * @var null|bool
80
     */
81
    public $richAttachment;
82
83
    /**
84
     * An array of URLs of related resources.
85
     *
86
     * @var string[]
87
     */
88
    public $seeAlso = [];
89
90
    /**
91
     * The name of the web site upon which the object resides.
92
     *
93
     * @var null|string
94
     */
95
    public $siteName;
96
97
    /**
98
     * The title of the object as it should appear in the graph.
99
     *
100
     * @var null|string
101
     */
102
    public $title;
103
104
    /**
105
     * The type of the object, such as 'article'.
106
     *
107
     * @var null|string
108
     */
109
    public $type;
110
111
    /**
112
     * The time when the object was last updated.
113
     *
114
     * @var null|DateTimeInterface
115
     */
116
    public $updatedTime;
117
118
    /**
119
     * The canonical URL of the object, used as its ID in the graph.
120
     *
121
     * @var null|string
122
     */
123
    public $url;
124
125
    /**
126
     * An array of videos attached to the object.
127
     *
128
     * @var Video[]
129
     */
130
    public $videos = [];
131
132
    /**
133
     * Assigns all properties given to the this Object instance.
134
     *
135
     * @param  Property[]               $properties Array of all properties to assign.
136
     * @param  bool                     $debug      Throw exceptions when parsing or not.
137
     * @throws UnexpectedValueException
138
     * @return $this
139
     */
140 17
    public function assignProperties(array $properties, bool $debug = false)
141
    {
142 17
        foreach ($properties as $property) {
143 13
            $name = $property->key;
144 13
            $value = $property->value;
145
146
            switch ($name) {
147 13
                case Property::AUDIO:
148 13
                case Property::AUDIO_URL:
149 1
                    $this->audios[] = new Audio($value);
150 1
                    break;
151 13
                case Property::AUDIO_SECURE_URL:
152 12
                case Property::AUDIO_TYPE:
153 3
                    if (count($this->audios) > 0) {
154 1
                        $this->audios[count($this->audios) - 1]->setAttribute($name, $value);
155 2
                    } elseif ($debug) {
156 1
                        throw new UnexpectedValueException(
157 1
                            sprintf(
158 1
                                "Found '%s' property but no audio was found before.",
159
                                $name
160
                            )
161
                        );
162
                    }
163 2
                    break;
164 10
                case Property::DESCRIPTION:
165 1
                    if ($this->description === null) {
166 1
                        $this->description = (string)$value;
167
                    }
168 1
                    break;
169 10
                case Property::DETERMINER:
170 1
                    if ($this->determiner === null) {
171 1
                        $this->determiner = (string)$value;
172
                    }
173 1
                    break;
174 10
                case Property::IMAGE:
175 10
                case Property::IMAGE_URL:
176 2
                    $this->images[] = new Image($value);
177 2
                    break;
178 10
                case Property::IMAGE_HEIGHT:
179 10
                case Property::IMAGE_SECURE_URL:
180 10
                case Property::IMAGE_TYPE:
181 10
                case Property::IMAGE_WIDTH:
182 6
                case Property::IMAGE_USER_GENERATED:
183 4
                    if (count($this->images) > 0) {
184 2
                        $this->images[count($this->images) - 1]->setAttribute($name, $value);
185 2
                    } elseif ($debug) {
186 1
                        throw new UnexpectedValueException(
187 1
                            sprintf(
188 1
                                "Found '%s' property but no image was found before.",
189
                                $name
190
                            )
191
                        );
192
                    }
193 3
                    break;
194 6
                case Property::LOCALE:
195 1
                    if ($this->locale === null) {
196 1
                        $this->locale = (string)$value;
197
                    }
198 1
                    break;
199 6
                case Property::LOCALE_ALTERNATE:
200 1
                    $this->localeAlternate[] = (string)$value;
201 1
                    break;
202 6
                case Property::PRICE_AMOUNT:
203
                    if ($this->priceAmount === null) {
204
                        $this->priceAmount = (string)$value;
205
                    }
206
                    break;
207 6
                case Property::PRICE_CURRENCY:
208
                    if ($this->priceCurrency === null) {
209
                        $this->priceCurrency = (string)$value;
210
                    }
211
                    break;
212 6
                case Property::RICH_ATTACHMENT:
213 1
                    $this->richAttachment = $this->convertToBoolean($value);
214 1
                    break;
215 6
                case Property::SEE_ALSO:
216 1
                    $this->seeAlso[] = (string)$value;
217 1
                    break;
218 6
                case Property::SITE_NAME:
219 1
                    if ($this->siteName === null) {
220 1
                        $this->siteName = (string)$value;
221
                    }
222 1
                    break;
223 6
                case Property::TITLE:
224 3
                    if ($this->title === null) {
225 3
                        $this->title = (string)$value;
226
                    }
227 3
                    break;
228 4
                case Property::UPDATED_TIME:
229 1
                    if ($this->updatedTime === null) {
230 1
                        $this->updatedTime = $this->convertToDateTime($value);
231
                    }
232 1
                    break;
233 4
                case Property::URL:
234 1
                    if ($this->url === null) {
235 1
                        $this->url = (string)$value;
236
                    }
237 1
                    break;
238 3
                case Property::VIDEO:
239 3
                case Property::VIDEO_URL:
240 1
                    $this->videos[] = new Video($value);
241 1
                    break;
242 3
                case Property::VIDEO_HEIGHT:
243 3
                case Property::VIDEO_SECURE_URL:
244 3
                case Property::VIDEO_TYPE:
245 3
                case Property::VIDEO_WIDTH:
246 3
                    if (count($this->videos) > 0) {
247 1
                        $this->videos[count($this->videos) - 1]->setAttribute($name, $value);
248 2
                    } elseif ($debug) {
249 1
                        throw new UnexpectedValueException(
250 1
                            sprintf(
251 1
                                "Found '%s' property but no video was found before.",
252
                                $name
253
                            )
254
                        );
255
                    }
256 2
                    break;
257
            }
258
        }
259
260 14
        return $this;
261
    }
262
263
    /**
264
     * Gets all properties set on this object.
265
     *
266
     * @return Property[]
267
     */
268
    public function getProperties(): array
269
    {
270
        $properties = [];
271
272
        foreach ($this->audios as $audio) {
273
            $properties = array_merge($properties, $audio->getProperties());
274
        }
275
276
        if ($this->title !== null) {
277
            $properties[] = new Property(Property::TITLE, $this->title);
278
        }
279
280
        if ($this->description !== null) {
281
            $properties[] = new Property(Property::DESCRIPTION, $this->description);
282
        }
283
284
        if ($this->determiner !== null) {
285
            $properties[] = new Property(Property::DETERMINER, $this->determiner);
286
        }
287
288
        foreach ($this->images as $image) {
289
            $properties = array_merge($properties, $image->getProperties());
290
        }
291
292
        if ($this->locale !== null) {
293
            $properties[] = new Property(Property::LOCALE, $this->locale);
294
        }
295
296
        foreach ($this->localeAlternate as $locale) {
297
            $properties[] = new Property(Property::LOCALE_ALTERNATE, $locale);
298
        }
299
        
300
        if ($this->priceAmount !== null) {
301
            $properties[] = new Property(Property::PRICE_AMOUNT, $this->priceAmount);
302
        }
303
304
        if ($this->priceCurrency !== null) {
305
            $properties[] = new Property(Property::PRICE_CURRENCY, $this->priceCurrency);
306
        }
307
308
        if ($this->richAttachment !== null) {
309
            $properties[] = new Property(Property::RICH_ATTACHMENT, (int)$this->richAttachment);
310
        }
311
312
        foreach ($this->seeAlso as $seeAlso) {
313
            $properties[] = new Property(Property::SEE_ALSO, $seeAlso);
314
        }
315
316
        if ($this->siteName !== null) {
317
            $properties[] = new Property(Property::SITE_NAME, $this->siteName);
318
        }
319
320
        if ($this->type !== null) {
321
            $properties[] = new Property(Property::TYPE, $this->type);
322
        }
323
324
        if ($this->updatedTime !== null) {
325
            $properties[] = new Property(Property::UPDATED_TIME, $this->updatedTime->format('c'));
326
        }
327
328
        if ($this->url !== null) {
329
            $properties[] = new Property(Property::URL, $this->url);
330
        }
331
332
        foreach ($this->videos as $video) {
333
            $properties = array_merge($properties, $video->getProperties());
334
        }
335
336
        return $properties;
337
    }
338
}
339