Completed
Push — master ( 462877...657388 )
by grégoire
01:55
created

InspectorPooler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 5
Bugs 2 Features 2
Metric Value
wmc 10
c 5
b 2
f 2
lcom 0
cbo 3
dl 0
loc 112
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPoolerType() 0 4 1
A getClient() 0 8 2
A createClient() 0 9 2
A createBuiltinClient() 0 19 2
B createCustomClient() 0 26 3
1
<?php
2
/*
3
 * This file is part of the Pomm package.
4
 *
5
 * (c) 2014 - 2015 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 - 2015 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
    /**
75
     * createBuiltinClient
76
     *
77
     * Return an instance of a builtin inspector from its short name. Throws an
78
     * exception if the built in inspector does not exist.
79
     *
80
     * @param   string $identifier
81
     * @throws  FoundationException
82
     * @return  ClientInterface
83
     */
84
    private function createBuiltinClient($identifier)
85
    {
86
        $class_name = sprintf(
87
            "\PommProject\Foundation\Inspector\%sInspector",
88
            Inflector::studlyCaps($identifier)
89
        );
90
91
        if (!class_exists($class_name)) {
92
            throw new FoundationException(
93
                sprintf(
94
                    "Unknown built in inspector '%s'. Default inspector clients are {%s}.",
95
                    $identifier,
96
                    'database, schema, relation, type, role'
97
                )
98
            );
99
        }
100
101
        return new $class_name;
102
    }
103
104
    /**
105
     * createCustomClient
106
     *
107
     * Load a custom inspector client. Throws an exception if class cannot be
108
     * loaded or is not a ClientInterface.
109
     *
110
     * @param   string $identifier
111
     * @throws  FoundationException
112
     * @return  ClientInterface
113
     */
114
    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...
115
    {
116
        try {
117
            $refl = new \ReflectionClass($identifier);
118
119
            if (!$refl->implementsInterface('\PommProject\Foundation\Client\ClientInterface')) {
120
                throw new FoundationException(
121
                    sprintf(
122
                        "Class '%s' must implement '\PommProject\Foundation\Client\ClientInterface'.",
123
                        $identifier
124
                    )
125
                );
126
            }
127
        } catch (\ReflectionException $e) {
128
            throw new FoundationException(
129
                sprintf(
130
                    "Cannot find inspector class '%s'.",
131
                    $identifier
132
                ),
133
                null,
134
                $e
135
            );
136
        }
137
138
        return new $identifier;
139
    }
140
}
141