Completed
Pull Request — master (#1)
by Romain
02:17
created

Liste   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 104
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setTopElementStyle() 0 7 1
A isValidTopElementStyle() 0 7 2
A getAllowedTopElementStyle() 0 7 1
A setButtons() 0 7 1
A jsonSerialize() 0 16 1
1
<?php
2
namespace Kerox\Messenger\Model\Message\Attachment\Template;
3
4
use Kerox\Messenger\Model\Message\Attachment\Template;
5
6
class Liste extends Template
7
{
8
9
    const TOP_ELEMENT_STYLE_LARGE = 'large';
10
    const TOP_ELEMENT_STYLE_COMPACT = 'compact';
11
12
    /**
13
     * @var string
14
     */
15
    protected $topElementStyle;
16
17
    /**
18
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Element\ListeElement[]
19
     */
20
    protected $elements = [];
21
22
    /**
23
     * @var \Kerox\Messenger\Model\Common\Buttons\AbstractButtons[]
24
     */
25
    protected $buttons = [];
26
27
    /**
28
     * Liste constructor.
29
     *
30
     * @param array $elements
31
     */
32
    public function __construct(array $elements)
33
    {
34
        parent::__construct();
35
36
        $this->isValidArray($elements, 4, 2);
37
38
        $this->elements = $elements;
39
    }
40
41
    /**
42
     * @param string $topElementStyle
43
     * @return Liste
44
     */
45
    public function setTopElementStyle(string $topElementStyle): Liste
46
    {
47
        $this->isValidTopElementStyle($topElementStyle);
48
        $this->topElementStyle = $topElementStyle;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @param string $topElementStyle
55
     * @return void
56
     * @throws \InvalidArgumentException
57
     */
58
    private function isValidTopElementStyle(string $topElementStyle)
59
    {
60
        $allowedTopElementStyle = $this->getAllowedTopElementStyle();
61
        if (!in_array($topElementStyle, $allowedTopElementStyle)) {
62
            throw new \InvalidArgumentException('$topElementStyle is not a valid');
63
        }
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    private function getAllowedTopElementStyle(): array
70
    {
71
        return [
72
            self::TOP_ELEMENT_STYLE_LARGE,
73
            self::TOP_ELEMENT_STYLE_COMPACT,
74
        ];
75
    }
76
77
    /**
78
     * @param \Kerox\Messenger\Model\Common\Buttons\AbstractButtons[] $buttons
79
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Liste
80
     * @internal param array $buttons
81
     */
82
    public function setButtons(array $buttons): Liste
83
    {
84
        $this->isValidArray($buttons, 1);
85
        $this->buttons = $buttons;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function jsonSerialize(): array
94
    {
95
        $payload = [
96
            'template_type' => Template::TYPE_LIST,
97
            'top_element_style' => $this->topElementStyle,
98
            'elements' => $this->elements,
99
            'buttons' => $this->buttons,
100
        ];
101
102
        $json = parent::jsonSerialize();
103
        $json += [
104
            'payload' => array_filter($payload),
105
        ];
106
107
        return $json;
108
    }
109
}
110