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 |
||
52 | */ |
||
53 | 4 | public function getOldestCachefileDate() |
|
54 | { |
||
55 | 4 | if (!is_dir($this->cacheDir)) { |
|
56 | return null; |
||
57 | } |
||
58 | |||
59 | 4 | $finder = new Finder(); |
|
60 | 4 | $finder->files() |
|
61 | 4 | ->name('catalogue*.php') |
|
62 | 4 | ->sortByModifiedTime() |
|
63 | 4 | ->in($this->cacheDir); |
|
64 | |||
65 | 4 | if ($finder->count() > 0) { |
|
66 | // Get first result (=oldest cache file) |
||
67 | 2 | $iterator = $finder->getIterator(); |
|
68 | 2 | $iterator->rewind(); |
|
69 | 2 | $file = $iterator->current(); |
|
70 | |||
71 | 2 | return (new \DateTime())->setTimestamp($file->getMTime()); |
|
72 | } |
||
73 | |||
74 | 2 | return null; |
|
75 | } |
||
76 | |||
77 | 4 | public function setCacheDir($cacheDir) |
|
78 | { |
||
79 | 4 | $this->cacheDir = $cacheDir; |
|
80 | 4 | } |
|
81 | |||
82 | public function setLogger($logger) |
||
83 | { |
||
84 | $this->logger = $logger; |
||
0 ignored issues
–
show
|
|||
85 | } |
||
86 | |||
87 | 4 | public function setTranslationRepository($translationRepository) |
|
88 | { |
||
89 | 4 | $this->translationRepository = $translationRepository; |
|
90 | 4 | } |
|
91 | } |
||
92 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: