Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

CacheFactory::createCacheProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 8
rs 9.4285
1
<?php namespace CMSFactory\Services\Cache;
2
3
use Doctrine\Common\Cache\Cache;
4
use Memcache;
5
use Memcached;
6
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
8
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
9
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
10
11
class CacheFactory
12
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
13
    use ContainerAwareTrait;
14
15
    /**
16
     * @return Cache
0 ignored issues
show
Documentation introduced by
Should the return type not be Cache|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
17
     * @throws ServiceNotFoundException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
18
     * @throws InvalidArgumentException
19
     * @throws ServiceCircularReferenceException
20
     */
21
    public function createCacheProvider() {
22
23
        foreach ($this->container->getParameter('cache.provider.priority') as $provider) {
24
            if ($this->checkRequirements($provider)) {
25
                return $this->container->get($provider);
26
            }
27
        }
28
    }
29
30
    /**
31
     * @param string $provider
32
     * @return bool
33
     * @throws ServiceNotFoundException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
34
     * @throws ServiceCircularReferenceException
35
     * @throws InvalidArgumentException
36
     */
37
    private function checkRequirements($provider) {
38
39
        switch ($provider) {
40
            case 'cache.provider.memcache':
41
                return $this->checkMemcache();
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
42
            case 'cache.provider.memcached':
43
                return $this->checkMemcached();
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
44
            case 'cache.provider.filesystem':
45
                return $this->checkFilesystem();
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
46
            case 'cache.provider.apc':
47
                return $this->checkApc();
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
48
            default:
49
                return false;
50
51
        }
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    private function checkApc() {
58
59
        return extension_loaded('apc') && ini_get('apc.enabled');
60
    }
61
62
    /**
63
     * @return bool
64
     * @throws ServiceNotFoundException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
65
     * @throws InvalidArgumentException
66
     * @throws ServiceCircularReferenceException
67
     */
68
    private function checkMemcache() {
69
70
        /** @var Memcache $memcache */
71
        $memcache = $this->container->get('memcache');
72
        $host = $this->container->getParameter('memcached.host');
73
        return extension_loaded('memcache') && class_exists('Memcache') && $memcache->getserverstatus($host) !== 0;
74
    }
75
76
    /**
77
     * @return bool
78
     * @throws ServiceNotFoundException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
79
     * @throws ServiceCircularReferenceException
80
     * @throws InvalidArgumentException
81
     */
82
    private function checkMemcached() {
83
84
        /** @var Memcached $memcached */
85
        $memcached = $this->container->get('memcached');
86
        $host = $this->container->getParameter('memcached.host');
87
        $port = $this->container->getParameter('memcached.port');
88
89
        $server = "{$host}:{$port}";
90
        $stats = $memcached->getStats();
91
        $connected = array_key_exists($server, $stats) && $stats[$server]['pid'] > 0;
92
93
        return extension_loaded('memcached') && class_exists('Memcached') && $connected;
94
    }
95
96
    /**
97
     * @return bool
98
     * @throws InvalidArgumentException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
99
     */
100
    private function checkFilesystem() {
101
102
        $cacheDirectory = $this->container->getParameter('cache.directory');
103
        return file_exists($cacheDirectory) && is_writable($cacheDirectory);
104
105
    }
106
107
}