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

DomainWhitelist::isValidDomains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Kerox\Messenger\Model\ThreadSettings;
3
4
use Kerox\Messenger\Model\ThreadSettings;
5
6
class DomainWhitelist extends ThreadSettings implements ActionTypeInterface
7
{
8
9
    /**
10
     * @var array
11
     */
12
    protected $whitelistedDomains;
13
14
    /**
15
     * @var string
16
     */
17
    protected $actionType;
18
19
    /**
20
     * DomainWhitelist constructor.
21
     *
22
     * @param array $whitelistedDomains
23
     * @param string $actionType
24
     */
25
    public function __construct(array $whitelistedDomains, $actionType = ActionTypeInterface::ADD)
26
    {
27
        parent::__construct(ThreadSettings::TYPE_DOMAIN_WHITELISTING);
28
29
        $this->isValidArray($whitelistedDomains, 10);
30
        $this->isValidDomains($whitelistedDomains);
31
        $this->isValidActionType($actionType);
32
33
        $this->whitelistedDomains = $whitelistedDomains;
34
        $this->actionType = $actionType;
35
    }
36
37
    /**
38
     * @param array $domains
39
     */
40
    private function isValidDomains(array $domains)
41
    {
42
        foreach ($domains as $domain) {
43
            $this->isValidUrl($domain);
44
        }
45
    }
46
47
    /**
48
     * @param $actionType
49
     * @throws \InvalidArgumentException
50
     */
51
    private function isValidActionType(string $actionType)
52
    {
53
        $allowedActionType = $this->getAllowedActionType();
54
        if (!in_array($actionType, $allowedActionType)) {
55
            throw new \InvalidArgumentException('$actionType must be either ' . implode(',', $allowedActionType));
56
        }
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    private function getAllowedActionType(): array
63
    {
64
        return [
65
            ActionTypeInterface::ADD,
66
            ActionTypeInterface::REMOVE,
67
        ];
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function jsonSerialize(): array
74
    {
75
        $json = parent::jsonSerialize();
76
        $json += [
77
            'whitelisted_domains' => $this->whitelistedDomains,
78
            'domain_action_type' => $this->actionType,
79
        ];
80
81
        return $json;
82
    }
83
}
84