Completed
Push — 1.x ( 2f995a...fa8c59 )
by Samuel
18:04
created

FastCGI::doRun()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 15
rs 9.4285
nc 2
cc 2
eloc 8
nop 1
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Adapter;
13
14
use CacheTool\Code;
15
use Adoy\FastCGI\Client;
16
17
class FastCGI extends AbstractAdapter
18
{
19
    /**
20
     * @var Client
21
     */
22
    protected $client;
23
24
    /**
25
     * @var string
26
     */
27
    protected $host;
28
29
    /**
30
     * @param string $host 127.0.0.1:9000 or /var/run/php5-fpm.sock
31
     * @param string $tempDir
32
     */
33
    public function __construct($host = null, $tempDir = null)
0 ignored issues
show
Unused Code introduced by
The parameter $tempDir is not used and could be removed.

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

Loading history...
34
    {
35
        // try to guess where it is
36
        if ($host === null) {
37
            if (file_exists('/var/run/php5-fpm.sock')) {
38
                $host = '/var/run/php5-fpm.sock';
39
            } else {
40
                $host = '127.0.0.1:9000';
41
            }
42
        }
43
44
        $this->host = $host;
45
46
        if (false !== strpos($host, ':')) {
47
            list($host, $port) = explode(':', $host);
48
            $this->client = new Client($host, $port);
49
        } else {
50
            // socket
51
            $this->client = new Client('unix://' . $host, -1);
52
        }
53
54
        $this->client->setReadWriteTimeout(60 * 1000);
55
        $this->client->setPersistentSocket(false);
56
        $this->client->setKeepAlive(true);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function doRun(Code $code)
63
    {
64
        $response = $this->request($code);
65
        $parts = explode("\r\n\r\n", $response);
66
67
        // remove headers
68
        array_shift($parts);
69
        $body = implode("\r\n\r\n", $parts);
70
71
        if (@unserialize($body) === false) {
72
            throw new \RuntimeException(sprintf("Error: %s", $response));
73
        }
74
75
        return $body;
76
    }
77
78
    protected function request(Code $code)
79
    {
80
        $file = $this->createTemporaryFile();
81
82
        $this->logger->info(sprintf('FastCGI: Dumped code to file: %s', $file));
83
84
        try {
85
            $code->writeTo($file);
86
87
            $environment = array(
88
                'REQUEST_METHOD'  => 'POST',
89
                'REQUEST_URI'     => '/',
90
                'SCRIPT_FILENAME' => $file,
91
            );
92
93
            $response = $this->client->request($environment, '');
94
            $this->logger->debug(sprintf('FastCGI: Response: %s', json_encode($response)));
95
96
            @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
            return $response;
98
        } catch (\Exception $e) {
99
            @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
100
101
            throw new \RuntimeException(
102
                sprintf('FastCGI error: %s (%s)', $e->getMessage(), $this->host),
103
                $e->getCode(),
104
                $e
105
            );
106
        }
107
    }
108
}
109