MemcachedLoader::load()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 14
nc 6
nop 3
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