Passed
Push — master ( cef0e5...e37e02 )
by Manuel
42s
created

ValidCreditorInformationPaymentReferenceCombinationValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 22 7
1
<?php
2
3
namespace Sprain\SwissQrBill\Constraint;
4
5
use kmukku\phpIso11649\phpIso11649;
6
use Sprain\SwissQrBill\DataGroup\Element\PaymentReference;
7
use Sprain\SwissQrBill\QrBill;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
11
class ValidCreditorInformationPaymentReferenceCombinationValidator extends ConstraintValidator
12
{
13
    private const QR_IBAN_IS_ALLOWED = [
14
        PaymentReference::TYPE_QR   => true,
15
        PaymentReference::TYPE_SCOR => false,
16
        PaymentReference::TYPE_NON  => false,
17
    ];
18
19
    public function validate($qrBill, Constraint $constraint)
20
    {
21
        if (!$qrBill instanceof QrBill) {
22
            return;
23
        }
24
25
        $creditorInformation = $qrBill->getCreditorInformation();
26
        $paymentReference = $qrBill->getPaymentReference();
27
28
        if (null === $creditorInformation || null === $paymentReference) {
29
            return;
30
        }
31
32
        if (null === $creditorInformation->getIban() || null === $paymentReference->getType()) {
1 ignored issue
show
introduced by
The condition null === $paymentReference->getType() is always false.
Loading history...
33
            return;
34
        }
35
36
        if (self::QR_IBAN_IS_ALLOWED[$paymentReference->getType()] !== $creditorInformation->containsQrIban()) {
37
            $this->context->buildViolation($constraint->message)
38
                ->setParameter('{{ referenceType }}', $paymentReference->getType())
39
                ->setParameter('{{ iban }}', $creditorInformation->getIban())
40
                ->addViolation();
41
        }
42
    }
43
}