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 (#194)
by
unknown
10:08 queued 08:43
created

Procedure::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the php-phantomjs.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
namespace JonnyW\PhantomJs\Procedure;
10
11
use JonnyW\PhantomJs\Engine;
12
use JonnyW\PhantomJs\Cache\CacheInterface;
13
use JonnyW\PhantomJs\Parser\ParserInterface;
14
use JonnyW\PhantomJs\Template\TemplateRendererInterface;
15
use JonnyW\PhantomJs\Exception\NotWritableException;
16
use JonnyW\PhantomJs\Exception\ProcedureFailedException;
17
use JonnyW\PhantomJs\StringUtils;
18
19
/**
20
 * PHP PhantomJs
21
 *
22
 * @author Jon Wenmoth <[email protected]>
23
 */
24
class Procedure implements ProcedureInterface
25
{
26
    /**
27
     * PhantomJS engine
28
     *
29
     * @var \JonnyW\PhantomJs\Engine
30
     * @access protected
31
     */
32
    protected $engine;
33
34
    /**
35
     * Parser instance.
36
     *
37
     * @var \JonnyW\PhantomJs\Parser\ParserInterface
38
     * @access protected
39
     */
40
    protected $parser;
41
42
    /**
43
     * Cache handler instance.
44
     *
45
     * @var \JonnyW\PhantomJs\Cache\CacheInterface
46
     * @access protected
47
     */
48
    protected $cacheHandler;
49
50
    /**
51
     * Template renderer.
52
     *
53
     * @var \JonnyW\PhantomJs\Template\TemplateRendererInterface
54
     * @access protected
55
     */
56
    protected $renderer;
57
58
    /**
59
     * Procedure template.
60
     *
61
     * @var string
62
     * @access protected
63
     */
64
    protected $template;
65
66
    /**
67
     * Internal constructor.
68
     *
69
     * @access public
70
     * @param \JonnyW\PhantomJs\Engine                             $engine
71
     * @param \JonnyW\PhantomJs\Parser\ParserInterface             $parser
72
     * @param \JonnyW\PhantomJs\Cache\CacheInterface               $cacheHandler
73
     * @param \JonnyW\PhantomJs\Template\TemplateRendererInterface $renderer
74
     */
75 38
    public function __construct(Engine $engine, ParserInterface $parser, CacheInterface $cacheHandler, TemplateRendererInterface $renderer)
76
    {
77 38
        $this->engine       = $engine;
78 38
        $this->parser       = $parser;
79 38
        $this->cacheHandler = $cacheHandler;
80 38
        $this->renderer     = $renderer;
81 38
    }
82
83
    /**
84
     * Run procedure.
85
     *
86
     * @access public
87
     * @param  \JonnyW\PhantomJs\Procedure\InputInterface           $input
88
     * @param  \JonnyW\PhantomJs\Procedure\OutputInterface          $output
89
     * @throws \JonnyW\PhantomJs\Exception\ProcedureFailedException
90
     * @throws \JonnyW\PhantomJs\Exception\NotWritableException
91
     * @return void
92
     */
93 38
    public function run(InputInterface $input, OutputInterface $output)
94
    {
95
        try {
96
97 38
            $executable = $this->write(
98 38
                $this->compile($input)
99
            );
100
101
            $descriptorspec = array(
102 38
                array('pipe', 'r'),
103
                array('pipe', 'w'),
104
                array('pipe', 'w')
105
            );
106
107 38
            $process = proc_open(escapeshellcmd(sprintf('%s %s', $this->engine->getCommand(), $executable)), $descriptorspec, $pipes, null, null);
108
109 38
            if (!is_resource($process)) {
110
                throw new ProcedureFailedException('proc_open() did not return a resource');
111
            }
112
113 38
            $result = stream_get_contents($pipes[1]);
114 38
            $log    = stream_get_contents($pipes[2]);
115
116 38
            fclose($pipes[0]);
117 38
            fclose($pipes[1]);
118 38
            fclose($pipes[2]);
119
120 38
            proc_close($process);
121
122 38
            $output->import(
123 38
                $this->parser->parse($result)
124
            );
125
126 38
            $this->engine->log($log);
127
128 38
            $this->remove($executable);
129
130
        } catch (NotWritableException $e) {
131
            throw $e;
132
        } catch (\Exception $e) {
133
134
            if (isset($executable)) {
135
                $this->remove($executable);
136
            }
137
138
            throw new ProcedureFailedException(sprintf('Error when executing PhantomJs procedure - %s', $e->getMessage()));
139
        }
140 38
    }
141
142
    /**
143
     * Set procedure template.
144
     *
145
     * @access public
146
     * @param  string                                $template
147
     * @return \JonnyW\PhantomJs\Procedure\Procedure
148
     */
149 38
    public function setTemplate($template)
150
    {
151 38
        $this->template = $template;
152
153 38
        return $this;
154
    }
155
156
    /**
157
     * Get procedure template.
158
     *
159
     * @access public
160
     * @return string
161
     */
162 38
    public function getTemplate()
163
    {
164 38
        return $this->template;
165
    }
166
167
    /**
168
     * Compile procedure.
169
     *
170
     * @access public
171
     * @param  \JonnyW\PhantomJs\Procedure\InputInterface $input
172
     * @return void
173
     */
174 38
    public function compile(InputInterface $input)
175
    {
176 38
       return $this->renderer->render($this->getTemplate(), array('input' => $input));
177
    }
178
179
    /**
180
     * Write compiled procedure to cache.
181
     *
182
     * @access protected
183
     * @param  string $compiled
184
     * @return string
185
     */
186 38
    protected function write($compiled)
187
    {
188 38
        return $this->cacheHandler->save(StringUtils::random(20), $compiled);
189
    }
190
191
    /**
192
     * Remove procedure script cache.
193
     *
194
     * @access protected
195
     * @param  string $filePath
196
     * @return void
197
     */
198 38
    protected function remove($filePath)
199
    {
200 38
        $this->cacheHandler->delete($filePath);
201 38
    }
202
}
203