Test Failed
Pull Request — master (#12)
by
unknown
02:05
created

Create::createResultObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Cardinity\Method\Payment;
4
5
use Cardinity\Method\MethodInterface;
6
use Symfony\Component\Validator\Constraints as Assert;
7
8
9
class Create implements MethodInterface
10
{
11
    const CARD = 'card';
12
    const RECURRING = 'recurring';
13
14
    private $attributes;
15
16
17
    public function __construct(array $attributes)
18
    {
19
        $this->attributes = $attributes;
20
    }
21
22
    public function getAction()
23
    {
24
        return 'payments';
25
    }
26
27
    public function getMethod()
28
    {
29
        return MethodInterface::POST;
30
    }
31
32
    public function getAttributes()
33
    {
34
        return $this->attributes;
35
    }
36
37
    public function createResultObject()
38
    {
39
        return new Payment();
40
    }
41
42
    public function getValidationConstraints()
43
    {
44
        return new Assert\Collection([
45
            'amount' =>  $this->buildElement('float', 1),
46
            'currency' => $this->buildElement('string', 1, ['min' => 3,'max' => 3]),
47
            'settle' => $this->buildElement('bool'),
48
            'order_id' => $this->buildElement('string', 0, ['min' => 2,'max' => 50]),
49
            'description' => $this->buildElement('string', 0, ['max' => 255]),
50
            'country' => $this->buildElement('string', 1, ['min' => 2,'max' => 2]),
51
            'payment_method' => new Assert\Required([
52
                new Assert\Type([
53
                    'type' => 'string',
54
                    'message' => 'The value {{ value }} is not a valid {{ type }}.'
55
                ]),
56
                new Assert\Choice([
57
                    'choices' => [
58
                        self::CARD,
59
                        self::RECURRING
60
                    ]
61
                ])
62
            ]),
63
            'payment_instrument' => $this->getPaymentInstrumentConstraints(
64
                $this->getAttributes()['payment_method']
65
            ),
66
            'threeds2_data' => new Assert\Optional(
67
                $this->getThreeDS2DataConstraints()
68
            )
69
        ]);
70
    }
71
72
    private function getPaymentInstrumentConstraints($method)
73
    {
74
        switch ($method) {
75
            case self::CARD:
76
                return new Assert\Collection([
77
                    'pan' => new Assert\Required([
78
                        new Assert\NotBlank(),
79
                        new Assert\Luhn()
80
                    ]),
81
                    'exp_year' => $this->buildElement(
82
                        'integer', 1, 
83
                        ['min' => 4,'max' => 4],
84
                        new Assert\Range(['min' => date('Y')]),
85
                    ),
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
86
                    'exp_month' => $this->buildElement('integer', 1),
87
                    'cvc' => $this->buildElement('string', 1, ['min' => 3, 'max' => 4]),
88
                    'holder' => $this->buildElement('string', 1, ['max' => 32]),
89
                ]);
90
            case self::RECURRING:
91
                return new Assert\Collection([
92
                    'payment_id' => $this->buildElement('string', 1),
93
                ]);
94
        }
95
96
        throw new \InvalidArgumentException(
97
            sprintf(
98
                'Payment instrument for payment method "%s" is not expected',
99
                $method
100
            )
101
        );
102
    }
103
104
    private function getThreeDS2DataConstraints()
105
    {
106
        return new Assert\Collection([
107
            'notification_url' => $this->buildElement('string', 1),
108
            'browser_info' => $this->getBrowserInfoConstraints(),
109
            'billing_address' => new Assert\Optional(
110
                $this->getAdressConstraints()
111
            ),
112
            'delivery_address' => new Assert\Optional(
113
                $this->getAdressConstraints()
114
            ),
115
            'cardholder_info' => new Assert\Optional(
116
                $this->getCardHolderInfoConstraints()
117
            ),
118
        ]);
119
    }
120
121
    private function getBrowserInfoConstraints()
122
    {
123
        return new Assert\Collection([
124
            'accept_header' => $this->buildElement('string', 1),
125
            'browser_language' => $this->buildElement('string', 1),
126
            'screen_width' => $this->buildElement('integer', 1),
127
            'screen_height' => $this->buildElement('integer', 1),
128
            'challenge_window_size' => $this->buildElement('string', 1),
129
            'user_agent' => $this->buildElement('string', 1),
130
            'color_depth' => $this->buildElement('integer', 1),
131
            'time_zone' => $this->buildElement('integer', 1),
132
            'ip_address' => new Assert\Optional($this->buildElement('string')),
133
            'javascript_enabled' => new Assert\Optional($this->buildElement('bool')),
134
            'java_enabled' => new Assert\Optional($this->buildElement('bool')),
135
        ]);
136
    }
137
138
    private function getAdressConstraints()
139
    {
140
        return new Assert\Collection([
141
            'address_line1' => $this->buildElement('string', 1, ['max'=>50]),
142
            'address_line2' => new Assert\Optional(
143
                $this->buildElement('string', 1, ['max'=>50])
144
            ),
145
            'address_line3' => new Assert\Optional(
146
                $this->buildElement('string', 0, ['max'=>50])
147
            ),
148
            'city' => $this->buildElement('string', 1, ['max'=>50]),
149
            'country' => $this->buildElement('string', 1, ['max'=>10]),
150
            'postal_code' => $this->buildElement('string', 1, ['max'=>16]),
151
            'state' => new Assert\Optional(
152
                $this->buildElement('string', 0, ['max'=>14])
153
            ),
154
        ]);
155
    }
156
157
    private function getCardHolderInfoConstraints()
158
    {
159
        return new Assert\Collection([
160
            'email_address' => new Assert\Optional(
161
                new Assert\Email(['mode'=>'loose'])
162
            ),
163
            'mobile_phone_number' => new Assert\Optional($this->buildElement('string')),
164
            'work_phone_number' => new Assert\Optional($this->buildElement('string')),
165
            'home_phone_number' => new Assert\Optional($this->buildElement('string')),
166
        ]); 
167
    }
168
169
    private function buildElement($typeValue, bool $isRequired=false, $length=0, $args=0)
170
    {
171
        $inside_array = [
172
            new Assert\Type([
173
                'type' => $typeValue,
174
                'message' => 'The value {{ value }} is not a valid {{ type }}.'
175
            ]),
176
        ];
177
        if ($isRequired) array_unshift($inside_array, new Assert\NotBlank());
178
        if ($length) array_push($inside_array, new Assert\Length($length));
179
        if ($args) array_push($inside_array, $args);
180
        
181
        return $isRequired 
182
            ? new Assert\Required($inside_array)
183
            : new Assert\Optional($inside_array)
184
        ;
185
    }
186
}
187