QrCodeComponent::init()   B
last analyzed

Complexity

Conditions 9
Paths 32

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 32
nop 0
crap 90
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\Component;
13
14
use Da\QrCode\Contracts\LabelInterface;
15
use Da\QrCode\Contracts\QrCodeInterface;
16
use Da\QrCode\Contracts\WriterInterface;
17
use Da\QrCode\Exception\InvalidPathException;
18
use Da\QrCode\QrCode;
19
use yii\base\Component;
20
21
/**
22
 * Class QrCodeComponent
23
 *
24
 * @author Antonio Ramirez <[email protected]>
25
 * @package Da\QrCode\Component
26
 *
27
 * @method QrCode useForegroundColor(integer $red, integer $green, integer $blue)
28
 * @method QrCode useBackgroundColor(integer $red, integer $green, integer $blue)
29
 * @method QrCode useEncoding(string $encoding)
30
 * @method QrCode useWriter(WriterInterface $writer)
31
 * @method QrCode useLogo(string $logo)
32
 * @method QrCode setText(string $text)
33
 * @method QrCode setSize(integer $size)
34
 * @method QrCode setLogoWidth(integer $width)
35
 * @method QrCode setLabel(LabelInterface $label)
36
 * @method QrCode setMargin(integer $margin)
37
 * @method QrCode setErrorCorrectionLevel(string $errorCorrectionLevel)
38
 * @method string getText()
39
 * @method integer getSize()
40
 * @method array getMargin()
41
 * @method array getForegroundColor()
42
 * @method array getBackgroundColor()
43
 * @method string getEncoding()
44
 * @method string getErrorCorrectionLevel()
45
 * @method string getLogoPath()
46
 * @method integer getLogoWidth()
47
 * @method LabelInterface getLabel()
48
 * @method string writeString()
49
 * @method string writeDataUri()
50
 * @method bool|integer writeFile(string $path)
51
 * @method string getContentType()
52
 */
53
class QrCodeComponent extends Component
54
{
55
    /**
56
     * @var string
57
     */
58
    public $text = '';
59
    /**
60
     * @var int
61
     */
62
    public $size = 300;
63
    /**
64
     * @var int
65
     */
66
    public $margin = 10;
67
    /**
68
     * @var array the foreground color. Syntax is:
69
     *
70
     * ```
71
     * [
72
     *  'r' => 0, // RED
73
     *  'g' => 0, // GREEN
74
     *  'b' => 0  // BLUE
75
     * ]
76
     * ```
77
     */
78
    public $foregroundColor;
79
    /**
80
     * @var array the background color. Syntax is:
81
     *
82
     * ```
83
     * [
84
     *  'r' => 255, // RED
85
     *  'g' => 255, // GREEN
86
     *  'b' => 255  // BLUE
87
     * ]
88
     * ```
89
     *
90
     */
91
    public $backgroundColor;
92
    /**
93
     * @var string
94
     */
95
    public $encoding = 'UTF-8';
96
    /**
97
     * @var string ErrorCorrectionLevelInterface value
98
     */
99
    public $errorCorrectionLevel;
100
    /**
101
     * @var string
102
     */
103
    public $logoPath;
104
    /**
105
     * @var int
106
     */
107
    public $logoWidth;
108
    /**
109
     * @var LabelInterface|string
110
     */
111
    public $label;
112
    /**
113
     * @var WriterInterface
114
     */
115
    public $writer;
116
    /**
117
     * @var QrCodeInterface
118
     */
119
    protected $qrCode;
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function __call($name, $params)
125
    {
126
        return call_user_func_array([$this->qrCode, $name], $params);
127
    }
128
129
    /**
130
     * @inheritdoc
131
     * @throws InvalidPathException
132
     */
133
    public function init()
134
    {
135
        $this->qrCode = (new QrCode($this->text, $this->errorCorrectionLevel, $this->writer))
136
            ->setSize(($this->size ?: 300))
137
            ->setMargin(($this->margin ?: 10))
138
            ->setEncoding(($this->encoding ?: 'UTF-8'));
139
140
        $this->qrCode = $this->logoPath ? $this->qrCode->setLogo($this->logoPath) : $this->qrCode;
141
        $this->qrCode = $this->logoWidth ? $this->qrCode->setLogoWidth($this->logoWidth) : $this->qrCode;
142
        $this->qrCode = $this->label ? $this->qrCode->setLabel($this->label) : $this->qrCode;
143
144
        if ($this->foregroundColor) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->foregroundColor of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145
            [$r, $g, $b] = $this->foregroundColor;
0 ignored issues
show
Bug introduced by
The variable $r does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $g does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $b does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
146
            $this->qrCode = $this->qrCode->setForegroundColor($r, $g, $b);
147
        }
148
        if ($this->backgroundColor) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->backgroundColor of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
149
            [$r, $g, $b] = $this->backgroundColor;
150
            $this->qrCode = $this->qrCode->setBackgroundColor($r, $g, $b);
151
        }
152
    }
153
}
154