Completed
Pull Request — master (#104)
by Alexis
07:51 queued 04:58
created

QRCode::getQRCodeInline()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 10
cts 20
cp 0.5
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 6
nop 6
crap 6
1
<?php
2
3
namespace PragmaRX\Google2FA\Support;
4
5
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
0 ignored issues
show
Bug introduced by
The type BaconQrCode\Renderer\Image\ImagickImageBackEnd was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use BaconQrCode\Renderer\Image\Png;
7
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
0 ignored issues
show
Bug introduced by
The type BaconQrCode\Renderer\Image\SvgImageBackEnd was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use BaconQrCode\Renderer\ImageRenderer;
0 ignored issues
show
Bug introduced by
The type BaconQrCode\Renderer\ImageRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
0 ignored issues
show
Bug introduced by
The type BaconQrCode\Renderer\RendererStyle\RendererStyle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use BaconQrCode\Writer as BaconQrCodeWriter;
11
use PragmaRX\Google2FA\Exceptions\InsecureCallException;
12
13
trait QRCode
14
{
15
    /**
16
     * Sending your secret key to Google API is a security issue. Developer must explicitly allow it.
17
     */
18
    protected $allowInsecureCallToGoogleApis = false;
19
20
    /**
21
     * Creates a Google QR code url.
22
     *
23
     * @param string $company
24
     * @param string $holder
25
     * @param string $secret
26
     * @param int    $size
27
     *
28
     * @throws InsecureCallException
29
     *
30
     * @return string
31
     */
32 2
    public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200)
33
    {
34 2
        if (!$this->allowInsecureCallToGoogleApis) {
35 1
            throw new InsecureCallException('It\'s not secure to send secret keys to Google Apis, you have to explicitly allow it by calling $google2fa->setAllowInsecureCallToGoogleApis(true).');
36
        }
37
38 1
        $url = $this->getQRCodeUrl($company, $holder, $secret);
39
40 1
        return Url::generateGoogleQRCodeUrl('https://chart.googleapis.com/', 'chart', 'chs='.$size.'x'.$size.'&chld=M|0&cht=qr&chl=', $url);
41
    }
42
43
    /**
44
     * Generates a QR code data url to display inline.
45
     *
46
     * @param string $company
47
     * @param string $holder
48
     * @param string $secret
49
     * @param int    $size
50
     * @param string $encoding Default to UTF-8
51
     * @param string $format   return format: png (base64 encoding) or svg
52
     *
53
     * @return string
54
     */
55 1
    public function getQRCodeInline($company, $holder, $secret, $size = 200, $encoding = 'utf-8', $format = 'png')
56
    {
57 1
        $url = $this->getQRCodeUrl($company, $holder, $secret);
58
59 1
        if (class_exists(Png::class)) {
60 1
            $renderer = new Png();
61 1
            $renderer->setWidth($size);
62 1
            $renderer->setHeight($size);
63
        } else {
64
            switch ($format) {
65
                case 'svg':
66
                    $backend = new SvgImageBackEnd();
67
                    break;
68
                default:
69
                    $backend = new ImagickImageBackEnd();
70
                    break;
71
            }
72
            $renderer = new ImageRenderer(
73
                new RendererStyle($size),
74
                $backend
75
            );
76
        }
77
78 1
        $bacon = new BaconQrCodeWriter($renderer);
79 1
        $data = $bacon->writeString($url, $encoding);
80
81 1
        if ($format == 'svg') {
82
            return $data;
83
        }
84
85 1
        return 'data:image/png;base64,'.base64_encode($data);
86
    }
87
88
    /**
89
     * Creates a QR code url.
90
     *
91
     * @param $company
92
     * @param $holder
93
     * @param $secret
94
     *
95
     * @return string
96
     */
97 2
    public function getQRCodeUrl($company, $holder, $secret)
98
    {
99 2
        return 'otpauth://totp/'.rawurlencode($company).':'.rawurlencode($holder).'?secret='.$secret.'&issuer='.rawurlencode($company).'';
100
    }
101
102
    /**
103
     * AllowInsecureCallToGoogleApis setter.
104
     *
105
     * @param mixed $allowInsecureCallToGoogleApis
106
     *
107
     * @return QRCode
108
     */
109 1
    public function setAllowInsecureCallToGoogleApis($allowInsecureCallToGoogleApis)
110
    {
111 1
        $this->allowInsecureCallToGoogleApis = $allowInsecureCallToGoogleApis;
112
113 1
        return $this;
114
    }
115
}
116