AbstractProxy   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 42
c 2
b 1
f 0
dl 0
loc 170
ccs 52
cts 52
cp 1
rs 10
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getPort() 0 3 1
A getConfigFile() 0 3 1
A setBinary() 0 3 1
A setIp() 0 3 1
A wait() 0 11 3
A setConfigFile() 0 7 2
A runCommand() 0 7 2
A getBinary() 0 3 1
A setPort() 0 3 1
A waitUntil() 0 13 2
A getIp() 0 3 1
A waitFor() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\HttpCache\Test\Proxy;
13
14
use Symfony\Component\Process\Process;
15
16
abstract class AbstractProxy implements ProxyInterface
17
{
18
    protected $port;
19
20
    protected $binary;
21
22
    protected $configFile;
23
24
    protected $ip = '127.0.0.1';
25
26
    /**
27
     * Wait for caching proxy to be started up and reachable.
28
     *
29
     * @param string $ip
30
     * @param int    $port
31
     * @param int    $timeout Timeout in milliseconds
32
     *
33
     * @throws \RuntimeException If proxy is not reachable within timeout
34
     */
35 31
    protected function waitFor($ip, $port, $timeout)
36
    {
37 31
        if (!$this->wait(
38 31
            $timeout,
39
            function () use ($ip, $port) {
40 28
                return false !== @fsockopen($ip, $port);
41 31
            }
42
        )) {
43 1
            throw new \RuntimeException(
44 1
                sprintf(
45 1
                    'Caching proxy cannot be reached at %s:%s',
46
                    $ip,
47
                    $port
48
                )
49
            );
50
        }
51 30
    }
52
53
    /**
54
     * Wait for caching proxy to be started up and reachable.
55
     *
56
     * @param string $ip
57
     * @param int    $port
58
     * @param int    $timeout Timeout in milliseconds
59
     *
60
     * @throws \RuntimeException If proxy is not reachable within timeout
61
     */
62 24
    protected function waitUntil($ip, $port, $timeout)
63
    {
64 24
        if (!$this->wait(
65 24
            $timeout,
66
            function () use ($ip, $port) {
67 23
                return false === @fsockopen($ip, $port);
68 24
            }
69
        )) {
70 1
            throw new \RuntimeException(
71 1
                sprintf(
72 1
                    'Caching proxy still up at %s:%s',
73
                    $ip,
74
                    $port
75
                )
76
            );
77
        }
78 23
    }
79
80 29
    protected function wait($timeout, $callback)
81
    {
82 29
        for ($i = 0; $i < $timeout; ++$i) {
83 28
            if ($callback()) {
84 28
                return true;
85
            }
86
87 21
            usleep(1000);
88
        }
89
90 1
        return false;
91
    }
92
93
    /**
94
     * Run a shell command.
95
     *
96
     * @param string $command
97
     *
98
     * @throws \RuntimeException If command execution fails
99
     */
100 29
    protected function runCommand($command, array $arguments)
101
    {
102 29
        $process = new Process(array_merge([$command], $arguments));
103 29
        $process->run();
104
105 29
        if (!$process->isSuccessful()) {
106 1
            throw new \RuntimeException($process->getErrorOutput());
107
        }
108 28
    }
109
110
    /**
111
     * @param int $port
112
     */
113 30
    public function setPort($port)
114
    {
115 30
        $this->port = $port;
116 30
    }
117
118
    /**
119
     * @return int
120
     */
121 32
    public function getPort()
122
    {
123 32
        return $this->port;
124
    }
125
126
    /**
127
     * Set Varnish binary (defaults to varnishd).
128
     *
129
     * @param string $binary
130
     */
131 4
    public function setBinary($binary)
132
    {
133 4
        $this->binary = $binary;
134 4
    }
135
136
    /**
137
     * Get Varnish binary.
138
     *
139
     * @return string
140
     */
141 33
    public function getBinary()
142
    {
143 33
        return $this->binary;
144
    }
145
146
    /**
147
     * Set IP address (defaults to 127.0.0.1).
148
     *
149
     * @param string $ip
150
     */
151 2
    public function setIp($ip)
152
    {
153 2
        $this->ip = $ip;
154 2
    }
155
156
    /**
157
     * Get IP address.
158
     *
159
     * @return string
160
     */
161 5
    public function getIp()
162
    {
163 5
        return $this->ip;
164
    }
165
166
    /**
167
     * @param string $configFile
168
     *
169
     * @throws \InvalidArgumentException
170
     */
171 31
    public function setConfigFile($configFile)
172
    {
173 31
        if (!file_exists($configFile)) {
174 1
            throw new \InvalidArgumentException('Cannot find config file: '.$configFile);
175
        }
176
177 30
        $this->configFile = $configFile;
178 30
    }
179
180
    /**
181
     * @return string
182
     */
183 31
    public function getConfigFile()
184
    {
185 31
        return $this->configFile;
186
    }
187
}
188