Passed
Pull Request — master (#28)
by Volodymyr
05:26
created

BankAccountValidator::validate()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 2
dl 0
loc 22
rs 9.2222
c 0
b 0
f 0
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 $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->buildViolation($validationMessage)
0 ignored issues
show
Bug introduced by
The method buildViolation() does not exist on SprykerEco\Yves\Payone\F...nt\BankAccountValidator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
                $this->/** @scrutinizer ignore-call */ 
52
                       buildViolation($validationMessage)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
                ->addViolation();
53
            }
54
        }
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\QuoteTransfer $data
59
     * @param \SprykerEco\Yves\Payone\Form\Constraint\BankAccount $constraint
60
     *
61
     * @return string[]
62
     */
63
    protected function validateBankAccount(QuoteTransfer $data, BankAccount $constraint)
64
    {
65
        $response = $constraint->getPayoneClient()->bankAccountCheck($data);
66
        if (in_array($response->getStatus(), static::INVALID_STATUSES)) {
67
            return [$response->getCustomerErrorMessage()];
68
        }
69
70
        return [];
71
    }
72
}
73