Label   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 112
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
A setFontSize() 0 6 1
A setFont() 0 11 2
A getFont() 0 4 1
A getText() 0 4 1
A getFontSize() 0 4 1
A getAlignment() 0 4 1
A getMargins() 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\LabelInterface;
15
use Da\QrCode\Exception\InvalidPathException;
16
17
class Label implements LabelInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $text;
23
    /**
24
     * @var int
25
     */
26
    protected $fontSize;
27
    /**
28
     * @var string
29
     */
30
    protected $font;
31
    /**
32
     * @var string
33
     */
34
    protected $alignment;
35
    /**
36
     * @var array
37
     */
38
    protected $margins = [
39
        't' => 0,
40
        'r' => 10,
41
        'b' => 10,
42
        'l' => 10,
43
    ];
44
45
    /**
46
     * Label constructor.
47
     *
48
     * @param string $text
49
     * @param string|null $font
50
     * @param int|null    $fontSize
51
     * @param string|null $alignment
52
     * @param array       $margins
53
     */
54
    public function __construct(string $text, string $font = null, $fontSize = null, $alignment = null, array $margins = [])
55
    {
56
        $this->text = $text;
57
        $this->font = $font ?: __DIR__ . '/../resources/fonts/noto_sans.otf';
58
        $this->fontSize = $fontSize ?: 16;
59
        $this->alignment = $alignment ?: LabelInterface::ALIGN_CENTER;
60
        $this->margins = array_merge($this->margins, $margins);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function setFontSize(int $size): LabelInterface
67
    {
68
        $this->fontSize = $size;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     * @throws InvalidPathException
76
     */
77
    public function setFont(string $font): LabelInterface
78
    {
79
        $path = realpath($font);
80
        if (!is_file($path)) {
81
            throw new InvalidPathException(sprintf('Invalid label font path "%s"', $path));
82
        }
83
84
        $this->font = $path;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function getFont(): string
93
    {
94
        return $this->font;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function getText(): string
101
    {
102
        return $this->text;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function getFontSize(): int
109
    {
110
        return $this->fontSize;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getAlignment(): string
117
    {
118
        return $this->alignment;
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function getMargins(): array
125
    {
126
        return $this->margins;
127
    }
128
}
129