Passed
Pull Request — master (#18)
by Manuel
01:30
created

ValidCreditorInformationPaymentReferenceCombinationValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 30 9
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
    public function validate($qrBill, Constraint $constraint)
14
    {
15
        if (!$qrBill instanceof QrBill) {
16
            return;
17
        }
18
19
        $creditorInformation = $qrBill->getCreditorInformation();
20
        $paymentReference = $qrBill->getPaymentReference();
21
22
        if (null === $creditorInformation || null === $paymentReference) {
23
            return;
24
        }
25
26
        if (null === $creditorInformation->getIban() || null === $paymentReference->getType()) {
1 ignored issue
show
introduced by
The condition null === $paymentReference->getType() is always false.
Loading history...
27
            return;
28
        }
29
30
        if ($creditorInformation->containsQrIban()) {
31
            if ($qrBill->getPaymentReference()->getType() !== PaymentReference::TYPE_QR) {
32
                $this->context->buildViolation($constraint->message)
33
                    ->setParameter('{{ referenceType }}', $paymentReference->getType())
34
                    ->setParameter('{{ iban }}', $creditorInformation->getIban())
35
                    ->addViolation();
36
            }
37
        } else {
38
            if ($qrBill->getPaymentReference()->getType() === PaymentReference::TYPE_QR) {
39
                $this->context->buildViolation($constraint->message)
40
                    ->setParameter('{{ referenceType }}', $paymentReference->getType())
41
                    ->setParameter('{{ iban }}', $creditorInformation->getIban())
42
                    ->addViolation();
43
            }
44
        }
45
    }
46
}