ModelPooler::getClientFromPool()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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