StashServiceProvider::fetchLogger()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 4
1
<?php
2
3
/**
4
 * This file is part of the m1\stash-silex library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/stash-silex
12
 * @version     0.1.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/stashsilex/blob/master/LICENSE
16
 * @link        http://github.com/m1/stashsilex/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\StashSilex;
20
21
use Silex\Application;
22
use Silex\ServiceProviderInterface;
23
24
/**
25
 * The StashServiceProvider provides the stash service to silex
26
 *
27
 * @since 0.1.0
28
 */
29
class StashServiceProvider implements ServiceProviderInterface
30
{
31
    /**
32
     * The session handler before this service provider checks to see if it should set a session handler
33
     *
34
     * @var null
35
     */
36
    private $session_handler;
37
38
    /**
39
     * Registers the service provider, sets the user defined options and sets the session handler
40
     *
41
     * @param \Silex\Application $app The silex app
42
     */
43 57
    public function register(Application $app)
44
    {
45 57
        $app['pool.default_options'] = array(
46 57
            'driver'  => 'Ephemeral',
47 57
            'session' => false,
48 57
            'logger'  => false,
49
        );
50
51
        $app['pools.options.init'] = $app->protect(function () use ($app) {
52 54
            static $init = false;
53
54 54
            if ($init) {
55 9
                return;
56
            }
57
58 54
            $init = true;
59
60 54
            $app['pools.options'] = self::createOptions($app);
61 57
        });
62
63
        $app['pools'] = $app->share(function () use ($app) {
64 51
            $app['pools.options.init']();
65
66 51
            return self::createPools($app);
67
68 57
        });
69
70 57
        $this->session_handler = isset($app['session.storage.handler']) ? $app['session.storage.handler'] : array();
71
72
        $app['session.storage.handler'] = $app->share(function () use ($app) {
73 12
            $app['pools.options.init']();
74
75 12
            foreach ($app['pools.options'] as $name => $options) {
76 12
                if (isset($options['session']) && $options['session']) {
77 9
                    $session_options = (is_array($options['session'])) ? $options['session'] : array();
78
79 9
                    return new StashSessionHandler($app['pools'][$name], $session_options);
80
                }
81 3
            }
82
83 3
            return $this->session_handler;
84 57
        });
85
86
        $app['pool'] = $app->share(function ($app) {
87 24
            $pools = $app['pools'];
88
89 21
            return $pools[$app['pools.default']];
90 57
        });
91 57
    }
92
93
    /**
94
     * The silex service provider boot function
95
     *
96
     * @param \Silex\Application $app The silex app
97
     *
98
     * @codeCoverageIgnore
99
     */
100
    public function boot(Application $app)
101
    {
102
    }
103
104
    /**
105
     * Creates and parses the options from the user options
106
     *
107
     * @param \Silex\Application $app The silex app
108
     *
109
     * @return array $options The parsed options
110
     */
111 54
    private static function createOptions($app)
112
    {
113 54
        if (!isset($app['pools.options'])) {
114 39
            $app['pools.options'] = array(
115 39
                'default' => isset($app['pool.options']) ? $app['pool.options'] : array()
116 39
            );
117 39
        }
118
119 54
        $tmp = $app['pools.options'];
120 54
        $options = array();
121
122 54
        foreach ($tmp as $name => $opts) {
123 54
            $options[$name] = array_replace($app['pool.default_options'], $opts);
124
125 54
            if (!isset($app['pools.default'])) {
126 54
                $app['pools.default'] = $name;
127 54
            }
128 54
        }
129
130 54
        return $options;
131
    }
132
133
    /**
134
     * Creates the pools with user defined options
135
     *
136
     * @param \Silex\Application $app The silex app
137
     *
138
     * @return \Pimple $pools The pools in the pimple container
139
     */
140 51
    private static function createPools($app)
141
    {
142 51
        $pools = new \Pimple();
143
144 51
        foreach ($app['pools.options'] as $name => $options) {
145 51
            $pool = new \Stash\Pool(self::fetchDriver($options));
146 48
            $pool->setLogger(self::fetchLogger($app, $options));
147
148 45
            $pools[$name] = $pools->share(function () use ($pool) {
149 42
                return $pool;
150 45
            });
151
152 45
        }
153
154 45
        return $pools;
155
    }
156
157
    /**
158
     * Fetches the driver if exists, else throws exception
159
     *
160
     * @param array $options The options for Stash
161
     *
162
     * @throws \InvalidArgumentException If the driver class can not be found
163
     *
164
     * @return mixed Instance of the driver class
165
     */
166 51
    private static function fetchDriver($options)
167
    {
168 51
        $driver = sprintf('Stash\Driver\%s', $options['driver']);
169
170 51
        if (!class_exists($driver)) {
171 3
            throw new \InvalidArgumentException(sprintf('\'%s\' driver class does not exist', $driver));
172
        }
173
174 48
        $driver = new \ReflectionClass($driver);
175
176 48
        $driver_options = (isset($options['options'])) ? $options['options'] : array();
177 48
        $driver = $driver->newInstanceArgs(array($driver_options));
178
179 48
        return $driver;
180
    }
181
182
    /**
183
     * Fetches the driver if exists, else throws exception
184
     *
185
     * @param \Silex\Application $app The silex app
186
     * @param array $options The options for Stash
187
     *
188
     * @throws \InvalidArgumentException If the logger service does not exist
189
     *
190
     * @return mixed The logger service
191
     */
192 48
    private static function fetchLogger($app, $options)
193
    {
194 48
        if (isset($options['logger']) && $options['logger']) {
195 6
            $logger = $options['logger'];
196
197 6
            if (!isset($app[$logger])) {
198 3
                throw new \InvalidArgumentException(
199 3
                    sprintf('\'%s\' logger not defined as service in app container', $logger)
200 3
                );
201
            }
202
203 3
            return $app[$options['logger']];
204
        }
205
206 42
        return null;
207
    }
208
}
209