ConverterPooler::getClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the 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\Client\ClientPooler;
13
use PommProject\Foundation\Exception\ConverterException;
14
15
/**
16
 * ConverterPooler
17
 *
18
 * Pooler for converters.
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
 * @see         ClientPooler
25
 */
26
class ConverterPooler extends ClientPooler
27
{
28
    protected $converter_holder;
29
30
    /**
31
     * __construct
32
     *
33
     * Instantiate converter pooler.
34
     *
35
     * @param  ConverterHolder $converter_holder
36
     */
37
    public function __construct(ConverterHolder $converter_holder)
38
    {
39
        $this->converter_holder = $converter_holder;
40
    }
41
42
    /**
43
     * getPoolerType
44
     *
45
     * @see ClientPoolerInterface
46
     */
47
    public function getPoolerType()
48
    {
49
        return 'converter';
50
    }
51
52
    /**
53
     * getClient
54
     *
55
     * @see ClientPoolerInterface
56
     */
57
    public function getClient($identifier)
58
    {
59
        if ($identifier !== PgArray::getSubType($identifier)) {
60
            return parent::getClient('array');
61
        } else {
62
            return parent::getClient($identifier);
63
        }
64
    }
65
66
    /**
67
     * createClient
68
     *
69
     * Check in the converter holder if the type has an associated converter.
70
     * If not, an exception is thrown.
71
     *
72
     * @see   ClientPooler
73
     * @throws ConverterException
74
     */
75
    public function createClient($identifier)
76
    {
77
        if (!$this->converter_holder->hasType($identifier)) {
78
            throw new ConverterException(
79
                sprintf(
80
                    "No converter registered for type '%s'.",
81
                    $identifier
82
                )
83
            );
84
        }
85
86
        return new ConverterClient(
87
            $identifier,
88
            $this->converter_holder->getConverterForType($identifier)
89
        );
90
    }
91
92
    /**
93
     * getConverterHolder
94
     *
95
     * Expose converter holder so one can add new converters on the fly.
96
     *
97
     * @return ConverterHolder
98
     */
99
    public function getConverterHolder()
100
    {
101
        return $this->converter_holder;
102
    }
103
}
104