ArangoDbServiceProvider::edgeManagement()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 33
Code Lines 17

Duplication

Lines 33
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 33
loc 33
rs 8.8571
cc 3
eloc 17
nc 1
nop 1
1
<?php
2
3
namespace SilexArangoDb\Silex\Provider;
4
5
use Silex\Application;
6
use Silex\ServiceProviderInterface;
7
use triagens\ArangoDb\AdminHandler;
8
use triagens\ArangoDb\Collection;
9
use triagens\ArangoDb\CollectionHandler;
10
use triagens\ArangoDb\ConnectionOptions;
11
use triagens\ArangoDb\Document;
12
use triagens\ArangoDb\DocumentHandler;
13
use triagens\ArangoDb\Edge;
14
use triagens\ArangoDb\EdgeHandler;
15
use triagens\ArangoDb\Graph;
16
use triagens\ArangoDb\GraphHandler;
17
use triagens\ArangoDb\Statement;
18
use triagens\ArangoDb\Transaction;
19
use triagens\ArangoDb\UpdatePolicy;
20
use triagens\ArangoDb\Connection;
21
use triagens\ArangoDb\UserHandler;
22
23
class ArangoDbServiceProvider implements ServiceProviderInterface
24
{
25
    public function register(Application $app)
26
    {
27
        $app['arangodb.default_options'] = array(
28
            // database name
29
            ConnectionOptions::OPTION_DATABASE => '_system',
30
            // server endpoint to connect to
31
            ConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529',
32
            // authorization type to use (currently supported: 'Basic')
33
            ConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
34
            // user for basic authorization
35
            ConnectionOptions::OPTION_AUTH_USER => 'root',
36
            // password for basic authorization
37
            ConnectionOptions::OPTION_AUTH_PASSWD => '',
38
            // connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
39
            ConnectionOptions::OPTION_CONNECTION => 'Close',
40
            // connect timeout in seconds
41
            ConnectionOptions::OPTION_TIMEOUT => 3,
42
            // whether or not to reconnect when a keep-alive connection has timed out on server
43
            ConnectionOptions::OPTION_RECONNECT => true,
44
            // optionally create new collections when inserting documents
45
            ConnectionOptions::OPTION_CREATE => true,
46
            // optionally create new collections when inserting documents
47
            ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST,
48
        );
49
50
        $app['arangodbs.options.initializer'] = $app->protect(
51
            function () use ($app) {
52
                static $initialized = false;
53
54
                if ($initialized) {
55
                    return;
56
                }
57
58
                $initialized = true;
59
60
                if (!isset($app['arangodbs.options'])) {
61
                    $app['arangodbs.options'] = array(
62
                        'default' => isset($app['arangodb.options']) ? $app['arangodb.options'] : array()
63
                    );
64
                }
65
66
                $tmp = $app['arangodbs.options'];
67
                foreach ($tmp as $name => &$options) {
68
                    $options = array_replace_recursive($app['arangodb.default_options'], $options);
69
70
                    if (!isset($app['arangodbs.default'])) {
71
                        $app['arangodbs.default'] = $name;
72
                    }
73
                }
74
75
                $app['arangodbs.options'] = $tmp;
76
            }
77
        );
78
79
        $app['arangodbs'] = $app->share(
80
            function ($app) {
81
                $app['arangodbs.options.initializer']();
82
83
                $dbs = new \Pimple();
84
                foreach (array_keys($app['arangodbs.options']) as $name) {
85
                    if ($app['arangodbs.default'] === $name) {
86
                        // we use shortcuts here in case the default has been overridden
87
                        $config = $app['arangodb.config'];
88
                    } else {
89
                        $config = $app['arangodbs.config'][$name];
90
                    }
91
92
                    $dbs[$name] = $dbs->share(
93
                        function () use ($config) {
94
                            return new Connection($config->getAll());
95
                        }
96
                    );
97
                }
98
99
                return $dbs;
100
            }
101
        );
102
103
        $app['arangodb'] = $app->share(
104
            function ($app) {
105
                $dbs = $app['arangodbs'];
106
107
                return $dbs[$app['arangodbs.default']];
108
            }
109
        );
110
111
        $app['arangodbs.config'] = $app->share(
112
            function ($app) {
113
                $app['arangodbs.options.initializer']();
114
115
                $configs = new \Pimple();
116
                foreach ($app['arangodbs.options'] as $name => $options) {
117
                    $configs[$name] = new ConnectionOptions($options);
118
                }
119
120
                return $configs;
121
            }
122
        );
123
124
        $app['arangodb.config'] = $app->share(
125
            function ($app) {
126
                $dbs = $app['arangodbs.config'];
127
128
                return $dbs[$app['arangodbs.default']];
129
            }
130
        );
131
132
        $this->collectionManagement($app);
133
        $this->documentManagement($app);
134
        $this->edgeManagement($app);
135
        $this->graphManagement($app);
136
        $this->statementManagement($app);
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function boot(Application $app)
143
    {
144
    }
145
146 View Code Duplication
    protected function collectionManagement(Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
147
    {
148
        $app['arangodb.collection'] = $app->protect(
149
            function (array $data = []) use ($app) {
150
                if (!empty($data)) {
151
                    return Collection::createFromArray($data);
152
                }
153
154
                return new Collection();
155
            }
156
        );
157
158
        $app['arangodb.collection_handler'] = $app->share(
159
            function ($app) {
160
                $db = $app['arangodb'];
161
162
                return new CollectionHandler($db);
163
            }
164
        );
165
166
        $app['arangodbs.collection_handler'] = $app->share(
167
            function ($app) {
168
                $app['arangodbs.options.initializer']();
169
170
                $handlers = new \Pimple();
171
                foreach ($app['arangodbs.options'] as $name => $options) {
172
                    $handlers[$name] = new CollectionHandler($app['arangodbs'][$name]);
173
                }
174
175
                return $handlers;
176
            }
177
        );
178
    }
179
180 View Code Duplication
    protected function documentManagement(Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
181
    {
182
        $app['arangodb.document'] = $app->protect(
183
            function (array $data = []) use ($app) {
184
                if (!empty($data)) {
185
                    return Document::createFromArray($data);
186
                }
187
                return new Document();
188
            }
189
        );
190
191
        $app['arangodb.document_handler'] = $app->share(
192
            function ($app) {
193
                $db = $app['arangodb'];
194
195
                return new DocumentHandler($db);
196
            }
197
        );
198
199
        $app['arangodbs.document_handler'] = $app->share(
200
            function ($app) {
201
                $app['arangodbs.options.initializer']();
202
203
                $handlers = new \Pimple();
204
                foreach ($app['arangodbs.options'] as $name => $options) {
205
                    $handlers[$name] = new DocumentHandler($app['arangodbs'][$name]);
206
                }
207
208
                return $handlers;
209
            }
210
        );
211
    }
212
213 View Code Duplication
    protected function edgeManagement(Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
214
    {
215
        $app['arangodb.edge'] = $app->protect(
216
            function (array $data = []) use ($app) {
217
                if (!empty($data)) {
218
                    return Edge::createFromArray($data);
219
                }
220
221
                return new Edge();
222
            }
223
        );
224
225
        $app['arangodb.edge_handler'] = $app->share(
226
            function ($app) {
227
                $db = $app['arangodb'];
228
229
                return new EdgeHandler($db);
230
            }
231
        );
232
233
        $app['arangodbs.edge_handler'] = $app->share(
234
            function ($app) {
235
                $app['arangodbs.options.initializer']();
236
237
                $handlers = new \Pimple();
238
                foreach ($app['arangodbs.options'] as $name => $options) {
239
                    $handlers[$name] = new EdgeHandler($app['arangodbs'][$name]);
240
                }
241
242
                return $handlers;
243
            }
244
        );
245
    }
246
247 View Code Duplication
    protected function graphManagement(Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
248
    {
249
        $app['arangodb.graph'] = $app->protect(
250
            function (array $data = []) use ($app) {
251
                if (!empty($data)) {
252
                    return Graph::createFromArray($data);
253
                }
254
255
                return new Graph();
256
            }
257
        );
258
259
        $app['arangodb.graph_handler'] = $app->share(
260
            function ($app) {
261
                $db = $app['arangodb'];
262
263
                return new GraphHandler($db);
264
            }
265
        );
266
267
        $app['arangodbs.graph_handler'] = $app->share(
268
            function ($app) {
269
                $app['arangodbs.options.initializer']();
270
271
                $handlers = new \Pimple();
272
                foreach ($app['arangodbs.options'] as $name => $options) {
273
                    $handlers[$name] = new GraphHandler($app['arangodbs'][$name]);
274
                }
275
276
                return $handlers;
277
            }
278
        );
279
    }
280
281
    protected function statementManagement(Application $app)
282
    {
283
        $app['arangodb.statement'] = $app->protect(
284
            function (array $query) use ($app) {
285
                $db = $app['arangodb'];
286
287
                $statement = new Statement($db, $query);
288
289
                return $statement->execute();
290
            }
291
        );
292
293
        $app['arangodbs.statement'] = $app->protect(
294
            function (array $query) use ($app) {
295
                $app['arangodbs.options.initializer']();
296
297
                $handlers = new \Pimple();
298
                foreach ($app['arangodbs.options'] as $name => $options) {
299
                    $handlers[$name] = new Statement($app['arangodbs'][$name], $query);
300
                }
301
302
                return $handlers;
303
            }
304
        );
305
    }
306
}
307