Completed
Push — master ( eb575e...c103e9 )
by Romain
13s
created

TargetAudience::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\ProfileSettings;
6
7
use Kerox\Messenger\Helper\UtilityTrait;
8
use Kerox\Messenger\Helper\ValidatorTrait;
9
10
class TargetAudience implements \JsonSerializable
11
{
12
    use UtilityTrait;
13
    use ValidatorTrait;
14
15
    private const AUDIENCE_TYPE_ALL = 'all';
16
    private const AUDIENCE_TYPE_CUSTOM = 'custom';
17
    private const AUDIENCE_TYPE_NONE = 'none';
18
19
    /**
20
     * @var string
21
     */
22
    protected $audienceType;
23
24
    /**
25
     * @var array
26
     */
27
    protected $whitelistCountries;
28
29
    /**
30
     * @var array
31
     */
32
    protected $blacklistCountries;
33
34
    /**
35
     * TargetAudience constructor.
36
     *
37
     * @param string $audienceType
38
     * @param array  $whitelistCountries
39 2
     * @param array  $blacklistCountries
40
     *
41
     * @throws \InvalidArgumentException
42
     */
43
    public function __construct(
44 2
        string $audienceType = self::AUDIENCE_TYPE_ALL,
45 1
        array $whitelistCountries = [],
46 1
        array $blacklistCountries = []
47
    ) {
48 1
        $this->isValidAudienceType($audienceType);
49 1
        $this->isValidCountries($whitelistCountries);
50 1
        $this->isValidCountries($blacklistCountries);
51 1
52
        $this->audienceType = $audienceType;
53
        $this->whitelistCountries = $whitelistCountries;
54
        $this->blacklistCountries = $blacklistCountries;
55
    }
56
57
    /**
58
     * @param string $audienceType
59
     * @param array  $whitelistCountries
60 1
     * @param array  $blacklistCountries
61
     *
62 1
     * @throws \InvalidArgumentException
63
     *
64 1
     * @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience
65
     */
66 1
    public static function create(
67
        string $audienceType = self::AUDIENCE_TYPE_ALL,
68
        array $whitelistCountries = [],
69
        array $blacklistCountries = []
70
    ): self {
71
        return new self($audienceType, $whitelistCountries, $blacklistCountries);
72
    }
73
74
    /**
75
     * @param string $country
76 1
     *
77
     * @throws \InvalidArgumentException
78 1
     *
79
     * @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience
80 1
     */
81
    public function addWhitelistCountry(string $country): self
82 1
    {
83
        $this->isValidCountry($country);
84
85
        $this->whitelistCountries[] = $country;
86
87
        return $this;
88
    }
89
90 1
    /**
91
     * @param string $country
92 1
     *
93 1
     * @throws \InvalidArgumentException
94 1
     *
95
     * @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience
96
     */
97 1
    public function addBlacklistCountry(string $country): self
98
    {
99
        $this->isValidCountry($country);
100
101
        $this->blacklistCountries[] = $country;
102
103
        return $this;
104 2
    }
105
106 2
    /**
107 2
     * @param array $countries
108 1
     *
109
     * @throws \InvalidArgumentException
110 1
     */
111
    private function isValidCountries(array $countries): void
112
    {
113
        if (!empty($countries)) {
114
            foreach ($countries as $country) {
115 2
                $this->isValidCountry($country);
116
            }
117
        }
118 2
    }
119 2
120 2
    /**
121
     * @param string $audienceType
122
     *
123
     * @throws \InvalidArgumentException
124
     */
125
    private function isValidAudienceType(string $audienceType): void
126
    {
127 1
        $allowedAudienceType = $this->getAllowedAudienceType();
128
        if (!\in_array($audienceType, $allowedAudienceType, true)) {
129
            throw new \InvalidArgumentException('$audienceType must be either ' . implode(', ', $allowedAudienceType));
130 1
        }
131
    }
132 1
133 1
    /**
134
     * @return array
135
     */
136
    private function getAllowedAudienceType(): array
137 1
    {
138
        return [
139
            self::AUDIENCE_TYPE_ALL,
140
            self::AUDIENCE_TYPE_CUSTOM,
141
            self::AUDIENCE_TYPE_NONE,
142
        ];
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function toArray(): array
149
    {
150
        $array = [
151
            'audience_type' => $this->audienceType,
152
            'countries'     => [
153
                'whitelist' => $this->whitelistCountries,
154
                'blacklist' => $this->blacklistCountries,
155
            ],
156
        ];
157
158
        return $this->arrayFilter($array);
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function jsonSerialize(): array
165
    {
166
        return $this->toArray();
167
    }
168
}
169