DatabaseLoader::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @namespace Asm\TranslationLoaderBundle\Translation
4
 */
5
namespace Asm\TranslationLoaderBundle\Translation;
6
7
use Asm\TranslationLoaderBundle\Model\TranslationManagerInterface;
8
use Symfony\Component\Config\Resource\ResourceInterface;
9
use Symfony\Component\Translation\Loader\LoaderInterface;
10
use Symfony\Component\Translation\MessageCatalogue;
11
12
/**
13
 * Class DatabaseLoader
14
 *
15
 * @package Asm\TranslationLoaderBundle\Service
16
 * @author marc aschmann <[email protected]>
17
 * @uses Symfony\Component\Translation\Loader\LoaderInterface
18
 * @uses Symfony\Component\Translation\MessageCatalogue
19
 */
20
class DatabaseLoader implements LoaderInterface, ResourceInterface
21
{
22
    /**
23
     * @var TranslationManagerInterface
24
     */
25
    private $translationManager;
26
27
    /**
28
     * default constructor
29
     *
30
     * @param TranslationManagerInterface $translationManager
31
     */
32 20
    public function __construct(TranslationManagerInterface $translationManager)
33
    {
34 20
        $this->translationManager = $translationManager;
35 20
    }
36
37
    /**
38
     * load messages from db
39
     *
40
     * @param string $resource translation key
41
     * @param string $locale current locale
42
     * @param string $messageDomain message domain
43
     * @return \Symfony\Component\Translation\MessageCatalogue
44
     */
45 6
    public function load($resource, $locale, $messageDomain = 'messages')
46
    {
47
        // get our translations, obviously
48 6
        $translations = $this->translationManager
49 6
            ->findTranslationsByLocaleAndDomain($locale, $messageDomain);
50
51 6
        $catalogue = new MessageCatalogue($locale);
52
53 6
        foreach ($translations as $translation) {
54 6
            $catalogue->set($translation->getTransKey(), $translation->getTranslation(), $messageDomain);
55 6
        }
56
57 6
        return $catalogue;
58
    }
59
60
    /**
61
     * Returns a string representation of the Resource.
62
     *
63
     * @return string A string representation of the Resource
64
     */
65 1
    public function __toString()
66
    {
67 1
        return 'DatabaseLoader::'.base64_encode(serialize(get_class_methods(__CLASS__)));
68
    }
69
70
    /**
71
     * Returns true if the resource has not been updated since the given timestamp.
72
     *
73
     * @param int $timestamp The last time the resource was loaded
74
     * @return bool True if the resource has not been updated, false otherwise
75
     */
76 2
    public function isFresh($timestamp)
77
    {
78 2
        $count = $this->translationManager->findTranslationFreshness($timestamp);
79
80
        // If we cannot fetch from database, keep the cache, even if it's not fresh.
81 2
        if (false === $count) {
82 1
            return true;
83
        }
84
85 1
        return (bool) $count;
86
    }
87
88
    /**
89
     * Returns the tied resource.
90
     *
91
     * @return mixed The resource
92
     */
93 1
    public function getResource()
94
    {
95 1
        return $this;
96
    }
97
}
98