Passed
Push — master ( fb865f...3ad45c )
by Romain
51s queued 14s
created

ProfileSettings::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
use Kerox\Messenger\Model\ProfileSettings\HomeUrl;
9
use Kerox\Messenger\Model\ProfileSettings\PaymentSettings;
10
use Kerox\Messenger\Model\ProfileSettings\TargetAudience;
11
12
class ProfileSettings implements \JsonSerializable
13
{
14
    use ValidatorTrait;
15
16
    /**
17
     * @var array|null
18
     */
19
    protected $startButton;
20
21
    /**
22
     * @var \Kerox\Messenger\Model\ProfileSettings\Greeting[]|null
23
     */
24
    protected $greetings;
25
26
    /**
27
     * @var \Kerox\Messenger\Model\ProfileSettings\IceBreakers[]|null
28
     */
29
    protected $iceBreakers;
30
31
    /**
32
     * @var \Kerox\Messenger\Model\ProfileSettings\PersistentMenu[]|null
33
     */
34
    protected $persistentMenus;
35
36
    /**
37
     * @var array|null
38
     */
39
    protected $whitelistedDomains;
40
41
    /**
42
     * @var string|null
43
     */
44
    protected $accountLinkingUrl;
45
46
    /**
47
     * @var \Kerox\Messenger\Model\ProfileSettings\PaymentSettings|null
48
     */
49
    protected $paymentSettings;
50
51
    /**
52
     * @var \Kerox\Messenger\Model\ProfileSettings\HomeUrl|null
53
     */
54
    protected $homeUrl;
55
56
    /**
57
     * @var \Kerox\Messenger\Model\ProfileSettings\TargetAudience|null
58
     */
59
    protected $targetAudience;
60
61
    /**
62
     * @return \Kerox\Messenger\Model\ProfileSettings
63
     */
64 9
    public static function create(): self
65
    {
66 9
        return new self();
67
    }
68
69
    /**
70
     * @param \Kerox\Messenger\Model\ProfileSettings\PersistentMenu[] $persistentMenus
71
     *
72
     * @return \Kerox\Messenger\Model\ProfileSettings
73
     */
74 1
    public function addPersistentMenus(array $persistentMenus): self
75
    {
76 1
        $this->persistentMenus = $persistentMenus;
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * @throws \Exception
83
     *
84
     * @return \Kerox\Messenger\Model\ProfileSettings
85
     */
86 1
    public function addStartButton(string $payload): self
87
    {
88 1
        $this->isValidString($payload, 1000);
89
90 1
        $this->startButton = [
91 1
            'payload' => $payload,
92
        ];
93
94 1
        return $this;
95
    }
96
97
    /**
98
     * @param \Kerox\Messenger\Model\ProfileSettings\Greeting[] $greetings
99
     *
100
     * @return \Kerox\Messenger\Model\ProfileSettings
101
     */
102 2
    public function addGreetings(array $greetings): self
103
    {
104 2
        $this->greetings = $greetings;
105
106 2
        return $this;
107
    }
108
109
    /**
110
     * @param \Kerox\Messenger\Model\ProfileSettings\IceBreakers[] $iceBreakers
111
     *
112
     * @return \Kerox\Messenger\Model\ProfileSettings
113
     */
114 1
    public function addIceBreakers(array $iceBreakers): self
115
    {
116 1
        $this->iceBreakers = $iceBreakers;
117
118 1
        return $this;
119
    }
120
121
    /**
122
     * @throws \Exception
123
     *
124
     * @return \Kerox\Messenger\Model\ProfileSettings
125
     */
126 1
    public function addWhitelistedDomains(array $whitelistedDomains): self
127
    {
128 1
        $this->isValidDomains($whitelistedDomains);
129
130 1
        $this->whitelistedDomains = $whitelistedDomains;
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * @throws \Exception
137
     *
138
     * @return \Kerox\Messenger\Model\ProfileSettings
139
     */
140 1
    public function addAccountLinkingUrl(string $accountLinkingUrl): self
141
    {
142 1
        $this->isValidUrl($accountLinkingUrl);
143
144 1
        $this->accountLinkingUrl = $accountLinkingUrl;
145
146 1
        return $this;
147
    }
148
149
    /**
150
     * @deprecated Since version 3.3.0 and will be removed in version 4.0.0.
151
     *
152
     * @return \Kerox\Messenger\Model\ProfileSettings
153
     */
154 1
    public function addPaymentSettings(PaymentSettings $paymentSettings): self
155
    {
156 1
        $this->paymentSettings = $paymentSettings;
157
158 1
        return $this;
159
    }
160
161
    /**
162
     * @return \Kerox\Messenger\Model\ProfileSettings
163
     */
164 1
    public function addHomeUrl(HomeUrl $homeUrl): self
165
    {
166 1
        $this->homeUrl = $homeUrl;
167
168 1
        return $this;
169
    }
170
171
    /**
172
     * @deprecated Since version 3.3.0 and will be removed in version 4.0.0.
173
     *
174
     * @return \Kerox\Messenger\Model\ProfileSettings
175
     */
176 1
    public function addTargetAudience(TargetAudience $targetAudience): self
177
    {
178 1
        $this->targetAudience = $targetAudience;
179
180 1
        return $this;
181
    }
182
183
    /**
184
     * @throws \Exception
185
     */
186 1
    private function isValidDomains(array $domains): void
187
    {
188 1
        $this->isValidArray($domains, 50);
189
190 1
        foreach ($domains as $domain) {
191 1
            $this->isValidUrl($domain);
192
        }
193 1
    }
194
195 10
    public function toArray(): array
196
    {
197
        $array = [
198 10
            'get_started' => $this->startButton,
199 10
            'greeting' => $this->greetings,
200 10
            'ice_breakers' => $this->iceBreakers,
201 10
            'persistent_menu' => $this->persistentMenus,
202 10
            'payment_settings' => $this->paymentSettings,
203 10
            'whitelisted_domains' => $this->whitelistedDomains,
204 10
            'account_linking_url' => $this->accountLinkingUrl,
205 10
            'home_url' => $this->homeUrl,
206 10
            'target_audience' => $this->targetAudience,
207
        ];
208
209 10
        return array_filter($array);
210
    }
211
212 10
    public function jsonSerialize(): array
213
    {
214 10
        return $this->toArray();
215
    }
216
}
217