InspectorPooler::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 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\Inspector;
11
12
use PommProject\Foundation\Inflector;
13
use PommProject\Foundation\Client\ClientPooler;
14
use PommProject\Foundation\Client\ClientInterface;
15
use PommProject\Foundation\Client\ClientPoolerInterface;
16
use PommProject\Foundation\Exception\FoundationException;
17
18
/**
19
 * InspectorPooler
20
 *
21
 * Pooler for Inspector client.
22
 *
23
 * @package   Foundation
24
 * @copyright 2014 - 2017 Grégoire HUBERT
25
 * @author    Grégoire HUBERT
26
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
27
 * @see       ClientPooler
28
 */
29
class InspectorPooler extends ClientPooler
30
{
31
    /**
32
     * getPoolerType
33
     *
34
     * @see ClientPoolerInterface
35
     */
36
    public function getPoolerType()
37
    {
38
        return 'inspector';
39
    }
40
41
    /**
42
     * getClient
43
     *
44
     * @see    ClientPooler
45
     * @param  null|string $identifier
46
     * @return Inspector
47
     */
48
    public function getClient($identifier = null)
49
    {
50
        if ($identifier === null) {
51
            $identifier = 'legacy';
52
        }
53
54
        return parent::getClient($identifier);
55
    }
56
57
    /**
58
     * createClient
59
     *
60
     * @see    ClientPooler
61
     * @return Inspector
62
     * @throws FoundationException
63
     */
64
    protected function createClient($identifier)
65
    {
66
        if (!class_exists($identifier)) {
67
            return $this->createBuiltinClient($identifier);
68
        }
69
70
        return $this->createCustomClient($identifier);
71
    }
72
73
    /**
74
     * createBuiltinClient
75
     *
76
     * Return an instance of a builtin inspector from its short name. Throws an
77
     * exception if the built in inspector does not exist.
78
     *
79
     * @param   string $identifier
80
     * @throws  FoundationException
81
     * @return  ClientInterface
82
     */
83
    private function createBuiltinClient($identifier)
84
    {
85
        $class_name = sprintf(
86
            "\PommProject\Foundation\Inspector\%sInspector",
87
            Inflector::studlyCaps($identifier)
88
        );
89
90
        if (!class_exists($class_name)) {
91
            throw new FoundationException(
92
                sprintf(
93
                    "Unknown built in inspector '%s'. Default inspector clients are {%s}.",
94
                    $identifier,
95
                    'database, schema, relation, type, role'
96
                )
97
            );
98
        }
99
100
        return new $class_name;
101
    }
102
103
    /**
104
     * createCustomClient
105
     *
106
     * Load a custom inspector client. Throws an exception if class cannot be
107
     * loaded or is not a ClientInterface.
108
     *
109
     * @param   string $identifier
110
     * @throws  FoundationException
111
     * @return  ClientInterface
112
     */
113
    private function createCustomClient($identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        try {
116
            $refl = new \ReflectionClass($identifier);
117
118
            if (!$refl->implementsInterface('\PommProject\Foundation\Client\ClientInterface')) {
119
                throw new FoundationException(
120
                    sprintf(
121
                        "Class '%s' must implement '\PommProject\Foundation\Client\ClientInterface'.",
122
                        $identifier
123
                    )
124
                );
125
            }
126
        } catch (\ReflectionException $e) {
127
            throw new FoundationException(
128
                sprintf(
129
                    "Cannot find inspector class '%s'.",
130
                    $identifier
131
                ),
132
                null,
133
                $e
134
            );
135
        }
136
137
        return new $identifier;
138
    }
139
}
140