Completed
Pull Request — master (#47)
by Romain
02:37
created

PersistentMenu::isValidButtons()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3

1 Method

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