GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ZbarDecoder   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 6
Bugs 1 Features 3
Metric Value
wmc 17
c 6
b 1
f 3
lcom 1
cbo 5
dl 0
loc 133
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A make() 0 7 1
A getPath() 0 7 2
A setPath() 0 4 1
A getFilepath() 0 4 1
A setFilepath() 0 7 2
A buildProcess() 0 6 1
B runProcess() 0 21 5
A output() 0 4 1
1
<?php
2
3
namespace RobbieP\ZbarQrdecoder;
4
5
use RobbieP\ZbarQrdecoder\Result\ErrorResult;
6
use RobbieP\ZbarQrdecoder\Result\Result;
7
use Symfony\Component\Process\Exception\ProcessFailedException;
8
use Symfony\Component\Process\ProcessBuilder;
9
10
class ZbarDecoder {
11
12
    const EXECUTABLE = 'zbarimg';
13
14
    private $path;
15
    private $file_path;
16
    private $result;
17
    /**
18
     * @var ProcessBuilder
19
     */
20
    private $processBuilder;
21
    /**
22
     * @var array
23
     */
24
    private $config;
25
26
    /**
27
     * @param array $config
28
     * @param ProcessBuilder $processBuilder
29
     */
30
    function __construct($config = [], $processBuilder = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
    {
32
        $this->config = $config;
33
        if(isset($this->config['path'])) {
34
            $this->setPath($this->config['path']);
35
        }
36
        $this->processBuilder =  is_null($processBuilder) ? new ProcessBuilder() : $processBuilder;
37
    }
38
39
    /**
40
     * Main constructor - builds the process, runs it then returns the Result object
41
     * @param $filename
42
     * @return mixed
43
     * @throws \Exception
44
     */
45
    public function make($filename)
46
    {
47
        $this->setFilepath($filename);
48
        $this->buildProcess();
49
        $this->runProcess();
50
        return $this->output();
51
    }
52
53
    /**
54
     * Returns the path to the executable zbarimg
55
     * Defaults to /usr/bin
56
     * @throws \Exception
57
     * @return mixed
58
     */
59
    public function getPath()
60
    {
61
        if(! $this->path ) {
62
            $this->setPath('/usr/bin');
63
        }
64
        return $this->path;
65
    }
66
67
    /**
68
     * @param mixed $path
69
     */
70
    public function setPath($path)
71
    {
72
        $this->path = rtrim($path, DIRECTORY_SEPARATOR);
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public function getFilepath()
79
    {
80
        return $this->file_path;
81
    }
82
83
    /**
84
     * @param mixed $filepath
85
     * @throws \Exception
86
     */
87
    public function setFilepath($filepath)
88
    {
89
        if(! is_file($filepath) ) {
90
            throw new \Exception('Invalid filepath given');
91
        }
92
        $this->file_path = $filepath;
93
    }
94
95
    /**
96
     * Builds the process
97
     * TODO: Configurable arguments
98
     * @throws \Exception
99
     */
100
    private function buildProcess()
101
    {
102
        $path = $this->getPath();
103
        $this->processBuilder->setPrefix($path . DIRECTORY_SEPARATOR . static::EXECUTABLE);
104
        $this->processBuilder->setArguments(array('-D', '--xml', '-q', $this->getFilepath()))->enableOutput();
105
    }
106
107
    /**
108
     * Runs the process
109
     * @throws \Exception
110
     */
111
    private function runProcess()
112
    {
113
        $process = $this->processBuilder->getProcess();
114
        try {
115
            $process->mustRun();
116
            $this->result = new Result($process->getOutput());
117
        } catch (ProcessFailedException $e) {
118
            switch($e->getProcess()->getExitCode()) {
119
                case 1:
120
                    throw new \Exception('An error occurred while processing the image. It could be bad arguments, I/O errors and image handling errors from ImageMagick');
121
                case 2:
122
                    throw new \Exception('ImageMagick fatal error');
123
                case 4:
124
                    $this->result = new ErrorResult('No barcode detected');
125
                    break;
126
                default:
127
                    throw new \Exception('Problem with decode - check you have zbar-tools installed');
128
            }
129
        }
130
131
    }
132
133
    /**
134
     * Only return the output class to the end user
135
     * @return mixed
136
     */
137
    private function output()
138
    {
139
        return $this->result;
140
    }
141
142
}