Completed
Pull Request — develop (#8)
by
unknown
07:18
created

OgSeoGenerator   F

Complexity

Total Complexity 61

Size/Duplication

Total Lines 503
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 61
lcom 1
cbo 9
dl 0
loc 503
rs 3.52
c 0
b 0
f 0

47 Methods

Rating   Name   Duplication   Size   Complexity  
A setType() 0 4 1
A getType() 0 4 1
A setSiteName() 0 4 1
A getSiteName() 0 4 1
A setTitle() 0 4 1
A getTitle() 0 4 1
A setImage() 0 4 1
A getImage() 0 4 1
A setImageType() 0 4 1
A getImageType() 0 4 1
A setImageAlt() 0 4 1
A getImageAlt() 0 4 1
A setImageWidth() 0 4 1
A getImageWidth() 0 4 1
A setImageHeight() 0 4 1
A getImageHeight() 0 4 1
A setImageSecureUrl() 0 4 1
A getImageSecureUrl() 0 4 1
A setVideo() 0 4 1
A getVideo() 0 4 1
A setVideoType() 0 4 1
A getVideoType() 0 4 1
A setVideoWidth() 0 4 1
A getVideoWidth() 0 4 1
A setVideoHeight() 0 4 1
A getVideoHeight() 0 4 1
A setVideoSecureUrl() 0 4 1
A getVideoSecureUrl() 0 4 1
A setAudio() 0 4 1
A getAudio() 0 4 1
A setAudioType() 0 4 1
A getAudioType() 0 4 1
A setAudioSecureUrl() 0 4 1
A getAudioSecureUrl() 0 4 1
A setDescription() 0 4 1
A getDescription() 0 4 1
A setLocale() 0 4 1
A getLocale() 0 4 1
A setAlternateLocales() 0 4 1
A getAlternateLocales() 0 4 1
A setDeterminer() 0 4 1
A getDeterminer() 0 4 1
A setUrl() 0 4 1
A getUrl() 0 4 1
F fromResource() 0 72 15
A get() 0 4 1
A set() 0 9 1

How to fix   Complexity   

Complex Class

Complex classes like OgSeoGenerator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 OgSeoGenerator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Leogout\Bundle\SeoBundle\Seo\Og;
4
5
use Leogout\Bundle\SeoBundle\Model\MetaTag;
6
use Leogout\Bundle\SeoBundle\Seo\AbstractSeoGenerator;
7
use Leogout\Bundle\SeoBundle\Seo\Stdlib\AudioAggregateInterface;
8
use Leogout\Bundle\SeoBundle\Seo\Stdlib\AudioInterface;
9
use Leogout\Bundle\SeoBundle\Seo\Stdlib\ImageAggregateInterface;
10
use Leogout\Bundle\SeoBundle\Seo\Stdlib\ImageInterface;
11
use Leogout\Bundle\SeoBundle\Seo\Stdlib\LocalizationAwareInterface;
12
use Leogout\Bundle\SeoBundle\Seo\Stdlib\ResourceInterface;
13
use Leogout\Bundle\SeoBundle\Seo\Stdlib\VideoAggregateInterface;
14
use Leogout\Bundle\SeoBundle\Seo\Stdlib\WebsiteInterface;
15
use Leogout\Bundle\SeoBundle\Seo\TitleSeoInterface;
16
use Leogout\Bundle\SeoBundle\Seo\DescriptionSeoInterface;
17
use Leogout\Bundle\SeoBundle\Seo\ImageSeoInterface;
18
use Leogout\Bundle\SeoBundle\Seo\Stdlib\VideoInterface;
19
20
/**
21
 * Description of OgSeoGenerator.
22
 *
23
 * @author: leogout
24
 */
25
class OgSeoGenerator extends AbstractSeoGenerator
26
{
27
    /**
28
     * @param string $content
29
     *
30
     * @return $this
31
     */
32
    public function setType($content)
33
    {
34
        return $this->set('og:type', $content);
35
    }
36
37
    /**
38
     * @return MetaTag
39
     */
40
    public function getType()
41
    {
42
        return $this->get('og:type');
43
    }
44
    /**
45
     * @param string $content
46
     *
47
     * @return $this
48
     */
49
    public function setSiteName($content)
50
    {
51
        return $this->set('og:site_name', $content);
52
    }
53
54
    /**
55
     * @return MetaTag
56
     */
57
    public function getSiteName()
58
    {
59
        return $this->get('og:site_name');
60
    }
61
62
    /**
63
     * @param string $content
64
     *
65
     * @return $this
66
     */
67
    public function setTitle($content)
68
    {
69
        return $this->set('og:title', $content);
70
    }
71
72
    /**
73
     * @return MetaTag
74
     */
75
    public function getTitle()
76
    {
77
        return $this->get('og:title');
78
    }
79
80
    /**
81
     * @param string $content
82
     *
83
     * @return $this
84
     */
85
    public function setImage($content)
86
    {
87
        return $this->set('og:image', $content);
88
    }
89
90
    /**
91
     * @return MetaTag
92
     */
93
    public function getImage()
94
    {
95
        return $this->get('og:image');
96
    }
97
98
    /**
99
     * @param string $content
100
     *
101
     * @return $this
102
     */
103
    public function setImageType($content)
104
    {
105
        return $this->set('og:image:type', $content);
106
    }
107
108
    /**
109
     * @return MetaTag
110
     */
111
    public function getImageType()
112
    {
113
        return $this->get('og:image:type');
114
    }
115
116
    /**
117
     * @param string $content
118
     *
119
     * @return $this
120
     */
121
    public function setImageAlt($content)
122
    {
123
        return $this->set('og:image:alt', $content);
124
    }
125
126
    /**
127
     * @return MetaTag
128
     */
129
    public function getImageAlt()
130
    {
131
        return $this->get('og:image:alt');
132
    }
133
134
    /**
135
     * @param int $content
136
     *
137
     * @return $this
138
     */
139
    public function setImageWidth($content)
140
    {
141
        return $this->set('og:image:width', $content);
142
    }
143
144
    /**
145
     * @return MetaTag
146
     */
147
    public function getImageWidth()
148
    {
149
        return $this->get('og:image:width');
150
    }
151
152
    /**
153
     * @param int $content
154
     *
155
     * @return $this
156
     */
157
    public function setImageHeight($content)
158
    {
159
        return $this->set('og:image:height', $content);
160
    }
161
162
    /**
163
     * @return MetaTag
164
     */
165
    public function getImageHeight()
166
    {
167
        return $this->get('og:image:height');
168
    }
169
170
    /**
171
     * @param string $content
172
     *
173
     * @return $this
174
     */
175
    public function setImageSecureUrl($content)
176
    {
177
        return $this->set('og:image:secure_url', $content);
178
    }
179
180
    /**
181
     * @return MetaTag
182
     */
183
    public function getImageSecureUrl()
184
    {
185
        return $this->get('og:image:secure_url');
186
    }
187
188
    /**
189
     * @param string $content
190
     *
191
     * @return $this
192
     */
193
    public function setVideo($content)
194
    {
195
        return $this->set('og:video', $content);
196
    }
197
198
    /**
199
     * @return MetaTag
200
     */
201
    public function getVideo()
202
    {
203
        return $this->get('og:video');
204
    }
205
206
    /**
207
     * @param string $content
208
     *
209
     * @return $this
210
     */
211
    public function setVideoType($content)
212
    {
213
        return $this->set('og:video:type', $content);
214
    }
215
216
    /**
217
     * @return MetaTag
218
     */
219
    public function getVideoType()
220
    {
221
        return $this->get('og:video:type');
222
    }
223
224
    /**
225
     * @param int $content
226
     *
227
     * @return $this
228
     */
229
    public function setVideoWidth($content)
230
    {
231
        return $this->set('og:video:width', $content);
232
    }
233
234
    /**
235
     * @return MetaTag
236
     */
237
    public function getVideoWidth()
238
    {
239
        return $this->get('og:video:width');
240
    }
241
242
    /**
243
     * @param int $content
244
     *
245
     * @return $this
246
     */
247
    public function setVideoHeight($content)
248
    {
249
        return $this->set('og:video:height', $content);
250
    }
251
252
    /**
253
     * @return MetaTag
254
     */
255
    public function getVideoHeight()
256
    {
257
        return $this->get('og:video:height');
258
    }
259
260
    /**
261
     * @param string $content
262
     *
263
     * @return $this
264
     */
265
    public function setVideoSecureUrl($content)
266
    {
267
        return $this->set('og:video:secure_url', $content);
268
    }
269
270
    /**
271
     * @return MetaTag
272
     */
273
    public function getVideoSecureUrl()
274
    {
275
        return $this->get('og:video:secure_url');
276
    }
277
278
    /**
279
     * @param string $content
280
     *
281
     * @return $this
282
     */
283
    public function setAudio($content)
284
    {
285
        return $this->set('og:audio', $content);
286
    }
287
288
    /**
289
     * @return MetaTag
290
     */
291
    public function getAudio()
292
    {
293
        return $this->get('og:audio');
294
    }
295
296
    /**
297
     * @param string $content
298
     *
299
     * @return $this
300
     */
301
    public function setAudioType($content)
302
    {
303
        return $this->set('og:audio:type', $content);
304
    }
305
306
    /**
307
     * @return MetaTag
308
     */
309
    public function getAudioType()
310
    {
311
        return $this->get('og:audio:type');
312
    }
313
314
    /**
315
     * @param string $content
316
     *
317
     * @return $this
318
     */
319
    public function setAudioSecureUrl($content)
320
    {
321
        return $this->set('og:audio:secure_url', $content);
322
    }
323
324
    /**
325
     * @return MetaTag
326
     */
327
    public function getAudioSecureUrl()
328
    {
329
        return $this->get('og:audio:secure_url');
330
    }
331
332
    /**
333
     * @param string $content
334
     *
335
     * @return $this
336
     */
337
    public function setDescription($content)
338
    {
339
        return $this->set('og:description', $content);
340
    }
341
342
    /**
343
     * @return MetaTag
344
     */
345
    public function getDescription()
346
    {
347
        return $this->get('og:description');
348
    }
349
    
350
    /**
351
     * @param string $content
352
     *
353
     * @return $this
354
     */
355
    public function setLocale($content)
356
    {
357
        return $this->set('og:locale', $content);
358
    }
359
360
    /**
361
     * @return MetaTag
362
     */
363
    public function getLocale()
364
    {
365
        return $this->get('og:locale');
366
    }
367
368
    /**
369
     * @param string[] $locales
370
     *
371
     * @return $this
372
     */
373
    public function setAlternateLocales($locales)
374
    {
375
        return $this->set('og:locale:alternate', $locales);
0 ignored issues
show
Documentation introduced by
$locales is of type array<integer,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
376
    }
377
378
    /**
379
     * @return MetaTag
380
     */
381
    public function getAlternateLocales()
382
    {
383
        return $this->get('og:locale:alternate');
384
    }
385
386
    /**
387
     * @param string $content
388
     *
389
     * @return $this
390
     */
391
    public function setDeterminer($content)
392
    {
393
        return $this->set('og:determiner', $content);
394
    }
395
396
    /**
397
     * @return MetaTag
398
     */
399
    public function getDeterminer()
400
    {
401
        return $this->get('og:determiner');
402
    }
403
404
    /**
405
     * @param string $content
406
     *
407
     * @return $this
408
     */
409
    public function setUrl($content)
410
    {
411
        return $this->set('og:url', $content);
412
    }
413
414
    /**
415
     * @return MetaTag
416
     */
417
    public function getUrl()
418
    {
419
        return $this->get('og:url');
420
    }
421
422
    /**
423
     * Generate seo tags from given resource.
424
     *
425
     * @param TitleSeoInterface|DescriptionSeoInterface|ImageSeoInterface $resource
426
     *
427
     * @return $this
428
     */
429
    public function fromResource($resource)
430
    {
431
        if ($resource instanceof TitleSeoInterface) {
432
            $this->setTitle($resource->getSeoTitle());
433
        }
434
        if ($resource instanceof DescriptionSeoInterface) {
435
            $this->setDescription($resource->getSeoDescription());
436
        }
437
        if ($resource instanceof ImageSeoInterface) {
438
            $this->setImage($resource->getSeoImage());
439
        }
440
441
        // Website
442
        if ($resource instanceof WebsiteInterface) {
443
            $this->setSiteName($resource->getSiteName());
444
        }
445
446
        // Resource
447
        if ($resource instanceof ResourceInterface) {
448
            $this->setTitle($resource->getTitle());
449
            $this->setDescription($resource->getDescription());
450
        }
451
452
        // Video
453
        $video = $resource;
454
        if ($video instanceof VideoAggregateInterface) {
455
            $video = $video->getVideo();
456
        }
457
        if ($video instanceof VideoInterface) {
458
            $this->setVideo($video->getUrl());
459
            $this->setVideoSecureUrl($video->getSecureUrl());
460
            $this->setVideoType($video->getMimeType());
461
            $this->setVideoWidth($video->getWidth());
462
            $this->setVideoHeight($video->getHeight());
463
        }
464
465
        // Image
466
        $image = $resource;
467
        if ($image instanceof ImageAggregateInterface) {
468
            $image = $image->getImage();
469
        }
470
        if ($image instanceof ImageInterface) {
471
            // new image interface
472
            $this->setImage($image->getUrl());
473
            $this->setImageAlt($image->getImageAlt());
474
            $this->setImageType($image->getMimeType());
475
            $this->setImageWidth($image->getWidth());
476
            $this->setImageHeight($image->getHeight());
477
            $this->setImageSecureUrl($image->getSecureUrl());
478
        }
479
480
        // Audio
481
        $audio = $resource;
482
        if ($audio instanceof AudioAggregateInterface) {
483
            $audio = $audio->getAudio();
484
        }
485
        if ($audio instanceof AudioInterface) {
486
            $this->setAudio($audio->getUrl());
487
            $this->setAudioType($audio->getMimeType());
488
            $this->setAudioSecureUrl($audio->getSecureUrl());
489
        }
490
491
        // Locales
492
        if ($resource instanceof LocalizationAwareInterface) {
493
            $this->setLocale($resource->getLocale());
494
            if ($locales = $resource->getAlternateLocales()) {
495
                $this->setAlternateLocales((is_array($locales)) ? $locales : [$locales]);
496
            }
497
        }
498
499
        return $this;
500
    }
501
502
    /**
503
     * @param string $type
504
     *
505
     * @return MetaTag
506
     */
507
    public function get($type)
508
    {
509
        return $this->tagBuilder->getMeta($type);
510
    }
511
512
    /**
513
     * @param string $type
514
     * @param string $value
515
     *
516
     * @return $this
517
     */
518
    public function set($type, $value)
519
    {
520
        $this->tagBuilder->addMeta($type)
521
            ->setType(MetaTag::NAME_TYPE)
522
            ->setTagName($type)
523
            ->setContent($value);
524
525
        return $this;
526
    }
527
}
528