DoctrineCacheCatalogueLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the openl10n package.
5
 *
6
 * (c) Matthieu Moquet <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Openl10n\Component\Translation\MessageCatalogue;
13
14
use Doctrine\Common\Cache\Cache;
15
use Symfony\Component\Translation\MessageCatalogueInterface;
16
17
/**
18
 * Catalogue loader using the doctrine/cache component.
19
 */
20
class DoctrineCacheCatalogueLoader implements MessageCatalogueLoader
21
{
22
    /**
23
     * Decorated catalogue loader.
24
     *
25
     * @var MessageCatalogueLoader
26
     */
27
    protected $catalogueLoader;
28
29
    /**
30
     * Cache provider.
31
     *
32
     * @var Cache
33
     */
34
    protected $cache;
35
36
    /**
37
     * @param MessageCatalogueLoader $catalogueLoader
38
     * @param Cache                  $cache
39
     */
40
    public function __construct(MessageCatalogueLoader $catalogueLoader, Cache $cache)
41
    {
42
        $this->catalogueLoader = $catalogueLoader;
43
        $this->cache = $cache;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function loadCatalogue($locale)
50
    {
51
        if ($this->cache->contains($locale)) {
52
            $catalogue = $this->cache->fetch($locale);
53
54
            // Ensure returned object from cache is the correct instance.
55
            // Because cache may have been corrupted.
56
            if ($catalogue instanceof MessageCatalogueInterface) {
57
                return $catalogue;
58
            }
59
        }
60
61
        $catalogue = $this->catalogueLoader->loadCatalogue($locale);
62
63
        $this->cache->save($locale, $catalogue);
64
65
        return $catalogue;
66
    }
67
}
68