Issues (281)

src/Cache/DefinitionCacheWarmer.php (4 issues)

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\Finder\Finder;
14
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
15
use Symfony\Component\HttpKernel\Kernel;
16
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
17
18
/**
19
 * DefinitionCacheWarmer
20
 */
21
class DefinitionCacheWarmer extends CacheWarmer
22
{
23
    /**
24
     * @var DefinitionRegistry
25
     */
26
    protected $registry;
27
28
    /**
29
     * @var Kernel
30
     */
31
    protected $kernel;
32
33
    /**
34
     * DefinitionCacheWarmer constructor.
35
     *
36
     * @param Kernel             $kernel
37
     * @param DefinitionRegistry $registry
38
     */
39
    public function __construct(Kernel $kernel, DefinitionRegistry $registry)
40
    {
41
        $this->kernel = $kernel;
42
        $this->registry = $registry;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function isOptional()
49
    {
50
        return true;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function warmUp($cacheDir)
57
    {
58
        $this->registry->clearCache(true);
59
        $this->updateControlFile();
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    protected function isFreshCache()
66
    {
67
        if (!file_exists($this->getControlFileName())) {
68
            return false;
69
        }
70
71
        $controlTime = filemtime($this->getControlFileName());
72
73
        $projectDir = $this->kernel->getProjectDir();
74
75
        if (Kernel::VERSION_ID >= 40000) {
76
            $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...
77
            $dirs[] = $projectDir.'/src';
78
        } else {
79
            $dirs[] = $this->kernel->getRootDir();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpKernel\Kernel::getRootDir() has been deprecated: since Symfony 4.2, use getProjectDir() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

79
            $dirs[] = /** @scrutinizer ignore-deprecated */ $this->kernel->getRootDir();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
80
            $dirs[] = $this->kernel->getRootDir().'/../src';
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpKernel\Kernel::getRootDir() has been deprecated: since Symfony 4.2, use getProjectDir() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
            $dirs[] = /** @scrutinizer ignore-deprecated */ $this->kernel->getRootDir().'/../src';

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
81
        }
82
83
        /** @var iterable $files */
84
        $files = Finder::create()
85
                       ->in($dirs)
86
                       ->date(sprintf('>= %s', date('Y-m-d H:i:s', $controlTime)))
87
                       ->files();
88
89
        //exist at least one modified file
90
        foreach ($files as $file) {
91
            return false;
92
            break;
0 ignored issues
show
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...
93
        }
94
95
        return true;
96
    }
97
98
    protected function getControlFileName()
99
    {
100
        return $this->kernel->getCacheDir().'/graphal.schema.timestamp';
101
    }
102
103
    protected function updateControlFile()
104
    {
105
        file_put_contents($this->getControlFileName(), time());
106
    }
107
}
108