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

MediaTemplate::isValidElements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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