HasIban::setIban()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Traits;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Ecodev\Felix\Api\Exception;
9
use Laminas\Validator\Iban;
10
11
/**
12
 * Trait for all objects with an IBAN (international bank account number).
13
 */
14
trait HasIban
15
{
16
    #[ORM\Column(type: 'string', length: 34, options: ['default' => ''])]
17
    private string $iban = '';
18
19
    /**
20
     * Set the IBAN (international bank account number).
21
     */
22 2
    public function setIban(string $iban): void
23
    {
24 2
        $iban = str_replace(' ', '', mb_strtoupper($iban));
25
26 2
        $validator = new Iban(['allow_non_sepa' => false]);
27 2
        if (empty($iban) || $validator->isValid($iban)) {
28 2
            $this->iban = $iban;
29
        } else {
30 1
            throw new Exception('Invalid IBAN number');
31
        }
32
    }
33
34
    /**
35
     * Get the IBAN (international bank account number).
36
     */
37 1
    public function getIban(): string
38
    {
39 1
        return $this->iban;
40
    }
41
}
42