Passed
Push — master ( 319fcb...7a107a )
by Rafael
04:41
created

DefinitionCacheWarmer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 103
c 0
b 0
f 0
ccs 6
cts 39
cp 0.1538
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getControlFileName() 0 3 1
A updateControlFile() 0 3 1
A isOptional() 0 3 1
B isFreshCache() 0 30 4
A warmUp() 0 4 1
A getSubscribedEvents() 0 4 1
A warmUpOnEveryRequest() 0 5 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Cache;
12
13
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
16
use Symfony\Component\HttpKernel\Kernel;
17
use Symfony\Component\HttpKernel\KernelEvents;
18
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
19
20
/**
21
 * DefinitionCacheWarmer
22
 */
23
class DefinitionCacheWarmer extends CacheWarmer implements EventSubscriberInterface
24
{
25
    /**
26
     * @var DefinitionRegistry
27
     */
28
    protected $registry;
29
30
    /**
31
     * @var Kernel
32
     */
33
    protected $kernel;
34
35
    /**
36
     * DefinitionCacheWarmer constructor.
37
     *
38
     * @param Kernel             $kernel
39
     * @param DefinitionRegistry $registry
40
     */
41 1
    public function __construct(Kernel $kernel, DefinitionRegistry $registry)
42
    {
43 1
        $this->kernel = $kernel;
44 1
        $this->registry = $registry;
45 1
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 1
    public function isOptional()
51
    {
52 1
        return true;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function warmUp($cacheDir)
59
    {
60
        $this->registry->clearCache();
61
        $this->updateControlFile();
62
    }
63
64
    /**
65
     * warmUp the cache on request
66
     * NOTE: this behavior its switched in the YnloGraphQLExtension
67
     */
68
    public function warmUpOnEveryRequest()
69
    {
70
        if (!$this->isFreshCache()) {
71
            $this->warmUp(null);
72
            $this->updateControlFile();
73
        }
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public static function getSubscribedEvents()
80
    {
81
        return [
82
            KernelEvents::REQUEST => 'warmUpOnEveryRequest',
83
        ];
84
    }
85
86
    protected function isFreshCache()
87
    {
88
        if (!file_exists($this->getControlFileName())) {
89
            return false;
90
        }
91
92
        $controlTime = filemtime($this->getControlFileName());
93
94
        $projectDir = $this->kernel->getProjectDir();
95
96
        if (Kernel::VERSION_ID >= 40000) {
97
            $dirs[] = $projectDir.'/config';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dirs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dirs = array(); before regardless.
Loading history...
98
            $dirs[] = $projectDir.'/src';
99
        } else {
100
            $dirs[] = $this->kernel->getRootDir();
101
            $dirs[] = $this->kernel->getRootDir().'/../src';
102
        }
103
104
        $files = Finder::create()
105
                       ->in($dirs[1])
106
                       ->date(sprintf('>= %s', date('Y-m-d H:i:s', $controlTime)))
107
                       ->files();
108
109
        //exist at least one modified file
110
        foreach ($files as $file) {
111
            return false;
112
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
113
        }
114
115
        return true;
116
    }
117
118
    protected function getControlFileName()
119
    {
120
        return $this->kernel->getCacheDir().'/graphal.schema.timestamp';
121
    }
122
123
    protected function updateControlFile()
124
    {
125
        file_put_contents($this->getControlFileName(), time());
126
    }
127
}
128