ConverterHolder   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 187
Duplicated Lines 9.63 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 1
dl 18
loc 187
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A registerConverter() 0 10 2
A addConverter() 9 18 4
A hasConverterName() 0 4 1
A getConverter() 0 4 2
A getConverterNames() 0 4 1
A addTypeToConverter() 9 16 2
A getConverterForType() 0 14 2
A hasType() 0 4 1
A getTypes() 0 4 1
A getTypesWithConverterName() 0 4 1

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
 * This file is part of Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Converter;
11
12
use PommProject\Foundation\Exception\ConverterException;
13
14
/**
15
 * ConverterHolder
16
 *
17
 * Responsible of holding all converters associated with their corresponding
18
 * types.
19
 *
20
 * @package   Foundation
21
 * @copyright 2014 - 2017 Grégoire HUBERT
22
 * @author    Grégoire HUBERT
23
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
24
 */
25
class ConverterHolder
26
{
27
    protected $converters = [];
28
    protected $types = [];
29
30
    /**
31
     * registerConverter
32
     *
33
     * Declare a converter and assign types to it.
34
     *
35
     * @param  string             $name
36
     * @param  ConverterInterface $converter
37
     * @param  array              $types
38
     * @param  bool               $strict
39
     * @return ConverterHolder    $this
40
     */
41
    public function registerConverter($name, ConverterInterface $converter, array $types, $strict = null)
42
    {
43
        $this->addConverter($name, $converter, $strict);
44
45
        foreach ($types as $type) {
46
            $this->addTypeToConverter($name, $type);
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * addConverter
54
     *
55
     * Add a converter with a new name. If strict is set to true and the
56
     * converter for this type has already been registered, then it throws and
57
     * exception.
58
     *
59
     * @param  string             $name
60
     * @param  ConverterInterface $converter
61
     * @param  bool               $strict (default true)
62
     * @throws ConverterException if $name already exists and strict.
63
     * @return ConverterHolder    $this
64
     */
65
    protected function addConverter($name, ConverterInterface $converter, $strict = null)
66
    {
67
        $strict = $strict === null ? true : (bool) $strict;
68
69 View Code Duplication
        if ($strict && $this->hasConverterName($name)) {
70
            throw new ConverterException(
71
                sprintf(
72
                    "A converter named '%s' already exists. (Known converters are {%s}).",
73
                    $name,
74
                    join(', ', $this->getConverterNames())
75
                )
76
            );
77
        }
78
79
        $this->converters[$name] = $converter;
80
81
        return $this;
82
    }
83
84
    /**
85
     * hasConverterName
86
     *
87
     * Tell if the converter exists or not.
88
     *
89
     * @param  string $name
90
     * @return bool
91
     */
92
    public function hasConverterName($name)
93
    {
94
        return (bool) isset($this->converters[$name]);
95
    }
96
97
    /**
98
     * getConverter
99
     *
100
     * Return the converter associated with this name. If no converters found,
101
     * NULL is returned.
102
     *
103
     * @param  string             $name
104
     * @return ConverterInterface
105
     */
106
    public function getConverter($name)
107
    {
108
        return $this->hasConverterName($name) ? $this->converters[$name] : null;
109
    }
110
111
    /**
112
     * getConverterNames
113
     *
114
     * Returns an array with the names of the registered converters.
115
     *
116
     * @return array
117
     */
118
    public function getConverterNames()
119
    {
120
        return array_keys($this->converters);
121
    }
122
123
    /**
124
     * addTypeToConverter
125
     *
126
     * Make the given converter to support a new PostgreSQL type. If the given
127
     * type is already defined, it is overrided with the new converter.
128
     *
129
     * @param  string          $name
130
     * @param  string          $type
131
     * @throws  ConverterException if $name does not exist.
132
     * @return ConverterHolder $this
133
     */
134
    public function addTypeToConverter($name, $type)
135
    {
136 View Code Duplication
        if (!$this->hasConverterName($name)) {
137
            throw new ConverterException(
138
                sprintf(
139
                    "No such converter name '%s'. Registered converters are {%s}.",
140
                    $name,
141
                    join(', ', $this->getConverterNames())
142
                )
143
            );
144
        }
145
146
        $this->types[$type] = $name;
147
148
        return $this;
149
    }
150
151
    /**
152
     * getConverterForType
153
     *
154
     * Returns the converter instance for the given type.
155
     *
156
     * @param  string             $type
157
     * @throws  ConverterException if there are no converters associated.
158
     * @return ConverterInterface
159
     */
160
    public function getConverterForType($type)
161
    {
162
        if (!$this->hasType($type)) {
163
            throw new ConverterException(
164
                sprintf(
165
                    "No converters associated with type '%s'. Handled types are {%s}.",
166
                    $type,
167
                    join(', ', $this->getTypes())
168
                )
169
            );
170
        }
171
172
        return $this->converters[$this->types[$type]];
173
    }
174
175
    /**
176
     * hasType
177
     *
178
     * Does the type exist ?
179
     *
180
     * @param  string $type
181
     * @return bool
182
     */
183
    public function hasType($type)
184
    {
185
        return (bool) isset($this->types[$type]);
186
    }
187
188
    /**
189
     * getTypes
190
     *
191
     * Return the list of handled types.
192
     *
193
     * @return array
194
     */
195
    public function getTypes()
196
    {
197
        return array_keys($this->types);
198
    }
199
200
    /**
201
     * getTypesWithConverterName
202
     *
203
     * Return the list of types with the related converter name.
204
     *
205
     * @return array
206
     */
207
    public function getTypesWithConverterName()
208
    {
209
        return $this->types;
210
    }
211
}
212