Completed
Pull Request — master (#332)
by André
03:21
created

SymfonyProxy::clear()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
crap 3
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
 * Controls the Symfony HttpCache proxy server.
16
 *
17
 * SYMFONY_CACHE_DIR directory to use for cache
18
 *                   (default sys_get_temp_dir() + '/foshttpcache-symfony')
19
 */
20
class SymfonyProxy implements ProxyInterface
21
{
22
    /**
23
     * Get Symfony cache directory.
24
     *
25
     * @return string
26
     */
27 7
    public function getCacheDir()
28
    {
29 7
        $path = defined('SYMFONY_CACHE_DIR') ? SYMFONY_CACHE_DIR : sys_get_temp_dir().'/foshttpcache-symfony';
30 7
        if (!$path || '/' === $path) {
31 1
            throw new \RuntimeException('Invalid test setup, the cache dir is '.$path);
32
        }
33
34 6
        return $path;
35
    }
36
37
    /**
38
     * Start the proxy server.
39
     */
40 1
    public function start()
41
    {
42 1
        $this->clear();
43 1
    }
44
45
    /**
46
     * Stop the proxy server.
47
     */
48 1
    public function stop()
49
    {
50
        // nothing to do
51 1
    }
52
53
    /**
54
     * Clear all cached content from the proxy server.
55
     */
56 6
    public function clear()
57
    {
58 6
        $path = realpath($this->getCacheDir());
59
60
        // false means the directory does not exist yet - it surely is empty then
61 6
        if (!is_dir($path)) {
62 1
            return;
63
        }
64
65 5
        $path = $this->getCacheDir();
66 5
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
67
            // @codeCoverageIgnoreStart
68
            system('DEL /S '.$path);
69
        } else {
70
            // @codeCoverageIgnoreEnd
71 5
            system('rm -r '.$path);
72
        }
73 5
    }
74
}
75