Completed
Push — master ( e178ea...863dc0 )
by Nathan
14:47
created

Generator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6667
cc 1
eloc 7
nc 1
nop 4
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 size
37
     */
38
    protected $size;
39
40
    /**
41
     * @var string
42
     */
43
    protected $color;
44
45
    /**
46
     * @var string|bool
47
     */
48
    protected $backgroundColor;
49
50
    /**
51
     * Generator constructor.
52
     *
53
     * @param string      $content
54
     * @param int         $size
55
     * @param string      $color
56
     * @param string|bool $background
57
     */
58
    public function __construct(
59
        $content,
60
        $size = self::DEFAULT_SIZE,
61
        $color = self::DEFAULT_COLOR,
62
        $background = self::DEFAULT_BACKGROUND
63
    ) {
64
        $this->setContent($content);
65
        $this->setSize($size);
66
        $this->setColor($color);
67
        $this->setBackgroundColor($background);
68
    }
69
70
    /**
71
     * Generate QR Code
72
     *
73
     * @param string      $content
74
     * @param int         $size
75
     * @param string      $color
76
     * @param string|bool $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 mixed
96
     */
97
    public function getContent()
98
    {
99
        return $this->content;
100
    }
101
102
    /**
103
     * Sets the content
104
     *
105
     * @param mixed $content
106
     */
107
    public function setContent($content)
108
    {
109
        $this->content = str_replace('+', ' ', $content);;
110
    }
111
112
    /**
113
     * Gets the size
114
     *
115
     * @return int
116
     */
117
    public function getSize()
118
    {
119
        return $this->size;
120
    }
121
122
    /**
123
     * Sets the size
124
     *
125
     * @param int $size
126
     */
127
    public function setSize($size)
128
    {
129
        if (is_numeric($size) && $size > 0) $this->size = $size;
0 ignored issues
show
Documentation Bug introduced by
It seems like $size of type integer or double or string is incompatible with the declared type object<Nathanmac\Utilities\QRCode\size> of property $size.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
130
    }
131
132
    /**
133
     * Gets the color
134
     *
135
     * @return string
136
     */
137
    public function getColor()
138
    {
139
        return $this->color;
140
    }
141
142
    /**
143
     * Sets the color
144
     *
145
     * @param string $color
146
     */
147
    public function setColor($color)
148
    {
149
        $this->color = $this->processColor($color, self::DEFAULT_COLOR);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->processColor($color, self::DEFAULT_COLOR) of type array<string,integer|dou...double","a":"integer"}> is incompatible with the declared type string of property $color.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
150
    }
151
152
    /**
153
     * Gets the background color
154
     *
155
     * @return bool|string
156
     */
157
    public function getBackgroundColor()
158
    {
159
        return $this->backgroundColor;
160
    }
161
162
    /**
163
     * Sets the background color
164
     *
165
     * @param bool|string $backgroundColor
166
     */
167
    public function setBackgroundColor($backgroundColor)
168
    {
169
        $this->backgroundColor = $this->processColor($backgroundColor, self::DEFAULT_BACKGROUND);
0 ignored issues
show
Documentation introduced by
self::DEFAULT_BACKGROUND is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation Bug introduced by
It seems like $this->processColor($bac...lf::DEFAULT_BACKGROUND) of type array<string,integer|dou...double","a":"integer"}> is incompatible with the declared type string|boolean of property $backgroundColor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Bug introduced by
It seems like $backgroundColor defined by parameter $backgroundColor on line 167 can also be of type boolean; however, Nathanmac\Utilities\QRCo...nerator::processColor() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
170
    }
171
172
    /**
173
     * Process the Color Value
174
     *
175
     * @param string $color
176
     * @param string $default
177
     *
178
     * @return array
179
     */
180
    protected function processColor($color, $default)
181
    {
182
        // Transparent
183
        if (false === $color) {
184
            return array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 255);
185
        }
186
187
        if (! preg_match("/^([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/", $color)) {
188
            $color = $default;
189
        }
190
191
        if (strlen($color) === 6) {
192
            return array(
193
                'r' => hexdec(substr($color, 0, 2)),
194
                'g' => hexdec(substr($color, 2, 2)),
195
                'b' => hexdec(substr($color, 4, 2)),
196
                'a' => 0
197
            );
198
        }
199
200
        // Length of 3
201
        return array(
202
            'r' => hexdec(substr($color, 0, 1) . substr($color, 0, 1)),
203
            'g' => hexdec(substr($color, 1, 1) . substr($color, 1, 1)),
204
            'b' => hexdec(substr($color, 2, 1) . substr($color, 2, 1)),
205
            'a' => 0
206
        );
207
    }
208
209
    /**
210
     * Generates a QRCode
211
     *
212
     * @return QrCode
213
     *
214
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
215
     */
216
    public function create()
217
    {
218
        return $this->getGenerator()
219
            ->setText($this->getContent())
220
            ->setSize($this->getSize())
221
            ->setPadding(10)
222
            ->setErrorCorrection('high')
223
            ->setForegroundColor($this->getColor())
224
            ->setBackgroundColor($this->getBackgroundColor())
225
            ->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
}