QrCode   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 334
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 30
lcom 4
cbo 4
dl 0
loc 334
ccs 0
cts 135
cp 0
rs 10
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A setForegroundColor() 0 10 1
A setBackgroundColor() 0 10 1
A setLogo() 0 10 2
A setEncoding() 0 6 1
A setWriter() 0 6 1
A setErrorCorrectionLevel() 0 6 1
A setText() 0 6 1
A setSize() 0 6 1
A setMargin() 0 6 1
A setLogoWidth() 0 6 1
A setLabel() 0 6 2
A getText() 0 4 1
A getSize() 0 4 1
A getMargin() 0 4 1
A getForegroundColor() 0 4 1
A getBackgroundColor() 0 4 1
A getEncoding() 0 4 1
A getErrorCorrectionLevel() 0 4 1
A getLogoPath() 0 4 1
A getLogoWidth() 0 4 1
A getLabel() 0 4 1
A getContentType() 0 4 1
A writeString() 0 4 1
A writeDataUri() 0 4 1
A writeFile() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/qrcode-library project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\QrCode;
13
14
use Da\QrCode\Contracts\ErrorCorrectionLevelInterface;
15
use Da\QrCode\Contracts\LabelInterface;
16
use Da\QrCode\Contracts\QrCodeInterface;
17
use Da\QrCode\Contracts\WriterInterface;
18
use Da\QrCode\Exception\InvalidPathException;
19
use Da\QrCode\Writer\PngWriter;
20
21
class QrCode implements QrCodeInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $text;
27
    /**
28
     * @var int
29
     */
30
    protected $size = 300;
31
    /**
32
     * @var int
33
     */
34
    protected $margin = 10;
35
    /**
36
     * @var array
37
     */
38
    protected $foregroundColor = [
39
        'r' => 0,
40
        'g' => 0,
41
        'b' => 0
42
    ];
43
    /**
44
     * @var array
45
     */
46
    protected $backgroundColor = [
47
        'r' => 255,
48
        'g' => 255,
49
        'b' => 255
50
    ];
51
    /**
52
     * @var string
53
     */
54
    protected $encoding = 'UTF-8';
55
    /**
56
     * @var string ErrorCorrectionLevelInterface value
57
     */
58
    protected $errorCorrectionLevel;
59
    /**
60
     * @var string
61
     */
62
    protected $logoPath;
63
    /**
64
     * @var int
65
     */
66
    protected $logoWidth;
67
    /**
68
     * @var LabelInterface
69
     */
70
    protected $label;
71
    /**
72
     * @var WriterInterface
73
     */
74
    protected $writer;
75
76
    /**
77
     * QrCode constructor.
78
     *
79
     * @param string|null $text
80
     * @param string|null $errorCorrectionLevel
81
     * @param WriterInterface|null $writer
82
     */
83
    public function __construct(string $text, string $errorCorrectionLevel = null, WriterInterface $writer = null)
84
    {
85
        $this->text = $text;
86
        $this->errorCorrectionLevel = $errorCorrectionLevel ?: ErrorCorrectionLevelInterface::LOW;
87
        $this->writer = $writer ?: new PngWriter();
88
    }
89
90
    /**
91
     * @param int $red
92
     * @param int $green
93
     * @param int $blue
94
     *
95
     * @return $this
96
     */
97
    public function setForegroundColor(int $red, int $green, int $blue): self
