Completed
Pull Request — master (#361)
by David
03:30
created

AbstractProxy::getConfigFile()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\ProcessBuilder;
15
16
abstract class AbstractProxy implements ProxyInterface
17
{
18
    protected $port;
19
    protected $binary;
20
    protected $configFile;
21
    protected $ip = '127.0.0.1';
22
23
    /**
24
     * Wait for caching proxy to be started up and reachable.
25
     *
26
     * @param string $ip
27
     * @param int    $port
28
     * @param int    $timeout Timeout in milliseconds
29
     *
30
     * @throws \RuntimeException If proxy is not reachable within timeout
31
     */
32 31 View Code Duplication
    protected function waitFor($ip, $port, $timeout)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34 31
        if (!$this->wait(
35
            $timeout,
36
            function () use ($ip, $port) {
37 28
                return false !== @fsockopen($ip, $port);
38 31
            }
39
        )) {
40 1
            throw new \RuntimeException(
41 1
                sprintf(
42 1
                    'Caching proxy cannot be reached at %s:%s',
43
                    $ip,
44 1
                    $port
45
                )
46
            );
47
        }
48 30
    }
49
50
    /**
51
     * Wait for caching proxy to be started up and reachable.
52
     *
53
     * @param string $ip
54
     * @param int    $port
55
     * @param int    $timeout Timeout in milliseconds
56
     *
57
     * @throws \RuntimeException If proxy is not reachable within timeout
58
     */
59 24 View Code Duplication
    protected function waitUntil($ip, $port, $timeout)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61 24
        if (!$this->wait(
62
            $timeout,
63 24
            function () use ($ip, $port) {
64 23
                return false === @fsockopen($ip, $port);
65 24
            }
66
        )) {
67 1
            throw new \RuntimeException(
68 1
                sprintf(
69 1
                    'Caching proxy still up at %s:%s',
70
                    $ip,
71 1
                    $port
72
                )
73
            );
74
        }
75 23
    }
76
77 29
    protected function wait($timeout, $callback)
78
    {
79 29
        for ($i = 0; $i < $timeout; ++$i) {
80 28
            if ($callback()) {
81 28
                return true;
82
            }
83
84 23
            usleep(1000);
85
        }
86
87 1
        return false;
88
    }
89
90
    /**
91
     * Run a shell command.
92
     *
93
     * @param string $command
94
     * @param array  $arguments
95
     *
96
     * @throws \RuntimeException If command execution fails
97
     */
98 29
    protected function runCommand($command, array $arguments)
99
    {
100 29
        $builder = new ProcessBuilder($arguments);
101 29
        $builder->setPrefix($command);
102
103 29
        $process = $builder->getProcess();
104 29
        $process->run();
105
106 29
        if (!$process->isSuccessful()) {
107 1
            throw new \RuntimeException($process->getErrorOutput());
108
        }
109 28
    }
110
111
    /**
112
     * @param int $port
113
     */
114 30
    public function setPort($port)
115
    {
116 30
        $this->port = $port;
117 30
    }
118
119
    /**
120
     * @return int
121
     */
122 32
    public function getPort()
123
    {
124 32
        return $this->port;
125
    }
126
127
    /**
128
     * Set Varnish binary (defaults to varnishd).
129
     *
130
     * @param string $binary
131
     */
132 4
    public function setBinary($binary)
133
    {
134 4
        $this->binary = $binary;
135 4
    }
136
137
    /**
138
     * Get Varnish binary.
139
     *
140
     * @return string
141
     */
142 33
    public function getBinary()
143
    {
144 33
        return $this->binary;
145
    }
146
147
    /**
148
     * Set IP address (defaults to 127.0.0.1).
149
     *
150
     * @param string $ip
151
     */
152 2
    public function setIp($ip)
153
    {
154 2
        $this->ip = $ip;
155 2
    }
156
157
    /**
158
     * Get IP address.
159
     *
160
     * @return string
161
     */
162 5
    public function getIp()
163
    {
164 5
        return $this->ip;
165
    }
166
167
    /**
168
     * @param string $configFile
169
     *
170
     * @throws \InvalidArgumentException
171
     */
172 31
    public function setConfigFile($configFile)
173
    {
174 31
        if (!file_exists($configFile)) {
175 1
            throw new \InvalidArgumentException('Cannot find config file: '.$configFile);
176
        }
177
178 30
        $this->configFile = $configFile;
179 30
    }
180
181
    /**
182
     * @return string
183
     */
184 31
    public function getConfigFile()
185
    {
186 31
        return $this->configFile;
187
    }
188
}
189