AuthenticationResource   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 82
dl 0
loc 176
rs 10
c 0
b 0
f 0
wmc 24

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
B setDetailsMessages() 0 28 9
B setDetails() 0 35 11
1
<?php
2
3
namespace DansMaCulotte\Monetico\Resources;
4
5
use DansMaCulotte\Monetico\Exceptions\AuthenticationException;
6
7
class AuthenticationResource
8
{
9
    /** @var array */
10
    public $details = [];
11
12
    /** @var string */
13
    public $protocol;
14
15
    /** @var string */
16
    public $version;
17
18
    /** @var string */
19
    public $status;
20
21
    /** @var array */
22
    const STATUSES = [
23
        'authenticated',
24
        'authentication_not_performed',
25
        'not_authenticated',
26
        'authentication_rejected',
27
        'authentication_attempted',
28
        'not_enrolled',
29
        'disabled',
30
    ];
31
32
    /** @var string */
33
    const PROTOCOL = '3DSecure';
34
35
    /** @var array */
36
    const VERSIONS = [
37
        '1.0.2',
38
        '2.1.0'
39
    ];
40
41
    /** @var array */
42
    const LIABILITY_SHIFTS = ['Y', 'N', 'NA'];
43
44
    /** @var array */
45
    const VERES_OPTIONS = ['Y', 'N', 'U'];
46
47
    /** @var array */
48
    const PARES_OPTIONS = ['Y', 'U', 'N', 'A'];
49
50
    /** @var array */
51
    const ARES_OPTIONS = ['Y', 'R', 'C', 'U', 'A', 'N'];
52
53
    /** @var array */
54
    const CRES_OPTIONS = ['Y', 'N'];
55
56
    /** @var array */
57
    const MERCHANT_PREFERENCES = [
58
        'no_preference',
59
        'challenge_preferred',
60
        'challenge_mandated',
61
        'no_challenge_requested',
62
        'no_challenge_requested_strong_authentication',
63
        'no_challenge_requested_trusted_third_party',
64
        'no_challenge_requested_risk_analysis'
65
    ];
66
67
    /** @var array  */
68
    const DDDS_STATUSES = [
69
        -1, 1, 4,
70
    ];
71
72
    /** @var array */
73
    const DISABLING_REASONS = [
74
        'commercant',
75
        'seuilnonatteint',
76
        'scoring'
77
    ];
78
79
    /**
80
     * Authentication constructor.
81
     *
82
     * @param string $protocol
83
     * @param string $status
84
     * @param string $version
85
     * @param array $details
86
     * @throws AuthenticationException
87
     */
88
    public function __construct(string $protocol, string $status, string $version, array $details)
89
    {
90
        $this->protocol = $protocol;
91
        if ($this->protocol !== self::PROTOCOL) {
92
            throw AuthenticationException::invalidProtocol($this->protocol);
93
        }
94
95
        $this->status = $status;
96
        if (!in_array($this->status, self::STATUSES, true)) {
97
            throw AuthenticationException::invalidStatus($this->status);
98
        }
99
100
        $this->version = $version;
101
        if (!in_array($this->version, self::VERSIONS, true)) {
102
            throw AuthenticationException::invalidVersion($this->version);
103
        }
104
105
        $this->setDetails($details);
106
        $this->setDetailsMessages($details);
107
    }
108
109
    /**
110
     * @param array $details
111
     * @throws AuthenticationException
112
     */
113
    public function setDetails(array $details)
114
    {
115
        if (isset($details['liabilityShift'])) {
116
            if (!in_array($details['liabilityShift'], self::LIABILITY_SHIFTS, true)) {
117
                throw AuthenticationException::invalidLiabilityShift($details['liabilityShift']);
118
            }
119
            $this->details['liabilityShift'] = $details['liabilityShift'];
120
        }
121
122
        if (isset($details['merchantPreference'])) {
123
            if (!in_array($details['merchantPreference'], self::MERCHANT_PREFERENCES, true)) {
124
                throw AuthenticationException::invalidMerchantPreference($details['merchantPreference']);
125
            }
126
            $this->details['merchantPreference'] = $details['merchantPreference'];
127
        }
128
129
        if (isset($details['transactionID'])) {
130
            $this->details['transactionID'] = $details['transactionID'];
131
        }
132
        if (isset($details['authenticationValue'])) {
133
            $this->details['authenticationValue'] = $details['authenticationValue'];
134
        }
135
136
        if (isset($details['status3DS'])) {
137
            if (!in_array($details['status3DS'], self::DDDS_STATUSES, true)) {
138
                throw AuthenticationException::invalidDDDSStatus($details['status3DS']);
139
            }
140
            $this->details['status3DS'] = $details['status3DS'];
141
        }
142
143
        if (isset($details['disablingReason'])) {
144
            if (!in_array($details['disablingReason'], self::DISABLING_REASONS, true)) {
145
                throw AuthenticationException::invalidDisablingReason($details['disablingReason']);
146
            }
147
            $this->details['disablingReason'] = $details['disablingReason'];
148
        }
149
    }
150
151
    /**
152
     * @param $details
153
     * @throws AuthenticationException
154
     */
155
    private function setDetailsMessages($details)
156
    {
157
        if (isset($details['VERes'])) {
158
            if (!in_array($details['VERes'], self::VERES_OPTIONS, true)) {
159
                throw AuthenticationException::invalidVERes($details['VERes']);
160
            }
161
            $this->details['VERes'] = $details['VERes'];
162
        }
163
164
        if (isset($details['PARes'])) {
165
            if (!in_array($details['PARes'], self::PARES_OPTIONS, true)) {
166
                throw AuthenticationException::invalidPARes($details['PARes']);
167
            }
168
            $this->details['PARes'] = $details['PARes'];
169
        }
170
171
        if (isset($details['ARes'])) {
172
            if (!in_array($details['ARes'], self::ARES_OPTIONS, true)) {
173
                throw AuthenticationException::invalidARes($details['ARes']);
174
            }
175
            $this->details['ARes'] = $details['ARes'];
176
        }
177
178
        if (isset($details['CRes'])) {
179
            if (!in_array($details['CRes'], self::CRES_OPTIONS, true)) {
180
                throw AuthenticationException::invalidCRes($details['CRes']);
181
            }
182
            $this->details['CRes'] = $details['CRes'];
183
        }
184
    }
185
}
186