Passed
Push — master ( 903b32...e503ad )
by Manuel
06:03
created

RfCreditorReferenceGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
eloc 19
c 2
b 0
f 1
dl 0
loc 44
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
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
class RfCreditorReferenceGenerator implements SelfValidatableInterface
14
{
15
    use SelfValidatableTrait;
16
17
    /** @var string */
18
    protected $reference;
19
20
    public static function generate(string $reference) : string
21
    {
22
        $generator = new self($reference);
23
24
        return $generator->doGenerate();
25
    }
26
27
    public function __construct(string $reference)
28
    {
29
        $this->reference = StringModifier::stripWhitespace($reference);
30
    }
31
32
    public function doGenerate() : string
33
    {
34
        if (!$this->isValid()) {
35
            throw new InvalidCreditorReferenceException(
36
                'The provided data is not valid to generate a creditor reference.'
37
            );
38
        }
39
40
        $generator = new phpIso11649();
41
42
        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...
43
    }
44
45
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
46
    {
47
        $metadata->addPropertyConstraints('reference', [
48
            new Assert\Regex([
49
                'pattern' => '/^[a-zA-Z0-9]*$/',
50
                'match' => true
51
            ]),
52
            new Assert\Length([
53
                'min' => 1,
54
                'max' => 21 // 25 minus 'RF' prefix minus 2-digit check sum
55
            ]),
56
            new Assert\NotBlank()
57
        ]);
58
    }
59
}
60