Completed
Push — master ( c0a8a5...c84007 )
by David
13s queued 11s
created

BaseInvalidateCommand::getContainer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 3
cts 7
cp 0.4286
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 4.679
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle 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\HttpCacheBundle\Command;
13
14
use FOS\HttpCacheBundle\CacheManager;
15
use LogicException;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
/**
21
 * Base class for commands to trigger cache invalidation from the command line.
22
 *
23
 * @author David Buchmann <[email protected]>
24
 */
25
abstract class BaseInvalidateCommand extends Command
26
{
27
    use ContainerAwareTrait;
28
29
    /**
30
     * @var CacheManager
31
     */
32
    private $cacheManager;
33
34
    /**
35
     * If no cache manager is specified explicitly, fos_http_cache.cache_manager
36
     * is automatically loaded.
37
     *
38
     * @param CacheManager|null $cacheManager The cache manager to talk to
39
     */
40 14
    public function __construct(CacheManager $cacheManager = null)
41
    {
42 14
        if (!$cacheManager) {
43 1
            @trigger_error('Instantiating commands without the cache manager is deprecated and will be removed in version 3', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
        }
45 14
        $this->cacheManager = $cacheManager;
46 14
        parent::__construct();
47 14
    }
48
49
    /**
50
     * Get the configured cache manager, loading fos_http_cache.cache_manager
51
     * from the container if none was specified.
52
     *
53
     * @return CacheManager
0 ignored issues
show
Documentation introduced by
Should the return type not be CacheManager|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...
54
     */
55 9
    protected function getCacheManager()
56
    {
57 9
        if (!$this->cacheManager) {
58 1
            $this->cacheManager = $this->getContainer()->get('fos_http_cache.cache_manager');
59
        }
60
61 9
        return $this->cacheManager;
62
    }
63
64
    /**
65
     * @return ContainerInterface
66
     *
67
     * @throws \LogicException
68
     */
69 1
    protected function getContainer()
70
    {
71 1
        if (null === $this->container) {
72
            $application = $this->getApplication();
73
            if (null === $application) {
74
                throw new LogicException('The container cannot be retrieved as the application instance is not yet set.');
75
            }
76
77
            $this->container = $application->getKernel()->getContainer();
78
        }
79
80 1
        return $this->container;
81
    }
82
}
83