BankAccountValidator::validate()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 23
rs 9.2222
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\Payone\Form\Constraint;
9
10
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SprykerEco\Shared\Payone\PayoneApiConstants;
12
use Symfony\Component\Form\Exception\UnexpectedTypeException;
13
use Symfony\Component\Validator\Constraint;
14
use Symfony\Component\Validator\ConstraintValidator;
15
16
class BankAccountValidator extends ConstraintValidator
17
{
18
    public const INVALID_STATUSES = [
19
        PayoneApiConstants::RESPONSE_TYPE_ERROR,
20
        PayoneApiConstants::RESPONSE_TYPE_INVALID,
21
    ];
22
23
    /**
24
     * @param string|null $value
25
     * @param \Symfony\Component\Validator\Constraint|\SprykerEco\Yves\Payone\Form\Constraint\BankAccount $constraint
26
     *
27
     * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
28
     *
29
     * @return void
30
     */
31
    public function validate($value, Constraint $constraint)
32
    {
33
        if (!$constraint instanceof BankAccount) {
34
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\BankAccount');
35
        }
36
37
        if ($value === null || $value === '') {
38
            return;
39
        }
40
41
        /** @var \Symfony\Component\Form\Form $root */
42
        $root = $this->context->getRoot();
43
44
        /** @var \Generated\Shared\Transfer\QuoteTransfer $data */
45
        $data = $root->getData();
46
47
        $validationMessages = $this->validateBankAccount($data, $constraint);
48
49
        if (count($validationMessages) > 0) {
50
            foreach ($validationMessages as $validationMessage) {
51
                $this->context
52
                    ->buildViolation($validationMessage)
53
                    ->addViolation();
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param \Generated\Shared\Transfer\QuoteTransfer $data
60
     * @param \SprykerEco\Yves\Payone\Form\Constraint\BankAccount $constraint
61
     *
62
     * @return string[]
63
     */
64
    protected function validateBankAccount(QuoteTransfer $data, BankAccount $constraint): array
65
    {
66
        $response = $constraint->getPayoneClient()->bankAccountCheck($data);
67
        if (in_array($response->getStatus(), static::INVALID_STATUSES)) {
68
            return [$response->getCustomerErrorMessage()];
69
        }
70
71
        return [];
72
    }
73
}
74