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

Create::getPaymentInstrumentConstraints()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 56
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 9.7251
c 0
b 0
f 0
cc 3
eloc 38
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cardinity\Method\Payment;
4
5
use Cardinity\Method\MethodInterface;
6
use Symfony\Component\Validator\Constraints as Assert;
7
8
class Create implements MethodInterface
9
{
10
    const CARD = 'card';
11
    const RECURRING = 'recurring';
12
13
    private $attributes;
14
15
    public function __construct(array $attributes)
16
    {
17
        $this->attributes = $attributes;
18
    }
19
20
    public function getAction()
21
    {
22
        return 'payments';
23
    }
24
25
    public function getMethod()
26
    {
27
        return MethodInterface::POST;
28
    }
29
30
    public function getAttributes()
31
    {
32
        return $this->attributes;
33
    }
34
35
    public function createResultObject()
36
    {
37
        return new Payment();
38
    }
39
40
    public function getValidationConstraints()
41
    {
42
        return new Assert\Collection([
43
            'amount' =>  $this->buildElement('float', 1),
44
            'currency' => $this->buildElement('string', 1, ['min' => 3,'max' => 3]),
45
            'settle' => $this->buildElement('bool'),
46
            'order_id' => $this->buildElement('string', 0, ['min' => 2,'max' => 50]),
47
            'description' => $this->buildElement('string', 0, ['max' => 255]),
48
            'country' => $this->buildElement('string', 1, ['min' => 2,'max' => 2]),
49
            'payment_method' => new Assert\Required([
50
                new Assert\Type(['type' => 'string']),
51
                new Assert\Choice([
52
                    'choices' => [
53
                        self::CARD,
54
                        self::RECURRING
55
                    ]
56
                ])
57
            ]),
58
            'payment_instrument' => $this->getPaymentInstrumentConstraints(
59
                $this->getAttributes()['payment_method']
60
            ),
61
            'threeds2_data' => new Assert\Optional([$this->getThreeDS2DataConstraints()])
62
        ]);
63
    }
64
65
    private function getPaymentInstrumentConstraints($method)
66
    {
67
        switch ($method) {
68
            case self::CARD:
69
                return new Assert\Collection([
70
                    'pan' => new Assert\Required([
71
                        new Assert\NotBlank(),
72
                        new Assert\Luhn()
73
                    ]),
74
                    'exp_year' => $this->buildElement(
75
                        'integer', 1, 
76
                        ['min' => 4,'max' => 4],
77
                        new Assert\Range(['min' => date('Y')]),
78
                    ),
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...
79
                    'exp_month' => $this->buildElement('integer', 1),
80
                    'cvc' => $this->buildElement('string', 1, ['min' => 3, 'max' => 4]),
81
                    'holder' => $this->buildElement('string', 1, ['max' => 32]),
82
                ]);
83
            case self::RECURRING:
84
                return new Assert\Collection([
85
                    'payment_id' => $this->buildElement('string', 1),
86
                ]);
87
        }
88
89
        throw new \InvalidArgumentException(
90
            sprintf(
91
                'Payment instrument for payment method "%s" is not expected',
92
                $method
93
            )
94
        );
95
    }
96
97
    private function getThreeDS2DataConstraints()
98
    {
99
        return new Assert\Collection([
100
            'notification_url' => $this->buildElement('string', 1),
101
            'browser_info' => $this->getBrowserInfoConstraints(),
102
            'billing_address' => $this->getAdressConstraints('billingAddress'),
103
            'delivery_address' => $this->getAdressConstraints('deliveryAddress'),
104
            'cardholder_info' => $this->getCardHolderInfoConstraints(),
105
        ]);
106
    }
107
108
    private function getBrowserInfoConstraints()
109
    {
110
        return new Assert\Collection([
111
            'accept_header' => $this->buildElement('string', 1),
112
            'browser_language' => $this->buildElement('string', 1),
113
            'screen_width' => $this->buildElement('integer', 1),
114
            'screen_height' => $this->buildElement('integer', 1),
115
            'challenge_window_size' => $this->buildElement('string', 1),
116
            'user_agent' => $this->buildElement('string', 1),
117
            'color_depth' => $this->buildElement('integer', 1),
118
            'time_zone' => $this->buildElement('integer', 1),
119
            'ip_address' => $this->buildElement('string'),
120
            'javascript_enabled' => $this->buildElement('bool'),
121
            'java_enabled' => $this->buildElement('bool'),
122
        ]);
123
    }
124
125
    private function getAdressConstraints()
126
    {
127
        return new Assert\Collection([
128
            'address_line1' => $this->buildElement('string', 1, ['max'=>50]),
129
            'address_line2' => $this->buildElement('string', 1, ['max'=>50]),
130
            'address_line3' => $this->buildElement('string', 0, ['max'=>50]),
131
            'city' => $this->buildElement('string', 1, ['max'=>50]),
132
            'country' => $this->buildElement('string', 1, ['max'=>10]),
133
            'postal_code' => $this->buildElement('string', 1, ['max'=>16]),
134
            'state' => $this->buildElement('string', 0, ['max'=>14]),
135
        ]);
136
    }
137
138
    private function getCardHolderInfoConstraints()
139
    {
140
        return new Assert\Collection([
141
            'email_address' => new Assert\Optional([
142
                new Assert\Email(['mode'=>'loose'])
143
            ]),
144
            'mobile_phone_number' => $this->buildElement('string'),
145
            'work_phone_number' => $this->buildElement('string'),
146
            'home_phone_number' => $this->buildElement('string'),
147
        ]); 
148
    }
149
150
    private function buildElement(
151
        string $typeValue, 
152
        bool $isRequired = false,
153
        array $length = [],
154
        $args = false
155
    ) {
156
        $inside_array = [
157
            new Assert\Type(['type', $typeValue]),
158
        ];
159
        if ($isRequired) array_unshift($inside_array, new Assert\NotBlank());
160
        if ($length) array_push($inside_array, new Assert\Length($length));
161
        if ($args) array_push($inside_array, $args);
162
        
163
        return $isRequired 
164
            ? new Assert\Required($inside_array)
165
            : new Assert\Optional($inside_array)
166
        ;
167
    }
168
}
169