1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation 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\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 - 2017 Grégoire HUBERT |
29
|
|
|
* @author Grégoire HUBERT |
30
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
31
|
|
|
* @see ClientPoolerInterface |
32
|
|
|
*/ |
33
|
|
|
trait ClientPoolerTrait |
34
|
|
|
{ |
35
|
|
|
private $session; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* getPoolerType |
39
|
|
|
* |
40
|
|
|
* @see ClientPoolerInterface |
41
|
|
|
*/ |
42
|
|
|
abstract public function getPoolerType(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* createClient |
46
|
|
|
* |
47
|
|
|
* Create a new client. |
48
|
|
|
* |
49
|
|
|
* @param string $identifier |
50
|
|
|
* @return Client |
51
|
|
|
*/ |
52
|
|
|
abstract protected function createClient($identifier); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* register |
56
|
|
|
* |
57
|
|
|
* @see ClientPoolerInterface |
58
|
|
|
*/ |
59
|
|
|
public function register(Session $session) |
60
|
|
|
{ |
61
|
|
|
$this->session = $session; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* getClient |
68
|
|
|
* |
69
|
|
|
* Basic getClient method. |
70
|
|
|
* |
71
|
|
|
* @param string $identifier |
72
|
|
|
* @return Client |
73
|
|
|
* @see ClientInterface |
74
|
|
|
*/ |
75
|
|
|
public function getClient($identifier) |
76
|
|
|
{ |
77
|
|
|
$client = $this->getClientFromPool($identifier); |
78
|
|
|
|
79
|
|
|
if ($client === null) { |
80
|
|
|
$client = $this->createClient($identifier); |
81
|
|
|
$this->getSession()->registerClient($client); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $client; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* getClientFromPool |
89
|
|
|
* |
90
|
|
|
* How the pooler fetch a client from the pool. |
91
|
|
|
* |
92
|
|
|
* @param string $identifier |
93
|
|
|
* @return Client|null |
94
|
|
|
*/ |
95
|
|
|
protected function getClientFromPool($identifier) |
96
|
|
|
{ |
97
|
|
|
return $this |
98
|
|
|
->getSession() |
99
|
|
|
->getClient($this->getPoolerType(), $identifier) |
100
|
|
|
; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* getSession |
105
|
|
|
* |
106
|
|
|
* Check if the session is set and return it. |
107
|
|
|
* |
108
|
|
|
* @return Session |
109
|
|
|
* @throws FoundationException |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
protected function getSession() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if ($this->session === null) { |
114
|
|
|
throw new FoundationException( |
115
|
|
|
sprintf( |
116
|
|
|
"Client pooler '%s' is not initialized, session not set.", |
117
|
|
|
get_class($this) |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->session; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.