RfCreditorReferenceGenerator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
eloc 19
c 3
b 0
f 1
dl 0
loc 43
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A loadValidatorMetadata() 0 12 1
A generate() 0 5 1
A doGenerate() 0 11 2
1
<?php declare(strict_types=1);
2
3
namespace Sprain\SwissQrBill\Reference;
4
5
use kmukku\phpIso11649\phpIso11649;
6
use Sprain\SwissQrBill\String\StringModifier;
7
use Sprain\SwissQrBill\Validator\Exception\InvalidCreditorReferenceException;
8
use Sprain\SwissQrBill\Validator\SelfValidatableInterface;
9
use Sprain\SwissQrBill\Validator\SelfValidatableTrait;
10
use Symfony\Component\Validator\Constraints as Assert;
11
use Symfony\Component\Validator\Mapping\ClassMetadata;
12
13
final class RfCreditorReferenceGenerator implements SelfValidatableInterface
14
{
15
    use SelfValidatableTrait;
16
17
    private string $reference;
18
19
    public static function generate(string $reference): string
20
    {
21
        $generator = new self($reference);
22
23
        return $generator->doGenerate();
24
    }
25
26
    public function __construct(string $reference)
27
    {
28
        $this->reference = StringModifier::stripWhitespace($reference);
29
    }
30
31
    public function doGenerate(): string
32
    {
33
        if (!$this->isValid()) {
34
            throw new InvalidCreditorReferenceException(
35
                'The provided data is not valid to generate a creditor reference.'
36
            );
37
        }
38
39
        $generator = new phpIso11649();
40
41
        return $generator->generateRfReference($this->reference, false);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $generator->gener...this->reference, false) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
42
    }
43
44
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
45
    {
46
        $metadata->addPropertyConstraints('reference', [
47
            new Assert\Regex([
48
                'pattern' => '/^[a-zA-Z0-9]*$/',
49
                'match' => true
50
            ]),
51
            new Assert\Length([
52
                'min' => 1,
53
                'max' => 21 // 25 minus 'RF' prefix minus 2-digit check sum
54
            ]),
55
            new Assert\NotBlank()
56
        ]);
57
    }
58
}
59