Completed
Push — 2.0 ( 143803...4e64fa )
by grégoire
08:42 queued 04:22
created

ClientPoolerTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 8.7 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 8
loc 92
c 1
b 1
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
getPoolerType() 0 1 ?
createClient() 0 1 ?
A register() 0 6 1
A getClient() 0 11 2
A getClientFromPool() 0 7 1
A getSession() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation 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\Client;
11
12
use PommProject\Foundation\Exception\FoundationException;
13
use PommProject\Foundation\Session\Session;
14
15
/**
16
 * ClientPoolerTrait
17
 *
18
 * Trait for client poolers. ClientPooler instances are factories for
19
 * Session's Client. The workflow of the ClientPooler is the following:
20
 * When a client is called
21
 * 1   It is fetched from the pool
22
 * 2.0 If no client is found:
23
 * 2.1 The createClient() method is triggered.
24
 * 2.2 The client is registered to the session
25
 * 3   The client is returned back
26
 *
27
 * @package   Foundation
28
 * @copyright 2014 - 2015 Grégoire HUBERT
29
 * @author    Grégoire HUBERT
30
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
31
 * @see       ClientPoolerInterface
32
 * @abstract
33
 */
34
trait ClientPoolerTrait
35
{
36
    private $session;
37
38
    /**
39
     * getPoolerType
40
     *
41
     * @see ClientPoolerInterface
42
     */
43
    abstract public function getPoolerType();
44
45
    /**
46
     * createClient
47
     *
48
     * Create a new client.
49
     *
50
     * @abstract
51
     * @access protected
52
     * @param  string $identifier
53
     * @return Client
54
     */
55
    abstract protected function createClient($identifier);
56
57
    /**
58
     * register
59
     *
60
     * @see ClientPoolerInterface
61
     */
62
    public function register(Session $session)
63
    {
64
        $this->session = $session;
65
66
        return $this;
67
    }
68
69
    /**
70
     * getClient
71
     *
72
     * Basic getClient method.
73
     *
74
     * @access public
75
     * @param  string $identifier
76
     * @return Client
77
     * @see    ClientInterface
78
     */
79
    public function getClient($identifier)
80
    {
81
        $client = $this->getClientFromPool($identifier);
82
83
        if ($client === null) {
84
            $client = $this->createClient($identifier);
85
            $this->getSession()->registerClient($client);
86
        }
87
88
        return $client;
89
    }
90
91
    /**
92
     * getClientFromPool
93
     *
94
     * How the pooler fetch a client from the pool.
95
     *
96
     * @access protected
97
     * @param  string      $identifier
98
     * @return Client|null
99
     */
100
    protected function getClientFromPool($identifier)
101
    {
102
        return $this
103
            ->getSession()
104
            ->getClient($this->getPoolerType(), $identifier)
105
            ;
106
    }
107
108
    /**
109
     * getSession
110
     *
111
     * Check if the session is set and return it.
112
     *
113
     * @access protected
114
     * @return Session
115
     * @throws FoundationException
116
     */
117 View Code Duplication
    protected function getSession()
118
    {
119
        if ($this->session === null) {
120
            throw new FoundationException(sprintf("Client pooler '%s' is not initialized, session not set.", get_class($this)));
121
        }
122
123
        return $this->session;
124
    }
125
}
126