Completed
Pull Request — master (#364)
by Alessandro
04:31
created

VarnishProxy::stop()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 3.0416
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
/**
15
 * {@inheritdoc}
16
 */
17
class VarnishProxy extends AbstractProxy
18
{
19
    protected $binary = 'varnishd';
20
    protected $port = 6181;
21
    protected $managementPort = 6182;
22
    protected $pid = '/tmp/foshttpcache-varnish.pid';
23
    protected $configFile;
24
    protected $configDir;
25
    protected $cacheDir;
26
    protected $allowInlineC = false;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param string $configFile Path to VCL file
32
     */
33 28
    public function __construct($configFile)
34
    {
35 28
        $this->setConfigFile($configFile);
36 27
        $this->setCacheDir(sys_get_temp_dir().DIRECTORY_SEPARATOR.'foshttpcache-varnish');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 26
    public function start()
43
    {
44
        $args = [
45 26
            '-a', $this->ip.':'.$this->getPort(),
46 26
            '-T', $this->ip.':'.$this->getManagementPort(),
47 26
            '-f', $this->getConfigFile(),
48 26
            '-n', $this->getCacheDir(),
49 26
            '-p', 'vcl_dir='.$this->getConfigDir(),
50
51 26
            '-P', $this->pid,
52
        ];
53 26
        if ($this->getAllowInlineC()) {
54 2
            $args[] = '-p';
55 2
            $args[] = 'vcc_allow_inline_c=on';
56
        }
57
58 26
        $this->runCommand($this->getBinary(), $args);
59
60 26
        $this->waitFor($this->ip, $this->getPort(), 5000);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 24
    public function stop()
67
    {
68 24
        if (file_exists($this->pid)) {
69
            try {
70 23
                $this->runCommand('kill', ['-9', trim(file_get_contents($this->pid))]);
71
            } catch (\RuntimeException $e) {
72
                // Ignore if command fails when Varnish wasn't running
73
            }
74 23
            unlink($this->pid);
75 23
            $this->waitUntil($this->ip, $this->getPort(), 5000);
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 23
    public function clear()
83
    {
84 23
        $this->stop();
85 23
        $this->start();
86
    }
87
88
    /**
89
     * @param string $configDir
90
     */
91 2
    public function setConfigDir($configDir)
92
    {
93 2
        $this->configDir = $configDir;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 26
    public function getConfigDir()
100
    {
101 26
        if (null === $this->configDir && null !== $this->configFile) {
102 24
            return dirname(realpath($this->getConfigFile()));
103
        }
104
105 2
        return $this->configDir;
106
    }
107
108
    /**
109
     * Set Varnish management port (defaults to 6182).
110
     *
111
     * @param int $managementPort
112
     */
113 3
    public function setManagementPort($managementPort)
114
    {
115 3
        $this->managementPort = $managementPort;
116
    }
117
118
    /**
119
     * Get Varnish management port.
120
     *
121
     * @return int
122
     */
123 27
    public function getManagementPort()
124
    {
125 27
        return $this->managementPort;
126
    }
127
128
    /**
129
     * Set Varnish cache directory.
130
     *
131
     * @param string $cacheDir
132
     */
133 27
    public function setCacheDir($cacheDir)
134
    {
135 27
        $this->cacheDir = $cacheDir;
136
    }
137
138
    /**
139
     * Get Varnish cache directory.
140
     *
141
     * @return string
142
     */
143 27
    public function getCacheDir()
144
    {
145 27
        return $this->cacheDir;
146
    }
147
148
    /**
149
     * Whether the inline C flag should be set.
150
     *
151
     * @return bool
152
     */
153 26
    public function getAllowInlineC()
154
    {
155 26
        return $this->allowInlineC;
156
    }
157
158
    /**
159
     * Set whether the inline c flag should be on or off.
160
     *
161
     * @param bool $allowInlineC True for on, false for off
162
     */
163 3
    public function setAllowInlineC($allowInlineC)
164
    {
165 3
        $this->allowInlineC = (bool) $allowInlineC;
166
    }
167
}
168