Completed
Pull Request — master (#10)
by Laurens
02:51
created

NotificationSettings::getPostData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\API\Inventory\Settings;
6
7
use LauLamanApps\IzettleApi\API\Inventory\Settings\Exception\EmailAddressNotValidException;
8
use LauLamanApps\IzettleApi\API\Universal\iZettlePostable;
9
10
final class NotificationSettings implements iZettlePostable
11
{
12
    private $emailReceivers;
13
    private $notifyBelowBalance;
14
    private $interval;
15
16
    public function new(array $emailReceivers, int $notifyBelowBalance, Interval $interval): self
17
    {
18
        return new self($emailReceivers, $notifyBelowBalance, $interval);
19
    }
20
21
    public function create(array $emailReceivers, int $notifyBelowBalance, Interval $interval): self
22
    {
23
        return new self($emailReceivers, $notifyBelowBalance, $interval);
24
    }
25
26
    public function getEmailReceivers(): array
27
    {
28
        return $this->emailReceivers;
29
    }
30
31
    public function getNotifyBelowBalance(): int
32
    {
33
        return $this->notifyBelowBalance;
34
    }
35
36
    public function getInterval(): Interval
37
    {
38
        return $this->interval;
39
    }
40
41
    public function getPostData(): array
42
    {
43
        return [
44
            "emailReceivers" => $this->emailReceivers,
45
            "notifyBelowBalance"=> $this->notifyBelowBalance,
46
            "interval" => $this->interval->getValue()
47
        ];
48
    }
49
50
    private function __construct(array $emailReceivers, int $notifyBelowBalance, Interval $interval)
51
    {
52
        $this->emailReceivers = $emailReceivers;
53
        $this->notifyBelowBalance = $notifyBelowBalance;
54
        $this->interval = $interval;
55
        $this->validateEmailAddresses();
56
    }
57
58
    private function validateEmailAddresses(): void
59
    {
60
        foreach ($this->emailReceivers as $emailReceiver)
61
        {
62
            $this->validateEmailAddress($emailReceiver);
63
        }
64
    }
65
66
    private function validateEmailAddress($emailReceiver): void
67
    {
68
        if (!filter_var($emailReceiver, FILTER_VALIDATE_EMAIL)) {
69
         throw new EmailAddressNotValidException(sprintf('\'%s\' is not a valid email address', $emailReceiver));
70
        }
71
    }
72
}
73