Completed
Push — master ( 9d642a...6a24bf )
by Manuel
02:21
created

QrBill   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 219
rs 10
c 0
b 0
f 0
wmc 26

21 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 1
A getUltimateCreditor() 0 3 1
A getCreditor() 0 3 1
A extractQrCodeDataFromElements() 0 11 3
B loadValidatorMetadata() 0 33 1
A setHeader() 0 5 1
A setPaymentAmountInformation() 0 5 1
A getHeader() 0 3 1
A setCreditor() 0 5 1
A getQrCode() 0 16 2
A setAlternativeSchemes() 0 5 1
A getQrCodeData() 0 14 3
A getPaymentReference() 0 3 1
A setCreditorInformation() 0 5 1
A setPaymentReference() 0 5 1
A getAlternativeSchemes() 0 3 1
A getUltimateDebtor() 0 3 1
A getPaymentAmountInformation() 0 3 1
A setUltimateCreditor() 0 5 1
A getCreditorInformation() 0 3 1
A setUltimateDebtor() 0 5 1
1
<?php
2
3
namespace Sprain\SwissQrBill;
4
5
use Endroid\QrCode\QrCode;
6
use Sprain\SwissQrBill\DataGroups\AlternativeScheme;
7
use Sprain\SwissQrBill\DataGroups\Creditor;
8
use Sprain\SwissQrBill\DataGroups\CreditorInformation;
9
use Sprain\SwissQrBill\DataGroups\Header;
10
use Sprain\SwissQrBill\DataGroups\Interfaces\QrCodeData;
11
use Sprain\SwissQrBill\DataGroups\PaymentAmountInformation;
12
use Sprain\SwissQrBill\DataGroups\PaymentReference;
13
use Sprain\SwissQrBill\DataGroups\UltimateCreditor;
14
use Sprain\SwissQrBill\DataGroups\UltimateDebtor;
15
use Sprain\SwissQrBill\Exception\InvalidQrBillDataException;
16
use Sprain\SwissQrBill\Validator\Interfaces\Validatable;
17
use Sprain\SwissQrBill\Validator\ValidatorTrait;
18
use Symfony\Component\Validator\Constraints as Assert;
19
use Symfony\Component\Validator\Mapping\ClassMetadata;
20
21
class QrBill implements Validatable
22
{
23
    use ValidatorTrait;
24
25
    const SWISS_CROSS_LOGO_FILE = __DIR__ . '/../assets/swiss-cross.png';
26
27
    /** @var Header */
28
    private $header;
29
30
    /** @var CreditorInformation */
31
    private $creditorInformation;
32
33
    /** @var Creditor */
34
    private $creditor;
35
36
    /** @var UltimateCreditor */
37
    private $ultimateCreditor;
38
39
    /** @var PaymentAmountInformation */
40
    private $paymentAmountInformation;
41
42
    /** @var UltimateDebtor */
43
    private $ultimateDebtor;
44
45
    /** @var PaymentReference */
46
    private $paymentReference;
47
48
    /** @var AlternativeScheme[] */
49
    private $alternativeSchemes = [];
50
51
    public static function create() : self
52
    {
53
        $header = new Header();
54
        $header->setCoding(Header::CODING_LATIN);
55
        $header->setQrType(Header::QRTYPE_SPC);
56
        $header->setVersion(Header::VERSION_0100);
57
58
        $qrBill = new self();
59
        $qrBill->setHeader($header);
60
61
        return $qrBill;
62
    }
63
64
    public function getHeader(): Header
65
    {
66
        return $this->header;
67
    }
68
69
    public function setHeader(Header $header) : self
70
    {
71
        $this->header = $header;
72
        
73
        return $this;
74
    }
75
76
    public function getCreditorInformation(): CreditorInformation
77
    {
78
        return $this->creditorInformation;
79
    }
80
81
    public function setCreditorInformation(CreditorInformation $creditorInformation) : self
82
    {
83
        $this->creditorInformation = $creditorInformation;
84
85
        return $this;
86
    }
87
88
    public function getCreditor(): Creditor
89
    {
90
        return $this->creditor;
91
    }
92
93
    public function setCreditor(Creditor $creditor) : self
94
    {
95
        $this->creditor = $creditor;
96
        
97
        return $this;
98
    }
99
100
    public function getUltimateCreditor(): ?UltimateCreditor
101
    {
102
        return $this->ultimateCreditor;
103
    }
104
105
    public function setUltimateCreditor(UltimateCreditor $ultimateCreditor) : self
106
    {
107
        $this->ultimateCreditor = $ultimateCreditor;
108
        
109
        return $this;
110
    }
111
112
    public function getPaymentAmountInformation(): PaymentAmountInformation
113
    {
114
        return $this->paymentAmountInformation;
115
    }
116
117
    public function setPaymentAmountInformation(PaymentAmountInformation $paymentAmountInformation) : self
118
    {
119
        $this->paymentAmountInformation = $paymentAmountInformation;
120
        
121
        return $this;
122
    }
123
124
    public function getUltimateDebtor(): ?UltimateDebtor
125
    {
126
        return $this->ultimateDebtor;
127
    }
128
129
    public function setUltimateDebtor(UltimateDebtor $ultimateDebtor) : self
130
    {
131
        $this->ultimateDebtor = $ultimateDebtor;
132
        
133
        return $this;
134
    }
135
136
    public function getPaymentReference(): PaymentReference
137
    {
138
        return $this->paymentReference;
139
    }
140
141
    public function setPaymentReference(PaymentReference $paymentReference) : self
142
    {
143
        $this->paymentReference = $paymentReference;
144
        
145
        return $this;
146
    }
147
148
    public function getAlternativeSchemes(): array
149
    {
150
        return $this->alternativeSchemes;
151
    }
152
153
    public function setAlternativeSchemes(array $alternativeSchemes) : self
154
    {
155
        $this->alternativeSchemes = $alternativeSchemes;
156
157
        return $this;
158
    }
159
160
    public function getQrCode() : QrCode
161
    {
162
        if (!$this->isValid()) {
163
            throw new InvalidQrBillDataException(
164
                'The provided data is not valid to generate a qr code. Use getViolations() to find details.'
165
            );
166
        }
167
168
        $qrCode = new QrCode();
169
        $qrCode->setText($this->getQrCodeData());
170
        $qrCode->setSize(543); // recommended 46x46 mm in px @ 300dpi
171
        $qrCode->setMargin(19); // recommended 1,6 mm in px @ 300dpi
172
        $qrCode->setLogoPath(self::SWISS_CROSS_LOGO_FILE);
173
        $qrCode->setLogoWidth(83); // recommended 7x7 mm in px @ 300dpi
174
175
        return $qrCode;
176
    }
177
178
    private function getQrCodeData() : string
179
    {
180
        $elements = [
181
            $this->getHeader(),
182
            $this->getCreditorInformation(),
183
            $this->getCreditor(),
184
            $this->getUltimateCreditor() ?: new UltimateCreditor(),
185
            $this->getPaymentAmountInformation(),
186
            $this->getUltimateDebtor() ?: new UltimateDebtor(),
187
            $this->getPaymentReference(),
188
            $this->getAlternativeSchemes()
189
        ];
190
191
        return $this->extractQrCodeDataFromElements($elements);
192
    }
193
194
    private function extractQrCodeDataFromElements(array $elements) : string
195
    {
196
        $qrCodeElements = [];
197
198
        foreach ($elements as $element) {
199
            if ($element instanceof QrCodeData) {
200
                $qrCodeElements = array_merge($qrCodeElements, $element->getQrCodeData());
201
            }
202
        }
203
204
        return implode("\r\n", $qrCodeElements);
205
    }
206
207
    public static function loadValidatorMetadata(ClassMetadata $metadata)
208
    {
209
        $metadata->addPropertyConstraints('header', [
210
            new Assert\NotNull(),
211
            new Assert\Valid()
212
        ]);
213
214
        $metadata->addPropertyConstraints('creditorInformation', [
215
            new Assert\NotNull(),
216
            new Assert\Valid()
217
        ]);
218
219
        $metadata->addPropertyConstraints('creditor', [
220
            new Assert\NotNull(),
221
            new Assert\Valid()
222
        ]);
223
224
        $metadata->addPropertyConstraints('ultimateCreditor', [
225
            new Assert\Valid()
226
        ]);
227
228
        $metadata->addPropertyConstraints('paymentAmountInformation', [
229
            new Assert\NotNull(),
230
            new Assert\Valid()
231
        ]);
232
233
        $metadata->addPropertyConstraints('ultimateDebtor', [
234
            new Assert\Valid()
235
        ]);
236
237
        $metadata->addPropertyConstraints('paymentReference', [
238
            new Assert\NotNull(),
239
            new Assert\Valid()
240
        ]);
241
    }
242
}
243