Mandate::accept()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\eCurring\Resource\Subscription;
6
7
use DateTimeImmutable;
8
use LauLamanApps\eCurring\Resource\Subscription\Exception\MandateAlreadyAcceptedException;
9
use LauLamanApps\eCurring\Resource\Subscription\Exception\MandateInvalidException;
10
11
final class Mandate
12
{
13
    /**
14
     * @var string
15
     *
16
     * The unique mandate code which is generated when creating a subscription.
17
     */
18
    private $code;
19
20
    /**
21
     * @var bool
22
     *
23
     * Indicates whether the mandate has been accepted.
24
     * A mandate can be accepted by the customer or automatically by using the API.
25
     */
26
    private $accepted;
27
28
    /**
29
     * @var DateTimeImmutable|null
30
     *
31
     * The date on which the mandate has been accepted.
32
     */
33
    private $acceptedDate;
34
35
    /**
36
     * @throws MandateInvalidException
37
     */
38 1
    public function __construct(
39
        string $code,
40
        ?bool $accepted = false,
41
        ?DateTimeImmutable $acceptedDate = null
42
    ) {
43 1
        $this->code = $code;
44 1
        $this->accepted = $accepted;
45 1
        $this->acceptedDate = $acceptedDate;
46
47 1
        $this->validate();
48 1
    }
49
50 1
    public function getCode(): string
51
    {
52 1
        return $this->code;
53
    }
54
55 1
    public function isAccepted(): ?bool
56
    {
57 1
        return $this->accepted;
58
    }
59
60 1
    public function getAcceptedDate(): ?DateTimeImmutable
61
    {
62 1
        return $this->acceptedDate;
63
    }
64
65
    /**
66
     * @throws MandateAlreadyAcceptedException
67
     */
68
    public function accept(DateTimeImmutable $acceptedDate): void
69
    {
70
        if ($this->accepted === true) {
71
            throw new MandateAlreadyAcceptedException(
72
                sprintf('Mandate already accepted on %s', $this->acceptedDate->format('Y-m-d H:i:s'))
0 ignored issues
show
Bug introduced by
The method format() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
                sprintf('Mandate already accepted on %s', $this->acceptedDate->/** @scrutinizer ignore-call */ format('Y-m-d H:i:s'))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
            );
74
        }
75
76
        $this->accepted = true;
77
        $this->acceptedDate = $acceptedDate;
78
    }
79
80
    /**
81
     * @throws MandateInvalidException
82
     */
83 1
    private function validate(): void
84
    {
85 1
        if ($this->accepted && $this->getAcceptedDate() === null) {
86
            throw new MandateInvalidException('No AcceptedDate provided although the mandate is accepted.');
87
        }
88 1
    }
89
}
90