Test Failed
Pull Request — master (#10)
by Laurens
03:11 queued 10s
created

NotificationSettings   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 63
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A new() 0 4 1
A create() 0 4 1
A getEmailReceivers() 0 4 1
A getNotifyBelowBalance() 0 4 1
A getInterval() 0 4 1
A getPostData() 0 8 1
A __construct() 0 7 1
A validateEmailAddresses() 0 7 2
A validateEmailAddress() 0 6 2
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
0 ignored issues
show
Bug introduced by
There is one abstract method getPostBodyData in this class; you could implement it, or declare this class as abstract.
Loading history...
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