L10nMongoDbManager   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 129
Duplicated Lines 10.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 1
dl 14
loc 129
ccs 64
cts 68
cp 0.9412
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getL10nResource() 7 26 4
B getAllL10nResourceList() 7 28 5
B setL10nResource() 0 27 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace L10nBundle\Manager\MongoDb;
4
5
use L10nBundle\Entity\L10nResource;
6
use L10nBundle\Manager\L10nManagerInterface;
7
8
/**
9
 * Manager plugged on a MongoDb database
10
 *
11
 * @author Cyril Otal
12
 */
13
class L10nMongoDbManager implements L10nManagerInterface
14
{
15
    /**
16
     * @var \MongoClient
17
     */
18
    protected $mongoClient;
19
20
    /**
21
     * @var \MongoDB
22
     */
23
    protected $db;
24
25
    /**
26
     * @param string $host
27
     * @param string $port
28
     * @param string $username
29
     * @param string $password
30
     * @param string $database
31
     */
32
    public function __construct($host, $port, $username, $password, $database)
33
    {
34
        $this->mongoClient = new \MongoClient('mongodb://' . $username . ':' . $password . '@' . $host . ':' . $port);
35
        $this->db = $this->mongoClient->selectDB($database);
36
    }
37
38
    /**
39
     * Return a L10nResource
40
     *
41
     * @param $idResource
42
     * @param $idLocalization
43
     *
44
     * @return L10nResource $l10nResource
45
     */
46 2
    public function getL10nResource($idResource, $idLocalization)
47
    {
48 2
        $l10nCollection = $this->db->L10nResource;
49 2
        $query = array('id_resource' => (string) $idResource, 'id_localization' => (string) $idLocalization);
50 2
        $l10nResult = $l10nCollection->findOne($query);
51
52 2
        $l10nResource = null;
53
54 2
        if (count($l10nResult)) {
55 2
            $valueList = array();
56 2
            $valueListResult = $l10nResult['value_list'];
57 2 View Code Duplication
            foreach ($valueListResult as $value) {
58 2
                if (isset($value['language'])) {
59 1
                    $valueList[$value['language']] = $value['value'];
60 1
                } else {
61 1
                    $valueList[] = $value;
62
                }
63 2
            }
64 2
            $l10nResource = new L10nResource();
65 2
            $l10nResource->setIdLocalization($idLocalization);
66 2
            $l10nResource->setIdResource($idResource);
67 2
            $l10nResource->setValueList($valueList);
68 2
        }
69
70 2
        return $l10nResource;
71
    }
72
73
    /**
74
     * Return all L10nResources
75
     *
76
     * @return L10nResource[] $l10nResource
77
     */
78 2
    public function getAllL10nResourceList()
79
    {
80 2
        $l10nCollection = $this->db->L10nResource;
81
        /** @var array $l10nResultList */
82 2
        $l10nResultList = $l10nCollection->find();
83
84 2
        $l10nResourceList = array();
85 2
        if (count($l10nResultList)) {
86 2
            foreach ($l10nResultList as $l10nResult) {
87 2
                $valueList = array();
88 2
                $valueListResult = $l10nResult['value_list'];
89 2 View Code Duplication
                foreach ($valueListResult as $value) {
90 2
                    if (isset($value['language'])) {
91 1
                        $valueList[$value['language']] = $value['value'];
92 1
                    } else {
93 1
                        $valueList[] = $value;
94
                    }
95 2
                }
96 2
                $l10nResource = new L10nResource();
97 2
                $l10nResource->setIdLocalization($l10nResult['id_localization']);
98 2
                $l10nResource->setIdResource($l10nResult['id_resource']);
99 2
                $l10nResource->setValueList($valueList);
100 2
                $l10nResourceList[] = $l10nResource;
101 2
            }
102 2
        }
103
104 2
        return $l10nResourceList;
105
    }
106
107
    /**
108
     * Update a L10nResource
109
     *
110
     * @param L10nResource $l10nResource which valueList is a list of values.
111
     *                                   array('value') if not internationalized,
112
     *                                   array('locale_code' => 'value', …) if internationalized
113
     */
114 2
    public function setL10nResource(L10nResource $l10nResource)
115
    {
116 2
        $idResource = $l10nResource->getIdResource();
117 2
        $idLocalization = $l10nResource->getIdLocalization();
118 2
        $valueList = $l10nResource->getValueList();
119 2
        $valueMongoList = array();
120 2
        foreach ($valueList as $locale => $value) {
121 2
            if ($locale) {
122 1
                $valueMongoList[] = array(
123 1
                    'language' => $locale,
124
                    'value'    => $value
125 1
                );
126 1
            } else {
127 1
                $valueMongoList[] = $value;
128
            }
129 2
        }
130 2
        $l10nCollection = $this->db->L10nResource;
131 2
        $l10nCollection->update(
132 2
            array('id_resource' => (string) $idResource, 'id_localization' => (string) $idLocalization),
133
            array(
134 2
                'id_resource'     => (string) $idResource,
135 2
                'id_localization' => (string) $idLocalization,
136
                'value_list'      => $valueMongoList
137 2
            ),
138 2
            array('upsert' => true)
139 2
        );
140 2
    }
141
}
142