98
    {
99
        $this->foregroundColor = [
100
            'r' => $red,
101
            'g' => $green,
102
            'b' => $blue,
103
        ];
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param int $red
110
     * @param int $green
111
     * @param int $blue
112
     *
113
     * @return $this
114
     */
115
    public function setBackgroundColor(int $red, int $green, int $blue): self
116
    {
117
        $this->backgroundColor = [
118
            'r' => $red,
119
            'g' => $green,
120
            'b' => $blue,
121
        ];
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param string $path
128
     *
129
     * @return $this
130
     * @throws InvalidPathException
131
     */
132
    public function setLogo(string $path): self
133
    {
134
        $logo = realpath($path);
135
        if (!is_file($logo)) {
136
            throw new InvalidPathException(sprintf('Invalid logo path: "%s"', $logo));
137
        }
138
        $this->logoPath = $logo;
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param string $encoding
145
     *
146
     * @return $this
147
     */
148
    public function setEncoding(string $encoding): self
149
    {
150
        $this->encoding = $encoding;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param WriterInterface $writer
157
     *
158
     * @return $this
159
     */
160
    public function setWriter(WriterInterface $writer): self
161
    {
162
        $this->writer = $writer;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param string $errorCorrectionLevel
169
     *
170
     * @return $this
171
     */
172
    public function setErrorCorrectionLevel(string $errorCorrectionLevel): self
173
    {
174
        $this->errorCorrectionLevel = $errorCorrectionLevel;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @param string $text
181
     *
182
     * @return $this
183
     */
184
    public function setText(string $text): self
185
    {
186
        $this->text = $text;
187
188
        return $this;
189
    }
190
191
    /**
192
     * @param int $size
193
     *
194
     * @return $this
195
     */
196
    public function setSize(int $size): self
197
    {
198
        $this->size = $size;
199
200
        return $this;
201
    }
202
203
    /**
204
     * @param int $margin
205
     *
206
     * @return $this
207
     */
208
    public function setMargin(int $margin): self
209
    {
210
        $this->margin = $margin;
211
212
        return $this;
213
    }
214
215
    /**
216
     * @param int $width
217
     *
218
     * @return $this
219
     */
220
    public function setLogoWidth(int $width): self
221
    {
222
        $this->logoWidth = $width;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @param LabelInterface|string $label
229
     *
230
     * @return $this
231
     */
232
    public function setLabel($label): self
233
    {
234
        $this->label = $label instanceof LabelInterface ? $label : new Label($label);
235
236
        return $this;
237
    }
238
239
    /**
240
     * @inheritdoc
241
     */
242
    public function getText(): ?string
243
    {
244
        return $this->text;
245
    }
246
247
    /**
248
     * @inheritdoc
249
     */
250
    public function getSize(): int
251
    {
252
        return $this->size;
253
    }
254
255
    /**
256
     * @inheritdoc
257
     */
258
    public function getMargin(): int
259
    {
260
        return $this->margin;
261
    }
262
263
    /**
264
     * @inheritdoc
265
     */
266
    public function getForegroundColor(): array
267
    {
268
        return $this->foregroundColor;
269
    }
270
271
    /**
272
     * @inheritdoc
273
     */
274
    public function getBackgroundColor(): array
275
    {
276
        return $this->backgroundColor;
277
    }
278
279
    /**
280
     * @inheritdoc
281
     */
282
    public function getEncoding(): string
283
    {
284
        return $this->encoding;
285
    }
286
287
    /**
288
     * @inheritdoc
289
     */
290
    public function getErrorCorrectionLevel(): string
291
    {
292
        return $this->errorCorrectionLevel;
293
    }
294
295
    /**
296
     * @inheritdoc
297
     */
298
    public function getLogoPath(): ?string
299
    {
300
        return $this->logoPath;
301
    }
302
303
    /**
304
     * @inheritdoc
305
     */
306
    public function getLogoWidth(): ?int
307
    {
308
        return $this->logoWidth;
309
    }
310
311
    /**
312
     * @inheritdoc
313
     */
314
    public function getLabel(): ?LabelInterface
315
    {
316
        return $this->label;
317
    }
318
319
    /**
320
     * @return string
321
     */
322
    public function getContentType(): string
323
    {
324
        return $this->writer->getContentType();
325
    }
326
327
    /**
328
     * @throws Exception\ValidationException
329
     * @throws Exception\BadMethodCallException
330
     * @return string
331
     */
332
    public function writeString(): string
333
    {
334
        return $this->writer->writeString($this);
335
    }
336
337
    /**
338
     * @return string
339
     */
340
    public function writeDataUri(): string
341
    {
342
        return $this->writer->writeDataUri($this);
343
    }
344
345
    /**
346
     * @param string $path
347
     *
348
     * @return bool|int
349
     */
350
    public function writeFile(string $path)
351
    {
352
        return $this->writer->writeFile($this, $path);
353
    }
354
}
355