ResourceCatalogueLoader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addResourceLoader() 0 4 1
A addResource() 0 8 2
B loadCatalogue() 0 23 4
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 Symfony\Component\Translation\Loader\LoaderInterface as ResourceLoader;
15
use Symfony\Component\Translation\MessageCatalogue;
16
17
/**
18
 * Load catalogue from resources.
19
 */
20
class ResourceCatalogueLoader implements MessageCatalogueLoader
21
{
22
    /**
23
     * @var LoaderInterface[]
24
     */
25
    protected $loaders = [];
26
27
    /**
28
     * @var array
29
     */
30
    protected $resources = [];
31
32
    /**
33
     * Adds a Loader.
34
     *
35
     * @param string         $format The name of the loader (@see addResource())
36
     * @param ResourceLoader $loader A LoaderInterface instance
37
     */
38
    public function addResourceLoader($format, ResourceLoader $loader)
39
    {
40
        $this->loaders[$format] = $loader;
41
    }
42
43
    /**
44
     * Adds a Resource.
45
     *
46
     * @param string $format   The name of the loader (@see addLoader())
47
     * @param mixed  $resource The resource name
48
     * @param string $locale   The locale
49
     * @param string $domain   The domain
50
     *
51
     * @throws \InvalidArgumentException If the locale contains invalid characters
52
     */
53
    public function addResource($format, $resource, $locale, $domain = null)
54
    {
55
        if (null === $domain) {
56
            $domain = 'messages';
57
        }
58
59
        $this->resources[$locale][] = [$format, $resource, $domain];
60
    }
61
62
    /**
63
     * Traverses all resources to fetch messages only for the given locale.
64
     *
65
     * {@inheritdoc}
66
     */
67
    public function loadCatalogue($locale)
68
    {
69
        // Create a new catalogue
70
        $catalogue = new MessageCatalogue($locale);
71
72
        // Traverse resources to complete catalogue
73
        $resources = isset($this->resources[$locale]) ? $this->resources[$locale] : [];
74
        foreach ($resources as $resource) {
75
            $name = $resource[0];
76
            $format = $resource[1];
77
            $domain = $resource[2];
78
79
            if (!isset($this->loaders[$name])) {
80
                throw new \RuntimeException("The \"$name\" translation loader is not registered.");
81
            }
82
83
            $catalogue->addCatalogue(
84
                $this->loaders[$name]->load($format, $locale, $domain)
85
            );
86
        }
87
88
        return $catalogue;
89
    }
90
}
91