Generator   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 19
c 3
b 2
f 0
lcom 1
cbo 1
dl 0
loc 236
ccs 52
cts 52
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A generate() 0 9 1
A getContent() 0 4 1
A setContent() 0 4 1
A getSize() 0 4 1
A setSize() 0 4 3
A getColor() 0 4 1
A setColor() 0 4 1
A getBackgroundColor() 0 4 1
A setBackgroundColor() 0 4 1
B processColor() 0 28 4
A create() 0 11 1
A getGenerator() 0 7 2
1
<?php
2
3
namespace Nathanmac\Utilities\QRCode;
4
5
use Endroid\QrCode\QrCode;
6
7
class Generator
8
{
9
    /**
10
     * Default Size
11
     */
12
    const DEFAULT_SIZE = 100;
13
14
    /**
15
     * Default Color
16
     */
17
    const DEFAULT_COLOR = '000000';
18
19
    /**
20
     * Default background
21
     *  if false transparent
22
     */
23
    const DEFAULT_BACKGROUND = false; // Transparent
24
25
    /**
26
     * @var QrCode|null
27
     */
28
    protected $QRGenerator = null;
29
30
    /**
31
     * @var string
32
     */
33
    protected $content;
34
35
    /**
36
     * @var int
37
     */
38
    protected $size;
39
40
    /**
41
     * @var array
42
     */
43
    protected $color;
44
45
    /**
46
     * @var array
47
     */
48
    protected $backgroundColor;
49
50
    /**
51
     * Generator constructor.
52
     *
53
     * @param string $content
54
     * @param int    $size
55
     * @param mixed  $color
56
     * @param mixed  $background
57
     */
58 30
    public function __construct(
59
        $content,
60
        $size = self::DEFAULT_SIZE,
61
        $color = self::DEFAULT_COLOR,
62
        $background = self::DEFAULT_BACKGROUND
63
    ) {
64 30
        $this->setContent($content);
65 30
        $this->setSize($size);
66 30
        $this->setColor($color);
67 30
        $this->setBackgroundColor($background);
68 30
    }
69
70
    /**
71
     * Generate QR Code
72
     *
73
     * @param string $content
74
     * @param int    $size
75
     * @param mixed  $color
76
     * @param mixed  $background
77
     *
78
     * @codeCoverageIgnore
79
     *
80
     * @return Generator
81
     */
82
    public static function generate(
83
        $content,
84
        $size = self::DEFAULT_SIZE,
85
        $color = self::DEFAULT_COLOR,
86
        $background = self::DEFAULT_BACKGROUND
87
    ) {
88
        return (new self($content, $size, $color, $background))
89
            ->create();
90
    }
91
92
    /**
93
     * Gets the content
94
     *
95
     * @return string
96
     */
97 6
    public function getContent()
98
    {
99 6
        return $this->content;
100
    }
101
102
    /**
103
     * Sets the content
104
     *
105
     * @param string $content
106
     */
107 30
    public function setContent($content)
108
    {
109 30
        $this->content = str_replace('+', ' ', $content);;
110 30
    }
111
112
    /**
113
     * Gets the size
114
     *
115
     * @return int
116
     */
117 15
    public function getSize()
118
    {
119 15
        return $this->size;
120
    }
121
122
    /**
123
     * Sets the size
124
     *
125
     * @param int $size
126
     */
127 30
    public function setSize($size)
128
    {
129 30
        if (is_numeric($size) && $size > 0) $this->size = (int) $size;
130 30
    }
131
132
    /**
133
     * Gets the color
134
     *
135
     * @return array
136
     */
137 21
    public function getColor()
138
    {
139 21
        return $this->color;
140
    }
141
142
    /**
143
     * Sets the color
144
     *
145
     * @param mixed $color
146
     */
147 30
    public function setColor($color)
148
    {
149 30
        $this->color = $this->processColor($color, self::DEFAULT_COLOR);
150 30
    }
151
152
    /**
153
     * Gets the background color
154
     *
155
     * @return array
156
     */
157 21
    public function getBackgroundColor()
158
    {
159 21
        return $this->backgroundColor;
160
    }
161
162
    /**
163
     * Sets the background color
164
     *
165
     * @param mixed $backgroundColor
166
     */
167 30
    public function setBackgroundColor($backgroundColor)
168
    {
169 30
        $this->backgroundColor = $this->processColor($backgroundColor, self::DEFAULT_BACKGROUND);
170 30
    }
171
172
    /**
173
     * Process the Color Value
174
     *
175
     * @param mixed $color
176
     * @param mixed $default
177
     *
178
     * @return array
179
     */
180 30
    protected function processColor($color, $default)
181
    {
182
        // Transparent
183 30
        if (false === $color) {
184 30
            return array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 255);
185
        }
186
187 30
        if (! preg_match("/^([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/", $color)) {
188 3
            $color = $default;
189 2
        }
190
191 30
        if (strlen($color) === 6) {
192
            return array(
193 30
                'r' => hexdec(substr($color, 0, 2)),
194 30
                'g' => hexdec(substr($color, 2, 2)),
195 30
                'b' => hexdec(substr($color, 4, 2)),
196 10
                'a' => 0
197 20
            );
198
        }
199
200
        // Length of 3
201
        return array(
202 6
            'r' => hexdec(substr($color, 0, 1) . substr($color, 0, 1)),
203 6
            'g' => hexdec(substr($color, 1, 1) . substr($color, 1, 1)),
204 6
            'b' => hexdec(substr($color, 2, 1) . substr($color, 2, 1)),
205 2
            'a' => 0
206 4
        );
207
    }
208
209
    /**
210
     * Generates a QRCode
211
     *
212
     * @return QrCode
213
     *
214
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
215
     */
216 3
    public function create()
217
    {
218 3
        return $this->getGenerator()
219 3
            ->setText($this->getContent())
220 3
            ->setSize($this->getSize())
221 3
            ->setPadding(10)
222 3
            ->setErrorCorrection('high')
223 3
            ->setForegroundColor($this->getColor())
224 3
            ->setBackgroundColor($this->getBackgroundColor())
225 3
            ->render();
226
    }
227
228
    /**
229
     * Returns the QRCode Generator Instance
230
     *
231
     * @codeCoverageIgnore
232
     *
233
     * @return QrCode
234
     */
235
    protected function getGenerator()
236
    {
237
        if (is_null($this->QRGenerator))
238
            $this->QRGenerator = new QrCode();
239
240
        return $this->QRGenerator;
241
    }
242
}