MemcachedLoader::setUberMemcached()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Sleepness\UberTranslationBundle\Translation\Loader;
4
5
use Sleepness\UberTranslationBundle\Storage\UberMemcached;
6
use Symfony\Component\Translation\Loader\LoaderInterface;
7
use Symfony\Component\Translation\Exception\InvalidResourceException;
8
use Symfony\Component\Translation\MessageCatalogue;
9
10
/**
11
 * Load messages from memcache and push them into catalogue
12
 *
13
 * @author Viktor Novikov <[email protected]>
14
 * @author Alexandr Zhulev <[email protected]>
15
 */
16
class MemcachedLoader implements LoaderInterface
17
{
18
    private $memcached;
19
20
    /**
21
     * @param UberMemcached $memcached
22
     */
23
    public function setUberMemcached(UberMemcached $memcached)
24
    {
25
        $this->memcached = $memcached;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function load($resource, $locale, $domain = 'messages')
32
    {
33
        $resource = $this->memcached;
34
        $messagesOfDomain = $resource->getItem($locale);
35
        // no messages in cache
36
        if (!isset($messagesOfDomain[$domain])) {
37
            $messages = array();
38
        } else {
39
            $messages = $messagesOfDomain[$domain];
40
        }
41
        if (!is_array($messages)) {
42
            throw new InvalidResourceException(sprintf('The resource "%s" must contain an array.', $resource));
43
        }
44
        $catalogue = new MessageCatalogue($locale);
45
        foreach ($messages as $ymlKey => $translation) {
46
            $catalogue->set($ymlKey, $translation, $domain);
47
        }
48
        $catalogue->addResource($resource);
49
50
        return $catalogue;
51
    }
52
}
53