Passed
Push — master ( f4ebb6...c31285 )
by Gaël
11:14
created

RecoveryResponse   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setAmounts() 0 7 3
A setDates() 0 13 5
B setOptions() 0 22 7
1
<?php
2
3
namespace DansMaCulotte\Monetico\Responses;
4
5
use DansMaCulotte\Monetico\Exceptions\Exception;
6
use DansMaCulotte\Monetico\Exceptions\RecoveryException;
7
use DateTime;
8
9
class RecoveryResponse extends AbstractResponse
10
{
11
    /** @var string */
12
    public $authorisationNumber;
13
14
    /** @var string */
15
    public $phone;
16
17
    /** @var float */
18
    public $estimatedAmount;
19
20
    /** @var \DateTime */
21
    public $authorisationDate;
22
23
    /** @var string */
24
    public $currency;
25
26
    /** @var float */
27
    public $amountDebited;
28
29
    /** @var \DateTime */
30
    public $debitDate;
31
32
    /** @var string */
33
    public $fileNumber;
34
35
    /** @var string */
36
    public $invoiceType;
37
38
    /** @var array  */
39
    const INVOICE_TYPES = [
40
        'preauto',
41
        'noshow',
42
    ];
43
44
    /** @var string */
45
    const DATE_FORMAT = 'Y-m-d';
46
47
    /**
48
     * RecoveryResponse constructor.
49
     *
50
     * @param array $data
51
     * @throws Exception
52
     * @throws RecoveryException
53
     */
54
    public function __construct(array $data = [])
55
    {
56
        parent::__construct($data);
57
58
        $this->setOptions($data);
59
        $this->setDates($data);
60
        $this->setAmounts($data);
61
    }
62
63
    /**
64
     * @param array $data
65
     * @throws RecoveryException
66
     */
67
    private function setDates($data): void
68
    {
69
        if (isset($data['date_autorisation'])) {
70
            $this->authorisationDate = DateTime::createFromFormat(self::DATE_FORMAT, $data['date_autorisation']);
0 ignored issues
show
Documentation Bug introduced by
It seems like DateTime::createFromForm...a['date_autorisation']) can also be of type false. However, the property $authorisationDate is declared as type DateTime. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
71
            if (!$this->authorisationDate) {
72
                throw RecoveryException::invalidResponseAuthorizationDate();
73
            }
74
        }
75
76
        if (isset($data['date_debit'])) {
77
            $this->debitDate = DateTime::createFromFormat(self::DATE_FORMAT, $data['date_debit']);
0 ignored issues
show
Documentation Bug introduced by
It seems like DateTime::createFromForm...T, $data['date_debit']) can also be of type false. However, the property $debitDate is declared as type DateTime. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
78
            if (!$this->authorisationDate) {
79
                throw RecoveryException::invalidResponseDebitDate();
80
            }
81
        }
82
    }
83
84
    /**
85
     * @param array $data
86
     * @throws Exception
87
     */
88
    public function setOptions(array $data): void
89
    {
90
        if (isset($data['aut'])) {
91
            $this->authorisationNumber = $data['aut'];
92
        }
93
94
        if (isset($data['numero_dossier'])) {
95
            $this->fileNumber = $data['numero_dossier'];
96
            if (strlen($this->fileNumber) > 12) {
97
                throw Exception::invalidResponseFileNumber($this->fileNumber);
98
            }
99
        }
100
101
        if (isset($data['type_facture'])) {
102
            $this->invoiceType = $data['type_facture'];
103
            if (!in_array($this->invoiceType, self::INVOICE_TYPES)) {
104
                throw Exception::invalidResponseInvoiceType($this->invoiceType);
105
            }
106
        }
107
108
        if (isset($data['phonie'])) {
109
            $this->phone = $data['phonie'];
110
        }
111
    }
112
113
    /**
114
     * @param array $data
115
     */
116
    private function setAmounts(array $data): void
117
    {
118
        if (isset($data['montant_estime'])) {
119
            $this->estimatedAmount = $data['montant_estime'];
120
        }
121
        if (isset($data['montant_debite'])) {
122
            $this->amountDebited = $data['montant_debite'];
123
        }
124
    }
125
}
126