QRencode::factory()   C
last analyzed

Complexity

Conditions 13
Paths 13

Size

Total Lines 32
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 27
nc 13
nop 3
dl 0
loc 32
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
* PHP QR Code encoder
4
*
5
* Main encoder classes.
6
*
7
* Based on libqrencode C library distributed under LGPL 2.1
8
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <[email protected]>
9
*
10
* PHP QR Code is distributed under LGPL 3
11
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
12
*
13
* This library is free software; you can redistribute it and/or
14
* modify it under the terms of the GNU Lesser General Public
15
* License as published by the Free Software Foundation; either
16
* version 3 of the License, or any later version.
17
*
18
* This library is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
* Lesser General Public License for more details.
22
*
23
* You should have received a copy of the GNU Lesser General Public
24
* License along with this library; if not, write to the Free Software
25
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
*/
27
namespace tinymeng\code\Gateways\qrcode;
28
29
class QRencode {
30
31
    public $casesensitive = true;
32
    public $eightbit = false;
33
    
34
    public $version = 0;
35
    public $size = 3;
36
    public $margin = 4;
37
    
38
    public $structured = 0; // not supported yet
39
    
40
    public $level = QR_ECLEVEL_L;
41
    public $hint = QR_MODE_8;
42
    
43
    //----------------------------------------------------------------------
44
    public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4)
45
    {
46
        $enc = new QRencode();
47
        $enc->size = $size;
48
        $enc->margin = $margin;
49
        
50
        switch ($level.'') {
51
            case '0':
52
            case '1':
53
            case '2':
54
            case '3':
55
                    $enc->level = $level;
56
                break;
57
            case 'l':
58
            case 'L':
59
                    $enc->level = QR_ECLEVEL_L;
60
                break;
61
            case 'm':
62
            case 'M':
63
                    $enc->level = QR_ECLEVEL_M;
64
                break;
65
            case 'q':
66
            case 'Q':
67
                    $enc->level = QR_ECLEVEL_Q;
68
                break;
69
            case 'h':
70
            case 'H':
71
                    $enc->level = QR_ECLEVEL_H;
72
                break;
73
        }
74
        
75
        return $enc;
76
    }
77
    
78
    //----------------------------------------------------------------------
79
    public function encodeRAW($intext, $outfile = false) 
0 ignored issues
show
Unused Code introduced by
The parameter $outfile is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
    public function encodeRAW($intext, /** @scrutinizer ignore-unused */ $outfile = false) 

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        $code = new Qrcode();
82
83
        if($this->eightbit) {
84
            $code->encodeString8bit($intext, $this->version, $this->level);
85
        } else {
86
            $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
87
        }
88
        
89
        return $code->data;
90
    }
91
92
    //----------------------------------------------------------------------
93
    public function encode($intext, $outfile = false) 
94
    {
95
        $code = new Qrcode();
96
97
        if($this->eightbit) {
98
            $code->encodeString8bit($intext, $this->version, $this->level);
99
        } else {
100
            $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
101
        }
102
        
103
        QRtools::markTime('after_encode');
104
        
105
        if ($outfile!== false) {
106
            file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
107
        } else {
108
            return QRtools::binarize($code->data);
109
        }
110
    }
111
    
112
    //----------------------------------------------------------------------
113
    public function encodePNG($intext, $outfile = false,$saveandprint=false) 
114
    {
115
        try {
116
        
117
            ob_start();
118
            $tab = $this->encode($intext);
119
            $err = ob_get_contents();
120
            ob_end_clean();
121
            
122
            if ($err != '')
123
                QRtools::log($outfile, $err);
124
            
125
            $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
126
            
127
            QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
128
        
129
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type tinymeng\code\Gateways\qrcode\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
130
        
131
            QRtools::log($outfile, $e->getMessage());
132
        
133
        }
134
    }
135
}
136