Completed
Push — master ( d04535...94085d )
by Romain
12s queued 10s
created

MediaTemplate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 12
loc 57
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A create() 0 4 1
A toArray() 12 12 1
A isValidElements() 0 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template;
6
7
use Kerox\Messenger\Exception\InvalidClassException;
8
use Kerox\Messenger\Model\Message\Attachment\Template;
9
use Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement;
10
11
class MediaTemplate extends Template
12
{
13
    /**
14
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement[]
15
     */
16
    protected $elements;
17
18
    /**
19
     * MediaTemplate constructor.
20
     *
21
     * @throws \Kerox\Messenger\Exception\MessengerException
22
     */
23 1
    public function __construct(array $elements)
24
    {
25 1
        parent::__construct();
26
27 1
        $this->isValidElements($elements);
28
29 1
        $this->elements = $elements;
30 1
    }
31
32
    /**
33
     * @throws \Kerox\Messenger\Exception\MessengerException
34
     *
35
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\MediaTemplate
36
     */
37 1
    public static function create(array $elements): self
38
    {
39 1
        return new self($elements);
40
    }
41
42
    /**
43
     * @throws \Kerox\Messenger\Exception\MessengerException
44
     */
45 1
    private function isValidElements(array $elements): void
46
    {
47 1
        $this->isValidArray($elements, 1, 1);
48 1
        foreach ($elements as $element) {
49 1
            if (!$element instanceof MediaElement) {
50 1
                throw new InvalidClassException(sprintf('Array can only contain instance of %s.', MediaElement::class));
51
            }
52
        }
53 1
    }
54
55 1 View Code Duplication
    public function toArray(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57 1
        $array = parent::toArray();
58
        $array += [
59
            'payload' => [
60 1
                'template_type' => Template::TYPE_MEDIA,
61 1
                'elements' => $this->elements,
62
            ],
63
        ];
64
65 1
        return $this->arrayFilter($array);
66
    }
67
}
68