ValidCreditorInformationPaymentReferenceCombinationValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 2
b 0
f 0
dl 0
loc 30
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 22 6
1
<?php declare(strict_types=1);
2
3
namespace Sprain\SwissQrBill\Constraint;
4
5
use Sprain\SwissQrBill\DataGroup\Element\PaymentReference;
6
use Sprain\SwissQrBill\QrBill;
7
use Symfony\Component\Validator\Constraint;
8
use Symfony\Component\Validator\ConstraintValidator;
9
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
10
11
/**
12
 * @internal
13
 */
14
final class ValidCreditorInformationPaymentReferenceCombinationValidator extends ConstraintValidator
15
{
16
    private const QR_IBAN_IS_ALLOWED = [
17
        PaymentReference::TYPE_QR   => true,
18
        PaymentReference::TYPE_SCOR => false,
19
        PaymentReference::TYPE_NON  => false,
20
    ];
21
22
    public function validate($qrBill, Constraint $constraint): void
23
    {
24
        if (!$constraint instanceof ValidCreditorInformationPaymentReferenceCombination) {
25
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\ValidCreditorInformationPaymentReferenceCombination');
26
        }
27
28
        if (!$qrBill instanceof QrBill) {
29
            return;
30
        }
31
32
        $creditorInformation = $qrBill->getCreditorInformation();
33
        $paymentReference = $qrBill->getPaymentReference();
34
35
        if (null === $creditorInformation || null === $paymentReference) {
36
            return;
37
        }
38
39
        if (self::QR_IBAN_IS_ALLOWED[$paymentReference->getType()] !== $creditorInformation->containsQrIban()) {
40
            $this->context->buildViolation($constraint->message)
41
                ->setParameter('{{ referenceType }}', $paymentReference->getType())
42
                ->setParameter('{{ iban }}', $creditorInformation->getIban())
43
                ->addViolation();
44
        }
45
    }
46
}
47