Passed
Push — master ( 195aeb...469104 )
by Romain
36s
created

TargetAudience   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 125
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A addWhitelistCountry() 0 8 1
A addBlacklistCountry() 0 8 1
A isValidCountries() 0 8 3
A getAllowedAudienceType() 0 8 1
A jsonSerialize() 0 12 1
A isValidAudienceType() 0 7 2
1
<?php
2
namespace Kerox\Messenger\Model\ProfileSettings;
3
4
use Kerox\Messenger\Helper\UtilityTrait;
5
use Kerox\Messenger\Helper\ValidatorTrait;
6
7
class TargetAudience implements \JsonSerializable
8
{
9
10
    use UtilityTrait;
11
    use ValidatorTrait;
12
13
    const AUDIENCE_TYPE_ALL = 'all';
14
    const AUDIENCE_TYPE_CUSTOM = 'custom';
15
    const AUDIENCE_TYPE_NONE = 'none';
16
17
    /**
18
     * @var string
19
     */
20
    protected $audienceType;
21
22
    /**
23
     * @var array
24
     */
25
    protected $whitelistCountries;
26
27
    /**
28
     * @var array
29
     */
30
    protected $blacklistCountries;
31
32
    /**
33
     * TargetAudience constructor.
34
     *
35
     * @param string $audienceType
36
     * @param array $whitelistCountries
37
     * @param array $blacklistCountries
38
     */
39 2
    public function __construct(string $audienceType = self::AUDIENCE_TYPE_ALL, array $whitelistCountries = [], array $blacklistCountries = [])
40
    {
41 2
        $this->isValidAudienceType($audienceType);
42 1
        $this->isValidCountries($whitelistCountries);
43 1
        $this->isValidCountries($blacklistCountries);
44
45 1
        $this->audienceType = $audienceType;
46 1
        $this->whitelistCountries = $whitelistCountries;
47 1
        $this->blacklistCountries = $blacklistCountries;
48 1
    }
49
50
    /**
51
     * @param string $country
52
     * @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience
53
     * @throws \InvalidArgumentException
54
     */
55 1
    public function addWhitelistCountry(string $country): TargetAudience
56
    {
57 1
        $this->isValidCountry($country);
58
59 1
        $this->whitelistCountries[] = $country;
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * @param string $country
66
     * @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience
67
     * @throws \InvalidArgumentException
68
     */
69 1
    public function addBlacklistCountry(string $country): TargetAudience
70
    {
71 1
        $this->isValidCountry($country);
72
73 1
        $this->blacklistCountries[] = $country;
74
75 1
        return $this;
76
    }
77
78
    /**
79
     * @param array $countries
80
     * @return void
81
     * @throws \InvalidArgumentException
82
     */
83 1
    private function isValidCountries(array $countries)
84
    {
85 1
        if (!empty($countries)) {
86 1
            foreach ($countries as $country) {
87 1
                $this->isValidCountry($country);
88
            }
89
        }
90 1
    }
91
92
    /**
93
     * @param string $audienceType
94
     * @throws \InvalidArgumentException
95
     */
96 2
    private function isValidAudienceType(string $audienceType)
97
    {
98 2
        $allowedAudienceType = $this->getAllowedAudienceType();
99 2
        if (!in_array($audienceType, $allowedAudienceType)) {
100 1
            throw new \InvalidArgumentException('$audienceType must be either ' . implode(', ', $allowedAudienceType));
101
        }
102 1
    }
103
104
    /**
105
     * @return array
106
     */
107 2
    private function getAllowedAudienceType(): array
108
    {
109
        return [
110 2
            self::AUDIENCE_TYPE_ALL,
111 2
            self::AUDIENCE_TYPE_CUSTOM,
112 2
            self::AUDIENCE_TYPE_NONE,
113
        ];
114
    }
115
116
    /**
117
     * @return array
118
     */
119 1
    public function jsonSerialize(): array
120
    {
121
        $json = [
122 1
            'audience_type' => $this->audienceType,
123
            'countries' => [
124 1
                'whitelist' => $this->whitelistCountries,
125 1
                'blacklist' => $this->blacklistCountries,
126
            ]
127
        ];
128
129 1
        return $this->arrayFilter($json);
130
    }
131
}
132