Completed
Pull Request — master (#316)
by David
17:11 queued 14:25
created

AbstractProxy::setPort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 32 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 32
        if (!$this->wait(
35 32
            $timeout,
36
            function () use ($ip, $port) {
37 29
                return false !== @fsockopen($ip, $port);
38
            }
39 32
        )) {
40 1
            throw new \RuntimeException(
41 1
                sprintf(
42 1
                    'Caching proxy cannot be reached at %s:%s',
43 1
                    $ip,
44
                    $port
45 1
                )
46 1
            );
47
        }
48 31
    }
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 24
            $timeout,
63 23
            function () use ($ip, $port) {
64 23
                return false === @fsockopen($ip, $port);
65
            }
66 24
        )) {
67 1
            throw new \RuntimeException(
68 1
                sprintf(
69 1
                    'Caching proxy still up at %s:%s',
70 1
                    $ip,
71
                    $port
72 1
                )
73 1
            );
74
        }
75 23
    }
76
77 30
    protected function wait($timeout, $callback)
78
    {
79 30
        for ($i = 0; $i < $timeout; ++$i) {
80 29
            if ($callback()) {
81 29
                return true;
82
            }
83
84 23
            usleep(1000);
85 23
        }
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 30
    protected function runCommand($command, array $arguments)
99
    {
100 30
        $builder = new ProcessBuilder($arguments);
101 30
        $builder->setPrefix($command);
102
103 30
        $process = $builder->getProcess();
104 30
        $process->run();
105
106 30
        if (!$process->isSuccessful()) {
107 1
            throw new \RuntimeException($process->getErrorOutput());
108
        }
109 29
    }
110
111
    /**
112
     * @param int $port
113
     */
114 31
    public function setPort($port)
115
    {
116 31
        $this->port = $port;
117 31
    }
118
119
    /**
120
     * @return int
121
     */
122 33
    public function getPort()
123
    {
124 33
        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 34
    public function getBinary()
143
    {
144 34
        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 6
    public function getIp()
163
    {
164 6
        return $this->ip;
165
    }
166
167
    /**
168
     * @param string $configFile
169
     *
170
     * @throws \InvalidArgumentException
171
     */
172 32
    public function setConfigFile($configFile)
173
    {
174 32
        if (!file_exists($configFile)) {
175 1
            throw new \InvalidArgumentException('Cannot find config file: '.$configFile);
176
        }
177
178 31
        $this->configFile = $configFile;
179 31
    }
180
181
    /**
182
     * @return string
183
     */
184 32
    public function getConfigFile()
185
    {
186 32
        return $this->configFile;
187
    }
188
}
189