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