Passed
Push — master ( 195aeb...469104 )
by Romain
36s
created

PersistentMenu   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 100
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setComposerInputDisabled() 0 6 1
A addButtons() 0 9 1
A isValidButtons() 0 11 3
A getAllowedButtonsType() 0 8 1
A jsonSerialize() 0 10 1
1
<?php
2
namespace Kerox\Messenger\Model\ProfileSettings;
3
4
use Kerox\Messenger\Helper\ValidatorTrait;
5
use Kerox\Messenger\Model\Common\Buttons\AbstractButtons;
6
7
class PersistentMenu implements ProfileSettingsInterface, \JsonSerializable
8
{
9
10
    use ValidatorTrait;
11
12
    /**
13
     * @var string
14
     */
15
    protected $locale;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $composerInputDisabled = false;
21
22
    /**
23
     * @var \Kerox\Messenger\Model\Common\Buttons\AbstractButtons[]
24
     */
25
    protected $buttons = [];
26
27
    /**
28
     * PersistentMenu constructor.
29
     *
30
     * @param string $locale
31
     */
32 1
    public function __construct(string $locale = self::DEFAULT_LOCALE)
33
    {
34 1
        if ($locale !== self::DEFAULT_LOCALE) {
35 1
            $this->isValidLocale($locale);
36
        }
37
38 1
        $this->locale = $locale;
39 1
    }
40
41
    /**
42
     * @param bool $composerInputDisabled
43
     * @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu
44
     */
45 1
    public function setComposerInputDisabled(bool $composerInputDisabled): PersistentMenu
46
    {
47 1
        $this->composerInputDisabled = $composerInputDisabled;
48
49 1
        return $this;
50
    }
51
52
    /**
53
     * @param \Kerox\Messenger\Model\Common\Buttons\AbstractButtons[] $buttons
54
     * @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu
55
     */
56 1
    public function addButtons(array $buttons): PersistentMenu
57
    {
58 1
        $this->isValidArray($buttons, 5);
59 1
        $this->isValidButtons($buttons);
60
61 1
        $this->buttons = $buttons;
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * @param array $buttons
68
     */
69 1
    private function isValidButtons(array $buttons)
70
    {
71 1
        $allowedButtonsType = $this->getAllowedButtonsType();
72
73
        /** @var AbstractButtons $button */
74 1
        foreach ($buttons as $button) {
75 1
            if (!in_array($button->getType(), $allowedButtonsType)) {
76 1
                throw new \InvalidArgumentException('Buttons can only be an instance of WebUrl, PostBack or Nested');
77
            }
78
        }
79 1
    }
80
81
    /**
82
     * @return array
83
     */
84 1
    private function getAllowedButtonsType(): array
85
    {
86
        return [
87 1
            AbstractButtons::TYPE_WEB_URL,
88 1
            AbstractButtons::TYPE_POSTBACK,
89 1
            AbstractButtons::TYPE_NESTED,
90
        ];
91
    }
92
93
    /**
94
     * @return array
95
     */
96 1
    public function jsonSerialize(): array
97
    {
98
        $json = [
99 1
            'locale' => $this->locale,
100 1
            'composer_input_disabled' => $this->composerInputDisabled,
101 1
            'call_to_actions' => $this->buttons,
102
        ];
103
104 1
        return array_filter($json);
105
    }
106
}
107