Passed
Push — master ( 683828...ff7ee8 )
by
unknown
07:41
created

HasIban::setIban()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 9
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Traits;
6
7
use Doctrine\DBAL\Exception\InvalidArgumentException;
8
use Doctrine\ORM\Mapping as ORM;
9
10
/**
11
 * Trait for all objects with an IBAN (international bank account number)
12
 */
13
trait HasIban
14
{
15
    /**
16
     * @var string
17
     *
18
     * @ORM\Column(type="string", length=34, unique=true, nullable=true)
19
     */
20
    private $iban;
21
22
    /**
23
     * Set the IBAN (international bank account number)
24
     *
25
     * @param string $iban
26
     *
27
     * @throws InvalidArgumentException
28
     */
29 1
    public function setIban(string $iban): void
30
    {
31 1
        $validator = new \Zend\Validator\Iban(['country_code' => 'CH']);
32 1
        if (empty($iban)) {
33
            $this->iban = null;
34 1
        } elseif ($validator->isValid($iban)) {
35 1
            $this->iban = $iban;
36
        } else {
37 1
            throw new InvalidArgumentException('Invalid IBAN number');
38
        }
39 1
    }
40
41
    /**
42
     * Get the IBAN (international bank account number)
43
     *
44
     * @return string
45
     */
46 1
    public function getIban(): string
47
    {
48 1
        return (string) $this->iban;
49
    }
50
}
51