MemcachedLoader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 5
c 4
b 1
f 0
lcom 1
cbo 3
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUberMemcached() 0 4 1
A load() 0 21 4
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