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

validate()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 7
nop 2
dl 0
loc 30
rs 8.0555
c 0
b 0
f 0
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
}