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

Command/TranslationCacheCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)
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);
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