ModelPooler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 51
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPoolerType() 0 4 1
A getClientFromPool() 0 4 1
A createClient() 0 22 4
1
<?php
2
/*
3
 * This file is part of the PommProject/ModelManager 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\ModelManager\Model;
11
12
use PommProject\Foundation\Client\ClientPooler;
13
use PommProject\Foundation\Client\ClientPoolerInterface;
14
use PommProject\ModelManager\Exception\ModelException;
15
16
/**
17
 * ModelPooler
18
 *
19
 * Client pooler for model package.
20
 *
21
 * @package   ModelManager
22
 * @copyright 2014 - 2015 Grégoire HUBERT
23
 * @author    Grégoire HUBERT
24
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
25
 * @see       ClientPooler
26
 */
27
class ModelPooler extends ClientPooler
28
{
29
    /**
30
     * @see ClientPoolerInterface
31
     */
32
    public function getPoolerType()
33
    {
34
        return 'model';
35
    }
36
37
    /**
38
     * getClientFromPool
39
     *
40
     * @see    ClientPooler
41
     * @return Model|null
42
     */
43
    protected function getClientFromPool($class)
44
    {
45
        return $this->getSession()->getClient($this->getPoolerType(), trim($class, "\\"));
46
    }
47
48
    /**
49
     * createModel
50
     *
51
     * @see    ClientPooler
52
     * @throws ModelException if incorrect
53
     * @return Model
54
     */
55
    protected function createClient($class)
56
    {
57
        try {
58
            $reflection = new \ReflectionClass($class);
59
        } catch (\ReflectionException $e) {
60
            throw new ModelException(sprintf(
61
                "Could not instantiate Model class '%s'. (Reason: '%s').",
62
                $class,
63
                $e->getMessage()
64
            ));
65
        }
66
67
        if (!$reflection->implementsInterface('\PommProject\Foundation\Client\ClientInterface')) {
68
            throw new ModelException(sprintf("'%s' class does not implement the ClientInterface interface.", $class));
69
        }
70
71
        if (!$reflection->isSubclassOf('\PommProject\ModelManager\Model\Model')) {
72
            throw new ModelException(sprintf("'%s' class does not extend \PommProject\ModelManager\Model.", $class));
73
        }
74
75
        return new $class();
76
    }
77
}
78