GenericTemplate::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

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 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template;
6
7
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;
8
9
class GenericTemplate extends AbstractTemplate
10
{
11
    public const IMAGE_RATIO_HORIZONTAL = 'horizontal';
12
    public const IMAGE_RATIO_SQUARE = 'square';
13
14
    /**
15
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Element\GenericElement[]
16
     */
17
    protected $elements;
18
19
    /**
20
     * @var string
21
     */
22
    protected $imageRatio;
23
24
    /**
25
     * Generic constructor.
26
     *
27
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Element\GenericElement[] $elements
28
     *
29
     * @throws \Kerox\Messenger\Exception\MessengerException
30
     */
31 3
    public function __construct(array $elements, string $imageRatio = self::IMAGE_RATIO_HORIZONTAL)
32
    {
33 3
        parent::__construct();
34
35 3
        $this->isValidArray($elements, 10);
36
37 3
        $this->elements = $elements;
38 3
        $this->imageRatio = $imageRatio;
39 3
    }
40
41
    /**
42
     *@throws \Kerox\Messenger\Exception\MessengerException
43
     *
44
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\GenericTemplate
45
     */
46 3
    public static function create(array $elements, string $imageRatio = self::IMAGE_RATIO_HORIZONTAL): self
47
    {
48 3
        return new self($elements, $imageRatio);
49
    }
50
51 3
    public function toArray(): array
52
    {
53 3
        $array = parent::toArray();
54
        $array += [
55
            'payload' => [
56 3
                'template_type' => AbstractTemplate::TYPE_GENERIC,
57 3
                'elements' => $this->elements,
58 3
                'image_aspect_ratio' => $this->imageRatio,
59
            ],
60
        ];
61
62 3
        return $this->arrayFilter($array);
63
    }
64
}
65