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

DomainWhitelist   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A isValidDomains() 0 6 2
A isValidActionType() 0 7 2
A getAllowedActionType() 0 7 1
A jsonSerialize() 0 10 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