Completed
Pull Request — master (#76)
by Thibaud
03:23
created

PhraseanetSDKServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[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
12
namespace PhraseanetSDK;
13
14
use Guzzle\Cache\DoctrineCacheAdapter;
15
use Guzzle\Plugin\Cache\CachePlugin;
16
use Guzzle\Plugin\History\HistoryPlugin;
17
use PhraseanetSDK\Cache\BackendCacheFactory;
18
use PhraseanetSDK\Cache\CanCacheStrategy;
19
use PhraseanetSDK\Cache\RevalidationFactory;
20
use PhraseanetSDK\Http\APIGuzzleAdapter;
21
use PhraseanetSDK\Http\ConnectedGuzzleAdapter;
22
use PhraseanetSDK\Http\Guzzle\GuzzleClient;
23
use PhraseanetSDK\Profiler\PhraseanetSDKDataCollector;
24
use PhraseanetSDK\Recorder\Filters\DuplicateFilter;
25
use PhraseanetSDK\Recorder\Filters\LimitFilter;
26
use PhraseanetSDK\Recorder\Filters\MonitorFilter;
27
use PhraseanetSDK\Recorder\Player;
28
use PhraseanetSDK\Recorder\Recorder;
29
use PhraseanetSDK\Recorder\RequestExtractor;
30
use PhraseanetSDK\Recorder\Storage\StorageFactory;
31
use Silex\Application as SilexApplication;
32
use Silex\ServiceProviderInterface;
33
34
/**
35
 * Phraseanet SDK Silex provider
36
 */
37
class PhraseanetSDKServiceProvider implements ServiceProviderInterface
38
{
39 17
    public function register(SilexApplication $app)
40
    {
41 17
        $app['recorder.config'] = $app['sdk.config'] = $app['cache.config'] = array();
42
43
        $app['phraseanet-sdk.recorder.config'] = $app->share(function () use ($app) {
44 9
            return array_replace_recursive(
45
                array(
46 9
                    'type' => 'file',
47
                    'options' => array(
48 9
                        'file' => realpath(__DIR__.'/../..').'/phraseanet.recorder.json',
49 9
                    ),
50 9
                    'limit' => 1000,
51 9
                ),
52 9
                $app['recorder.config']
53 9
            );
54 17
        });
55
56
        $app['phraseanet-sdk.config'] = $app->share(function () use ($app) {
57
            return array_replace(
58
                array(
59
                    'client-id' => null,
60
                    'secret'    => null,
61
                    'url'       => null,
62
                ),
63
                $app['sdk.config']
64
            );
65 17
        });
66
67
        $app['phraseanet-sdk.cache.config'] = $app->share(function () use ($app) {
68 10
            return array_replace(
69
                array(
70 10
                    'type' => 'array',
71 10
                    'ttl' => 300,
72 10
                ),
73 10
                $app['cache.config']
74 10
            );
75 17
        });
76
77
        $app['phraseanet-sdk.cache.factory'] = $app->share(function () use ($app) {
78 8
            return new BackendCacheFactory();
79 17
        });
80
81
        $app['phraseanet-sdk.cache.adapter'] = $app->share(function () use ($app) {
82 7
            $config = $app['phraseanet-sdk.cache.config'];
83
84 7
            $backend = $app['phraseanet-sdk.cache.factory']->create(
85 7
                $config['type'],
86 7
                isset($config['options']['host']) ? $config['options']['host'] : null,
87 7
                isset($config['options']['port']) ? $config['options']['port'] : null
88 7
            );
89
90 7
            return new DoctrineCacheAdapter($backend);
91 17
        });
92
93
        $app['phraseanet-sdk.cache.revalidation'] = $app->share(function (SilexApplication $app) {
94 9
            $config = $app['phraseanet-sdk.cache.config'];
95 9
            if (isset($config['revalidation']) && is_string($config['revalidation'])) {
96 2
                $factory = new RevalidationFactory();
97
98 2
                return $factory->create($config['revalidation']);
99 7
            } elseif (isset($config['revalidation'])) {
100
                return $config['revalidation'];
101
            }
102
103 7
            return;
104 17
        });
105
106
        $app['phraseanet-sdk.cache.can_cache'] = $app->share(function (SilexApplication $app) {
107 9
            $config = $app['phraseanet-sdk.cache.config'];
108 9
            if (isset($config['can_cache'])) {
109
                return $config['can_cache'];
110
            }
111
112 9
            return new CanCacheStrategy();
113 17
        });
114
115
        $app['phraseanet-sdk.cache.key_provider'] = $app->share(function (SilexApplication $app) {
116 9
            $config = $app['phraseanet-sdk.cache.config'];
117
118 9
            return isset($config['key_provider']) ? $config['key_provider'] : null;
119 17
        });
120
121
        $app['phraseanet-sdk.cache.plugin'] = $app->share(function () use ($app) {
122 6
            $cacheConfig = array_merge(
123 6
                $app['phraseanet-sdk.cache.config'],
124
                array(
125 6
                    'adapter' => $app['phraseanet-sdk.cache.adapter'],
126 6
                    'default_ttl' => $app['phraseanet-sdk.cache.config']['ttl'],
127 6
                    'key_provider' => $app['phraseanet-sdk.cache.key_provider'],
128 6
                    'can_cache' => $app['phraseanet-sdk.cache.can_cache'],
129 6
                    'revalidation' => $app['phraseanet-sdk.cache.revalidation'],
130
                )
131 6
            );
132
133 6
            return new CachePlugin($cacheConfig);
134 17
        });
135
136
        $app['phraseanet-sdk.guzzle.plugins'] = $app->share(function ($app) {
137
            $plugins = array(
138 5
                $app['phraseanet-sdk.cache.plugin'],
139 5
            );
140
141 5
            if (isset($app['profiler']) || $app['recorder.enabled']) {
142 2
                $plugins[] = $app['phraseanet-sdk.guzzle.history-plugin'];
143 2
            }
144
145 5
            return $plugins;
146 17
        });
147
148
        $app['phraseanet-sdk.guzzle-adapter'] = $app->share(function (SilexApplication $app) {
149 3
            return GuzzleClient::create(
150 3
                $app['phraseanet-sdk.config']['url'],
151 3
                $app['phraseanet-sdk.guzzle.plugins']
152 3
            );
153 17
        });
154
155
        $app['phraseanet-sdk.guzzle-connected-adapter'] = $app->protect(function ($token) use ($app) {
156 1
            return new ConnectedGuzzleAdapter($token, $app['phraseanet-sdk.guzzle-adapter']);
157 17
        });
158
159
        $app['phraseanet-sdk.guzzle-api-adapter'] = $app->protect(function ($token) use ($app) {
160 1
            return new APIGuzzleAdapter($app['phraseanet-sdk.guzzle-connected-adapter']($token));
161 17
        });
162
163
        $app['phraseanet-sdk'] = $app->share(function (SilexApplication $app) {
164 1
            return Application::create($app['phraseanet-sdk.config'], $app['phraseanet-sdk.guzzle-adapter']);
165 17
        });
166
167
        $app['phraseanet-sdk.guzzle.history-plugin'] = $app->share(function (SilexApplication $app) {
168 5
            $plugin = new HistoryPlugin();
169 5
            $plugin->setLimit($app['phraseanet-sdk.recorder.config']['limit']);
170
171 5
            return $plugin;
172 17
        });
173
174 17
        $app['recorder.enabled'] = false;
175
176 17
        if (isset($app['profiler'])) {
177 2
            $app['data_collectors'] = array_merge($app['data_collectors'], array(
178
                'phraseanet-sdk' => $app->share(function ($app) {
179 2
                    return new PhraseanetSDKDataCollector($app['phraseanet-sdk.guzzle.history-plugin']);
180 2
                }),
181 2
            ));
182 2
            $app['data_collector.templates'] = array_merge($app['data_collector.templates'], array(
183 2
                array('phrasea-sdk', '@PhraseanetSDK/Collector/phraseanet-sdk.html.twig'),
184 2
            ));
185
186 2
            $app['phraseanet-sdk.profiler.templates_path'] = __DIR__.'/Profiler/resources/views';
187
188 2
            $app['twig.loader.filesystem'] = $app->share($app->extend(
189 2
                'twig.loader.filesystem',
190
                function ($loader, $app) {
191 2
                    $loader->addPath($app['phraseanet-sdk.profiler.templates_path'], 'PhraseanetSDK');
192
193 2
                    return $loader;
194
                }
195 2
            ));
196 2
        }
197
198
        $app['phraseanet-sdk.recorder.storage-factory'] = $app->share(function (SilexApplication $app) {
199 2
            return new StorageFactory($app['phraseanet-sdk.cache.factory']);
200 17
        });
201
202
        $app['phraseanet-sdk.recorder.request-extractor'] = $app->share(function (SilexApplication $app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203 2
            return new RequestExtractor();
204 17
        });
205
206
        $app['phraseanet-sdk.recorder.storage'] = $app->share(function (SilexApplication $app) {
207 3
            $config = $app['phraseanet-sdk.recorder.config'];
208
209 3
            return $app['phraseanet-sdk.recorder.storage-factory']->create($config['type'], $config['options']);
210 17
        });
211
212
        $app['phraseanet-sdk.recorder.filters'] = $app->share(function (SilexApplication $app) {
213
            return array(
214 2
                new MonitorFilter(),
215 2
                new DuplicateFilter(),
216 2
                new LimitFilter($app['phraseanet-sdk.recorder.config']['limit']),
217 2
            );
218 17
        });
219
220
        $app['phraseanet-sdk.recorder'] = $app->share(function (SilexApplication $app) {
221 2
            $recorder = new Recorder(
222 2
                $app['phraseanet-sdk.guzzle.history-plugin'],
223 2
                $app['phraseanet-sdk.recorder.storage'],
224 2
                $app['phraseanet-sdk.recorder.request-extractor']
225 2
            );
226
227 2
            foreach ($app['phraseanet-sdk.recorder.filters'] as $filter) {
228 2
                $recorder->addFilter($filter);
229 2
            }
230
231 2
            return $recorder;
232 17
        });
233
234
        $app['phraseanet-sdk.player.factory'] = $app->protect(function ($token) use ($app) {
235 1
            return new Player(
236 1
                $app['phraseanet-sdk.guzzle-api-adapter']($token),
237 1
                $app['phraseanet-sdk.recorder.storage']
238 1
            );
239 17
        });
240 17
    }
241
242 11
    public function boot(SilexApplication $app)
243
    {
244 11
        if ($app['recorder.enabled']) {
245 2
            $app->finish(function () use ($app) {
246 1
                $app['phraseanet-sdk.recorder']->save();
247 2
            });
248 2
        }
249 11
    }
250
}
251