Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

TranslationCacheCommand::flushTranslationCache()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 12
1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Command;
4
5
use Kunstmaan\TranslatorBundle\Service\Translator\CacheValidator;
6
use Kunstmaan\TranslatorBundle\Service\Translator\ResourceCacher;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
12
13
/**
14
 * @final since 5.1
15
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
16
 */
17
class TranslationCacheCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
18
{
19
    /**
20
     * @var ResourceCacher
21
     */
22
    private $resourceCacher;
23
24
    /**
25
     * @var CacheValidator
26
     */
27
    private $cacheValidator;
28
29
    /**
30
     * @param ResourceCacher|null $resourceCacher
31
     * @param CacheValidator|null $cacheValidator
32
     */
33 View Code Duplication
    public function __construct(/* ResourceCacher */ $resourceCacher = null, /* CacheValidator */ $cacheValidator = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        parent::__construct();
36
37
        if (!$resourceCacher instanceof ResourceCacher) {
38
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), 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...
39
40
            $this->setName(null === $resourceCacher ? 'kuma:translator:cache' : $resourceCacher);
41
42
            return;
43
        }
44
45
        $this->resourceCacher = $resourceCacher;
46
        $this->cacheValidator = $cacheValidator;
47
    }
48
49
    protected function configure()
50
    {
51
        $this
52
        ->setName('kuma:translator:cache')
53
        ->setDescription('Request cache status and flush cache')
54
        ->addOption('flush', 'f', InputOption::VALUE_NONE, 'Flush translation cache (if any)')
55
        ->addOption('status', null, InputOption::VALUE_NONE, 'Request cache status')
56
        ;
57
    }
58
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        if ($input->getOption('flush')) {
62
            return $this->flushTranslationCache($input, $output);
63
        }
64
65
        if ($input->getOption('status')) {
66
            return $this->showTranslationCacheStatus($input, $output);
67
        }
68
69
        throw new InvalidArgumentException('No or invalid option provided');
70
    }
71
72
    public function flushTranslationCache(InputInterface $input, OutputInterface $output)
73
    {
74
        if (null === $this->resourceCacher) {
75
            $this->resourceCacher = $this->getContainer()->get('kunstmaan_translator.service.translator.resource_cacher');
76
        }
77
78
        if ($this->resourceCacher->flushCache()) {
79
            $output->writeln('<info>Translation cache succesfully flushed</info>');
80
        }
81
    }
82
83
    public function showTranslationCacheStatus(InputInterface $input, OutputInterface $output)
84
    {
85
        if (null === $this->cacheValidator) {
86
            $this->cacheValidator = $this->getContainer()->get('kunstmaan_translator.service.translator.cache_validator');
87
        }
88
89
        $oldestFile = $this->cacheValidator->getOldestCachefileDate();
90
        $newestTranslation = $this->cacheValidator->getLastTranslationChangeDate();
91
        $isFresh = $this->cacheValidator->isCacheFresh();
92
93
        $output->writeln(sprintf('Oldest file mtime: <info>%s</info>', $oldestFile instanceof \DateTime ? $oldestFile->format('Y-m-d H:i:s') : 'none found'));
94
        $output->writeln(sprintf('Newest translation (in stash): <info>%s</info>', $newestTranslation instanceof \DateTime ? $newestTranslation->format('Y-m-d H:i:s') : 'none found'));
95
        $output->writeln(sprintf('Status: <info>%s</info>', $isFresh ? 'fresh' : 'outdated'));
96
97
        return 0;
98
    }
99
}
100