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.
Completed
Pull Request — master (#10)
by
unknown
03:59
created

ZbarDecoder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 152
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A make() 0 8 1
A getPath() 0 8 2
A setPath() 0 4 1
A getFilePath() 0 4 1
A setFilePath() 0 7 2
A buildProcess() 0 12 1
B runProcess() 0 26 6
A output() 0 4 1
1
<?php
2
3
namespace RobbieP\ZbarQrdecoder;
4
5
use RobbieP\ZbarQrdecoder\Result\AbstractResult;
6
use RobbieP\ZbarQrdecoder\Result\ErrorResult;
7
use RobbieP\ZbarQrdecoder\Result\Parser\ParserXML;
8
use RobbieP\ZbarQrdecoder\Result\ResultCollection;
9
use Symfony\Component\Process\Exception\ProcessFailedException;
10
use Symfony\Component\Process\Process;
11
use Symfony\Component\Process\ProcessBuilder;
12
13
class ZbarDecoder
14
{
15
    const EXECUTABLE = 'zbarimg';
16
17
    private $path;
18
    private $filePath;
19
20
    /**
21
     * @var AbstractResult|ResultCollection
22
     */
23
    private $result;
24
    /**
25
     * @var Process
26
     */
27
    private $process;
28
29
    /**
30
     * @param array $config
31
     * @param Process $process
32
     * @throws \Exception
33
     */
34
    public function __construct(array $config = [], Process $process = null)
35
    {
36
        if (isset($config['path'])) {
37
            $this->setPath($config['path']);
38
        }
39
        $this->process = null === $process ? new Process($this->getPath()) : $process;
40
    }
41
42
    /**
43
     * Main constructor - builds the process, runs it then returns the Result object
44
     *
45
     * @param $filename
46
     *
47
     * @return AbstractResult|ResultCollection
48
     * @throws \Exception
49
     */
50
    public function make($filename)
51
    {
52
        $this->setFilePath($filename);
53
        $this->buildProcess();
54
        $this->runProcess();
55
56
        return $this->output();
57
    }
58
59
    /**
60
     * Returns the path to the executable zbarimg
61
     * Defaults to /usr/bin
62
     *
63
     * @throws \Exception
64
     * @return mixed
65
     */
66
    public function getPath()
67
    {
68
        if (!$this->path) {
69
            $this->setPath('/usr/bin');
70
        }
71
72
        return $this->path;
73
    }
74
75
    /**
76
     * @param mixed $path
77
     */
78
    public function setPath($path)
79
    {
80
        $this->path = rtrim($path, '/');
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86
    public function getFilePath()
87
    {
88
        return $this->filePath;
89
    }
90
91
    /**
92
     * @param mixed $filePath
93
     *
94
     * @throws \Exception
95
     */
96
    public function setFilePath($filePath)
97
    {
98
        if (!is_file($filePath)) {
99
            throw new \RuntimeException('Invalid filepath given');
100
        }
101
        $this->filePath = $filePath;
102
    }
103
104
    /**
105
     * Builds the process
106
     * TODO: Configurable arguments
107
     *
108
     * @throws \Exception
109
     */
110
    private function buildProcess()
111
    {
112
        $path = $this->getPath();
113
        $this->process->setCommandLine([
114
            $path . DIRECTORY_SEPARATOR . static::EXECUTABLE,
115
            '-D',
116
            '--xml',
117
            '-q',
118
            $this->getFilePath()
119
        ])->enableOutput();
120
121
    }
122
123
    /**
124
     * Runs the process
125
     *
126
     * @throws \RuntimeException
127
     */
128
    private function runProcess()
129
    {
130
        $process = $this->process;
131
        try {
132
            $process->mustRun();
133
            $parser = new ParserXML();
134
            $result = $parser->parse($process->getOutput());
135
            if (count($result) === 1) {
136
                $result = $result->getResults()[0];
137
            }
138
            $this->result = $result;
139
        } catch (ProcessFailedException $e) {
140
            switch ($e->getProcess()->getExitCode()) {
141
                case 1:
142
                    throw new \RuntimeException('An error occurred while processing the image. It could be bad arguments, I/O errors and image handling errors from ImageMagick');
143
                case 2:
144
                    throw new \RuntimeException('ImageMagick fatal error');
145
                case 4:
146
                    $this->result = new ErrorResult('No barcode detected');
147
                    break;
148
                default:
149
                    throw new \RuntimeException('Problem with decode - check you have zbar-tools installed');
150
            }
151
        }
152
153
    }
154
155
    /**
156
     * Only return the output class to the end user
157
     *
158
     * @return AbstractResult|ResultCollection
159
     */
160
    private function output()
161
    {
162
        return $this->result;
163
    }
164
}
165