Completed
Push — l10n_master ( 453465...12070f )
by Jeroen
15:21 queued 08:31
created

CacheValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 82
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isCacheFresh() 0 11 2
A getLastTranslationChangeDate() 0 4 1
A getOldestCachefileDate() 0 23 3
A setCacheDir() 0 4 1
A setLogger() 0 4 1
A setTranslationRepository() 0 4 1
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
Bug introduced by
The property logger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
    }
86
87 4
    public function setTranslationRepository($translationRepository)
88
    {
89 4
        $this->translationRepository = $translationRepository;
90 4
    }
91
}
92