Completed
Push — master ( 4e64ba...8e1cb2 )
by Ruud
95:22 queued 80:32
created

Service/Translator/CacheValidator.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\Service\Translator;
4
5
use Symfony\Component\Finder\Finder;
6
7
/**
8
 * Validates and checks translations cache
9
 */
10
class CacheValidator
11
{
12
    /**
13
     * Where to store cache files
14
     *
15
     * @var string
16
     */
17
    private $cacheDir;
18
19
    private $translationRepository;
20
21
    /**
22
     * Checks the caching files of they are even with the stasher content
23
     *
24
     * @return bool
25
     */
26 2
    public function isCacheFresh()
27
    {
28 2
        $fileDate = $this->getOldestCachefileDate();
29 2
        $stashDate = $this->getLastTranslationChangeDate();
30
31 2
        if ($fileDate === null) {
32 1
            return true;
33
        }
34
35 1
        return $fileDate >= $stashDate;
36
    }
37
38
    /**
39
     * Get the last updated or inserted from all database translations
40
     *
41
     * @return DateTime last createdAt or updateAt date from the translations stash
42
     */
43 2
    public function getLastTranslationChangeDate()
44
    {
45 2
        return $this->translationRepository->getLastChangedTranslationDate();
46
    }
47
48
    /**
49
     * Retrieve a Datetime of the oldest cache file made
50
     *
51
     * @return DateTime mtime of oldest cache file
0 ignored issues
show
Should the return type not be null|\DateTime?

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...
52
     */
53 2
    public function getOldestCachefileDate()
54
    {
55 2
        if (!is_dir($this->cacheDir)) {
56
            return null;
57
        }
58
59 2
        $finder = new Finder();
60 2
        $finder->files()
61 2
            ->name('catalogue*.php')
62 2
            ->sortByModifiedTime()
63 2
            ->in($this->cacheDir);
64
65 2
        foreach ($finder as $file) {
66 1
            $date = new \DateTime();
67 1
            $date->setTimestamp($file->getMTime());
68
69 1
            return $date;
70
        }
71
72 1
        return null;
73
    }
74
75 2
    public function setCacheDir($cacheDir)
76
    {
77 2
        $this->cacheDir = $cacheDir;
78 2
    }
79
80
    public function setLogger($logger)
81
    {
82
        $this->logger = $logger;
83
    }
84
85 2
    public function setTranslationRepository($translationRepository)
86
    {
87 2
        $this->translationRepository = $translationRepository;
88 2
    }
89
}
90