Completed
Push — master ( 17f397...dedab2 )
by Samuel
13:14 queued 11:43
created

FastCGI   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 39.58%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 1
cbo 4
dl 0
loc 105
ccs 19
cts 48
cp 0.3958
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 30 6
A doRun() 0 15 2
B request() 0 30 2
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 Array of patterns matching php socket files
26
     */
27
    protected $possibleSocketFilePatterns = [
28
        '/var/run/php*.sock',
29
        '/var/run/php/*.sock'
30
    ];
31
32
    /**
33
     * @var string
34
     */
35
    protected $host;
36
37
    /**
38
     * @param string $host 127.0.0.1:9000 or /var/run/php5-fpm.sock
39
     * @param string $tempDir
40
     */
41 3
    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...
42
    {
43
        // try to guess where it is
44 3
        if ($host === null) {
45 2
            foreach ($this->possibleSocketFilePatterns as $possibleSocketFilePattern) {
46 2
                $possibleSocketFile = current(glob($possibleSocketFilePattern));
47 2
                if (file_exists($possibleSocketFile)) {
48
                    $host = $possibleSocketFile;
49
                    break;
50
                }
51 2
            }
52 2
            if ($host === null) {
53 2
                $host = '127.0.0.1:9000';
54 2
            }
55 2
        }
56
57 3
        $this->host = $host;
58
59 3
        if (false !== strpos($host, ':')) {
60 3
            list($host, $port) = explode(':', $host);
61 3
            $this->client = new Client($host, $port);
62 3
        } else {
63
            // socket
64
            $this->client = new Client('unix://' . $host, -1);
65
        }
66
67 3
        $this->client->setReadWriteTimeout(60 * 1000);
68 3
        $this->client->setPersistentSocket(false);
69 3
        $this->client->setKeepAlive(true);
70 3
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function doRun(Code $code)
76
    {
77
        $response = $this->request($code);
78
        $parts = explode("\r\n\r\n", $response);
79
80
        // remove headers
81
        array_shift($parts);
82
        $body = implode("\r\n\r\n", $parts);
83
84
        if (@unserialize($body) === false) {
85
            throw new \RuntimeException(sprintf("Error: %s", $response));
86
        }
87
88
        return $body;
89
    }
90
91
    protected function request(Code $code)
92
    {
93
        $file = $this->createTemporaryFile();
94
95
        $this->logger->info(sprintf('FastCGI: Dumped code to file: %s', $file));
96
97
        try {
98
            $code->writeTo($file);
99
100
            $environment = array(
101
                'REQUEST_METHOD'  => 'POST',
102
                'REQUEST_URI'     => '/',
103
                'SCRIPT_FILENAME' => $file,
104
            );
105
106
            $response = $this->client->request($environment, '');
107
            $this->logger->debug(sprintf('FastCGI: Response: %s', json_encode($response)));
108
109
            @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...
110
            return $response;
111
        } catch (\Exception $e) {
112
            @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...
113
114
            throw new \RuntimeException(
115
                sprintf('FastCGI error: %s (%s)', $e->getMessage(), $this->host),
116
                $e->getCode(),
117
                $e
118
            );
119
        }
120
    }
121
}
122