L10nYamlManager   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 112
ccs 36
cts 40
cp 0.9
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getAllL10nResourceList() 0 19 5
A setL10nResource() 0 4 1
A getCatalogue() 0 4 1
A hydrate() 0 9 1
A getL10nResource() 0 21 4
1
<?php
2
3
namespace L10nBundle\Manager\Yaml;
4
5
use L10nBundle\Entity\L10nResource;
6
use L10nBundle\Manager\L10nManagerInterface;
7
use L10nBundle\Utils\Helper\L10nCatalogueHelper;
8
use Symfony\Component\Yaml\Yaml;
9
10
/**
11
 * Manager plugged on a YAML page
12
 * @author Cyril Otal
13
 */
14
class L10nYamlManager implements L10nManagerInterface
15
{
16
    /**
17
     * Catalogue of resource_key, localisation_key, value
18
     *
19
     * @var array
20
     */
21
    private $catalogue;
22
23
    /**
24
     * @param L10nCatalogueHelper $catalogueHelper
25
     * @param array               $config
26
     */
27 1
    public function __construct(L10nCatalogueHelper $catalogueHelper, array $config)
28
    {
29 1
        $this->catalogue = $catalogueHelper->createCatalogue($config);
30 1
    }
31
32
    /**
33
     * Return a L10nResource
34
     *
35
     * @param $idResource
36
     * @param $idLocalization
37
     *
38
     * @return L10nResource|null $l10nResource
39
     */
40 1
    public function getL10nResource($idResource, $idLocalization)
41
    {
42 1
        $values = array();
43
44 1
        $l10nResource = null;
45
46 1
        if (isset($this->catalogue[$idResource][$idLocalization])) {
47 1
            $valueList = $this->catalogue[$idResource][$idLocalization];
48 1
            if (!is_array($valueList)) {
49
                $values[] = $valueList;
50
            } else {
51 1
                $values = $valueList;
52
            }
53
54 1
            if (!empty($values)) {
55 1
                $l10nResource = $this->hydrate($idLocalization, $idResource, $values);
56 1
            }
57 1
        }
58
59 1
        return $l10nResource;
60
    }
61
62
    /**
63
     * Return all L10nResources
64
     *
65
     * @return L10nResource[] $l10nResource
66
     */
67 1
    public function getAllL10nResourceList()
68
    {
69 1
        $l10nResourceList = array();
70
71 1
        if (!empty($this->catalogue)) {
72 1
            foreach ($this->catalogue as $idResource => $idLocalizationList) {
73 1
                foreach ($idLocalizationList as $idLocalization => $valueList) {
74
75 1
                    if (!is_array($valueList)) {
76
                        $valueList = array($valueList);
77
                    }
78
79 1
                    $l10nResourceList[] = $this->hydrate($idLocalization, $idResource, $valueList);
80 1
                }
81 1
            }
82 1
        }
83
84 1
        return $l10nResourceList;
85
    }
86
87
    /**
88
     * Dummy method to respect interface
89
     *
90
     * @param L10nResource $l10nResource
91
     *
92
     * @throws \Exception
93
     */
94 1
    public function setL10nResource(L10nResource $l10nResource)
95
    {
96 1
        throw new \Exception('Can\'t save data in a YAML source');
97
    }
98
99
    /**
100
     * @return array
101
     */
102 1
    public function getCatalogue()
103
    {
104 1
        return $this->catalogue;
105
    }
106
107
    /**
108
     * Build a L10nResource
109
     *
110
     * @param $idLocalization
111
     * @param $idResource
112
     * @param $valueList
113
     *
114
     * @return L10nResource
115
     */
116 3
    protected function hydrate($idLocalization, $idResource, $valueList)
117
    {
118 3
        $l10nResource = new L10nResource();
119 3
        $l10nResource->setIdLocalization($idLocalization);
120 3
        $l10nResource->setIdResource($idResource);
121 3
        $l10nResource->setValueList($valueList);
122
123 3
        return $l10nResource;
124
    }
125
}
126