Completed
Branch develop (8166a6)
by Romain
01:52
created

ThreadSettings::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
1
<?php
2
namespace Kerox\Messenger\Model;
3
4
use Kerox\Messenger\ValidatorTrait;
5
6
class ThreadSettings implements \JsonSerializable
7
{
8
9
    use ValidatorTrait;
10
11
    const TYPE_GREETING = 'greeting';
12
    const TYPE_CALL_TO_ACTIONS = 'call_to_actions';
13
    const TYPE_DOMAIN_WHITELISTING = 'domain_whitelisting';
14
    const TYPE_ACCOUNT_LINKING = 'account_linking';
15
    const TYPE_PAYMENT = 'payment';
16
17
    const STATE_EXISTING_THREAD = 'existing_thread';
18
    const STATE_NEW_THREAD = 'new_thread';
19
20
    /**
21
     * @var string
22
     */
23
    protected $type;
24
25
    /**
26
     * @var null|string
27
     */
28
    protected $state;
29
30
    /**
31
     * ThreadSettings constructor.
32
     *
33
     * @param string $type
34
     * @param string $state
35
     */
36
    public function __construct(string $type, string $state = null)
37
    {
38
        $this->type = $type;
39
40
        if ($state !== null && $type === self::TYPE_CALL_TO_ACTIONS) {
41
            $this->isValidState($state);
42
            $this->state = $state;
43
        }
44
    }
45
46
    /**
47
     * @param string $state
48
     * @throws \InvalidArgumentException
49
     */
50
    private function isValidState(string $state)
51
    {
52
        $allowedState = $this->getAllowedState();
53
        if (!in_array($state, $allowedState)) {
54
            throw new \InvalidArgumentException('$state must be either ' . implode(', '), $allowedState);
55
        }
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    private function getAllowedState(): array
62
    {
63
        return [
64
            ThreadSettings::STATE_NEW_THREAD,
65
            ThreadSettings::STATE_EXISTING_THREAD,
66
        ];
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function jsonSerialize(): array
73
    {
74
        $json = [
75
            'setting_type' => $this->type,
76
            'thread_state' => $this->state,
77
        ];
78
79
        return array_filter($json);
80
    }
81
}
82