Passed
Pull Request — master (#1)
by Mariano
03:12
created

SimpleConverterContainer::validateBuilder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of php-simple-conversion.
4
 *
5
 * php-simple-conversion is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-conversion is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-conversion.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Mcustiel\Conversion;
20
21
use Mcustiel\Conversion\Exception\ConverterDoesNotExistException;
22
23
/**
24
 * Singleton class the holds all the registered converters, and allows to access them.
25
 *
26
 * @author mcustiel
27
 */
28
class SimpleConverterContainer implements ConverterContainer
29
{
30
    /**
31
     * @var ConverterBuilder|Converter[][]
32
     */
33
    private $converters = [];
34
35
    /**
36
     * Registers a converter.
37
     *
38
     * @param ConverterBuilder $converterBuilder the builder of the converter to register
39
     *
40
     * @throws \InvalidArgumentException if from or to are unset
41
     */
42
    public function addConverter(ConverterBuilder $converterBuilder)
43
    {
44
        $this->validateBuilder($converterBuilder);
45
        if (!isset($this->converters[$converterBuilder->getFrom()])) {
46
            $this->converters[$converterBuilder->getFrom()] = [];
47
        }
48
        $this->converters[$converterBuilder->getFrom()][$converterBuilder->getTo()] = $converterBuilder;
49
    }
50
51
    /**
52
     * Access the implementation of the converter for the given from and to parameters.
53
     *
54
     * @param string $from the type from which the converter converts
55
     * @param string $to   the type to which the converter converts to
56
     *
57
     * @throws \Mcustiel\Conversion\Exception\ConverterDoesNotExistException
58
     *
59
     * @return Converter
60
     */
61
    public function getConverter($from, $to)
62
    {
63
        if (!isset($this->converters[$from][$to])) {
64
            throw new ConverterDoesNotExistException(
65
                "Converter from {$from} to {$to} does not exist"
66
            );
67
        }
68
        $converter = $this->converters[$from][$to];
69
        if ($converter instanceof ConverterBuilder) {
70
            $this->converters[$from][$to] = $converter->getConverter();
71
        }
72
73
        return $this->converters[$from][$to];
74
    }
75
76
    /**
77
     * @param converter
78
     * @param mixed $converter
79
     */
80
    private function validateBuilder($converter)
81
    {
82
        if (empty($converter->getFrom())) {
83
            throw new \InvalidArgumentException('From is unset');
84
        }
85
        if (empty($converter->getTo())) {
86
            throw new \InvalidArgumentException('To is unset');
87
        }
88
    }
89
}
90