Completed
Pull Request — 2.0 (#75)
by Julien
02:03
created

SessionBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 9
Bugs 4 Features 2
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 209
c 9
b 4
f 2
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A createConnection() 0 4 1
A buildSession() 0 19 1
A getDefaultConfiguration() 0 13 1
A createSession() 0 6 1
A __construct() 0 16 2
A addParameter() 0 6 1
A getConverterHolder() 0 4 1
A preConfigure() 0 4 1
A createClientHolder() 0 4 1
A postConfigure() 0 4 1
A initializeConverterHolder() 0 4 1
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\Session;
11
12
use PommProject\Foundation\Converter;
13
use PommProject\Foundation\ParameterHolder;
14
use PommProject\Foundation\Client\ClientHolder;
15
use PommProject\Foundation\Converter\ConverterHolder;
16
17
/**
18
 * SessionBuilder
19
 *
20
 * Session factory.
21
 * This class is responsible of creating and configuring a session. It is a
22
 * default configuration for session and is dedicated to be overloaded.
23
 *
24
 * @package   Foundation
25
 * @copyright 2014 - 2015 Grégoire HUBERT
26
 * @author    Grégoire HUBERT
27
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
28
 */
29
class SessionBuilder
30
{
31
    protected $configuration;
32
    protected $converter_holder;
33
34
    /**
35
     * __construct
36
     *
37
     * Instantiate builder.
38
     *
39
     * Mandatory configuration options are:
40
     * dsn:  connection parameters
41
     * name: database logical name
42
     *
43
     * @access public
44
     * @param array             $configuration
45
     * @param ConverterHolder   $converter_holder
46
     */
47
    public function __construct(array $configuration, ConverterHolder $converter_holder = null)
48
    {
49
        $this->configuration = new ParameterHolder(
50
            array_merge(
51
                $this->getDefaultConfiguration(),
52
                $configuration
53
            )
54
        );
55
        $converter_holder = $converter_holder === null
56
            ? new ConverterHolder
57
            : $converter_holder
58
            ;
59
60
        $this->initializeConverterHolder($converter_holder);
61
        $this->converter_holder = $converter_holder;
62
    }
63
64
    /**
65
     * addParameter
66
     *
67
     * Add a configuration parameter.
68
     *
69
     * @access public
70
     * @param  string $name
71
     * @param  mixed $value
72
     * @return SessionBuilder $this
73
     */
74
    public function addParameter($name, $value)
75
    {
76
        $this->configuration->setParameter($name, $value);
77
78
        return $this;
79
    }
80
81
    /**
82
     * getConverterHolder
83
     *
84
     * Return the converter holder.
85
     *
86
     * @access public
87
     * @return ConverterHolder
88
     */
89
    public function getConverterHolder()
90
    {
91
        return $this->converter_holder;
92
    }
93
94
    /**
95
     * buildSession
96
     *
97
     * Build a new session.
98
     *
99
     * @final
100
     * @access public
101
     * @param  string   $stamp
102
     * @return Session
103
     */
104
    final public function buildSession($stamp = null)
105
    {
106
        $this->preConfigure();
107
        $dsn = $this
108
            ->configuration->mustHave('dsn')->getParameter('dsn');
109
        $connection_configuration =
110
            $this->configuration
111
            ->mustHave('connection:configuration')
112
            ->getParameter('connection:configuration')
113
            ;
114
        $session = $this->createSession(
115
            $this->createConnection($dsn, $connection_configuration),
0 ignored issues
show
Bug introduced by
It seems like $dsn defined by $this->configuration->mu...')->getParameter('dsn') on line 107 can also be of type array; however, PommProject\Foundation\S...der::createConnection() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
            $this->createClientHolder(),
117
            $stamp
118
        );
119
        $this->postConfigure($session);
120
121
        return $session;
122
    }
123
124
    /**
125
     * getDefaultConfiguration
126
     *
127
     * This must return the default configuration for new sessions. Default
128
     * parameters are overrided by the configuration passed as parameter to
129
     * this builder.
130
     *
131
     * @access protected
132
     * @return array
133
     */
134
    protected function getDefaultConfiguration()
135
    {
136
        return
137
            [
138
                "connection:configuration" =>
139
                [
140
                    'bytea_output'  => 'hex',
141
                    'intervalstyle' => 'ISO_8601',
142
                    'datestyle'     => 'ISO',
143
                    'standard_conforming_strings' => 'true',
144
                ]
145
            ];
146
    }
147
148
    /**
149
     * preConfigure
150
     *
151
     * If any computation to the configuration must be done before each session
152
     * creation, it goes here.
153
     *
154
     * @access protected
155
     * @return SessionBuilder $this
156
     */
157
    protected function preConfigure()
158
    {
159
        return $this;
160
    }
161
162
    /**
163
     * createConnection
164
     *
165
     * Connection instantiation.
166
     *
167
     * @access protected
168
     * @param  string   $dsn
169
     * @param  string|array $connection_configuration
170
     * @return Connection
171
     */
172
    protected function createConnection($dsn, $connection_configuration)
173
    {
174
        return new Connection($dsn, $connection_configuration);
0 ignored issues
show
Bug introduced by
It seems like $connection_configuration defined by parameter $connection_configuration on line 172 can also be of type string; however, PommProject\Foundation\S...nnection::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
175
    }
176
177
    /**
178
     * createSession
179
     *
180
     * Session instantiation.
181
     *
182
     * @access protected
183
     * @param  Connection   $connection
184
     * @param  ClientHolder $client_holder
185
     * @param  string|null  $stamp
186
     * @return Session
187
     */
188
    protected function createSession(Connection $connection, ClientHolder $client_holder, $stamp)
189
    {
190
        $session_class = $this->configuration->getParameter('class:session', '\PommProject\Foundation\Session\Session');
191
192
        return new $session_class($connection, $client_holder, $stamp);
193
    }
194
195
    /**
196
     * createClientHolder
197
     *
198
     * Instantiate ClientHolder.
199
     *
200
     * @access protected
201
     * @return ClientHolder
202
     */
203
    protected function createClientHolder()
204
    {
205
        return new ClientHolder();
206
    }
207
208
    /**
209
     * postConfigure
210
     *
211
     * Session configuration once created.
212
     * All pooler registration stuff goes here.
213
     *
214
     * @access protected
215
     * @param  Session          $session
216
     * @return SessionBuilder   $this
217
     */
218
    protected function postConfigure(Session $session)
219
    {
220
        return $this;
221
    }
222
223
    /**
224
     * initializeConverterHolder
225
     *
226
     * Converter initialization at startup.
227
     * If new converters are to be registered, it goes here.
228
     *
229
     * @access protected
230
     * @param  ConverterHolder  $converter_holder
231
     * @return SessionBuilder   $this
232
     */
233
    protected function initializeConverterHolder(ConverterHolder $converter_holder)
234
    {
235
        return $this;
236
    }
237
}
238