QrCodeAction::run()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 12
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\Action;
13
14
use Da\QrCode\Component\QrCodeComponent;
15
use Yii;
16
use yii\base\Action;
17
use yii\web\Response;
18
19
class QrCodeAction extends Action
20
{
21
    /**
22
     * @var string the text to render if there are no parameter. Defaults to null, which means the component should
23
     *             render the text given as a parameter.
24
     */
25
    public $text;
26
    /**
27
     * @var string the parameter
28
     */
29
    public $param = 'text';
30
    /**
31
     * @var string whether the URL parameter is passed via GET or POST. Defaults to 'get'.
32
     */
33
    public $method = 'get';
34
    /**
35
     * @var string the qr component name configured on the Yii2 app. The component should have configured all the
36
     *             possible options like adding a logo, styling, labelling, etc.
37
     */
38
    public $component = 'qr';
39
40
    /**
41
     * Runs the action.
42
     */
43
    public function run()
44
    {
45
        $text = call_user_func([Yii::$app->request, $this->method], $this->param, $this->text);
46
47
        $qr = Yii::$app->get($this->component);
48
49
        if ($text && $qr instanceof QrCodeComponent) {
50
            Yii::$app->response->format = Response::FORMAT_RAW;
51
            Yii::$app->response->headers->add('Content-Type', $qr->getContentType());
52
53
            return $qr->setText((string)$text)->writeString();
54
        }
55
    }
56
}
57