ModelServiceProvider::registerModelDependencies()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 59
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 4
eloc 32
nc 8
nop 1
dl 0
loc 59
rs 9.408
c 6
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Charcoal\Model\ServiceProvider;
4
5
// From Pimple
6
use Charcoal\Cms\ConfigInterface;
0 ignored issues
show
Bug introduced by
The type Charcoal\Cms\ConfigInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Charcoal\Factory\GenericResolver;
8
use Pimple\Container;
9
use Pimple\ServiceProviderInterface;
10
11
// From 'charcoal-factory'
12
use Charcoal\Factory\GenericFactory as Factory;
13
14
// From 'charcoal-property'
15
use Charcoal\Property\PropertyInterface;
16
use Charcoal\Property\GenericProperty;
17
18
// From 'charcoal-core'
19
use Charcoal\Loader\CollectionLoader;
20
use Charcoal\Model\Collection;
21
use Charcoal\Model\ModelInterface;
22
use Charcoal\Model\Service\MetadataConfig;
23
use Charcoal\Model\Service\MetadataLoader;
24
use Charcoal\Model\Service\ModelBuilder;
25
use Charcoal\Model\Service\ModelLoaderBuilder;
26
use Charcoal\Source\SourceInterface;
27
use Charcoal\Source\DatabaseSource;
28
29
/**
30
 * Model Service Providers.
31
 *
32
 * ##Container dependencies
33
 *
34
 * The following keys are expected to be set on the container
35
 * (from external sources / providers):
36
 *
37
 * - `cache` A PSR-6 caching pool.
38
 * - `config` A charcoal app config (\Charcoal\Config\ConfigInterface)q
39
 * - `database` A PDO database instance
40
 * - `logger` A PSR-3 logger.
41
 * - `view` A \Charcoal\View\ViewInterface instance
42
 *
43
 * ## Services
44
 *
45
 * The following services are registered on the container:
46
 *
47
 * - `model/factory` A \Charcoal\Factory\FactoryInterface factory to create models.
48
 * - `model/collection/loader` A collection loader (should not be used).
49
 */
50
class ModelServiceProvider implements ServiceProviderInterface
51
{
52
    /**
53
     * @param Container $container A Pimple DI container.
54
     * @return void
55
     */
56
    public function register(Container $container)
57
    {
58
        $this->registerModelDependencies($container);
59
        $this->registerMetadataDependencies($container);
60
        $this->registerBuilderDependencies($container);
61
        $this->registerCollectionDependencies($container);
62
    }
63
64
    /**
65
     * @param Container $container A Pimple DI container.
66
     * @return void
67
     */
68
    protected function registerBuilderDependencies(Container $container)
69
    {
70
        /**
71
         * @param Container $container A Pimple DI container.
72
         * @return \Charcoal\Factory\FactoryInterface
73
         */
74
        $container['model/factory'] = function (Container $container) {
75
            return new Factory([
76
                'base_class' => ModelInterface::class,
77
                'arguments'  => [ $container['model/dependencies'] ]
78
            ]);
79
        };
80
81
        /**
82
         * @param Container $container A Pimple DI container.
83
         * @return ModelBuilder
84
         */
85
        $container['model/builder'] = function (Container $container) {
86
            return new ModelBuilder([
87
                'factory'           => $container['model/factory'],
88
                'metadata_loader'   => $container['metadata/loader'],
89
                'source_factory'    => $container['source/factory']
90
            ]);
91
        };
92
93
        /**
94
         * @param Container $container A Pimple DI container.
95
         * @return ModelLoaderBuilder
96
         */
97
        $container['model/loader/builder'] = function (Container $container) {
98
            return new ModelLoaderBuilder([
99
                'factory' => $container['model/factory'],
100
                'cache'   => $container['cache']
101
            ]);
102
        };
103
    }
104
105
    /**
106
     * @param Container $container A Pimple DI container.
107
     * @return void
108
     */
109
    protected function registerCollectionDependencies(Container $container)
110
    {
111
        /** The default collection class name. */
112
        $container['model/collection/class'] = Collection::class;
113
114
        /**
115
         * @param Container $container A Pimple DI container.
116
         * @return \ArrayAccess|\Traversable
117
         */
118
        $container['model/collection'] = $container->factory(function (Container $container) {
119
            return new $container['model/collection/class'];
120
        });
121
122
        /**
123
         * @param Container $container A Pimple DI container.
124
         * @return CollectionLoader
125
         */
126
        $container['model/collection/loader'] = $container->factory(function (Container $container) {
127
            $factory = $container['model/collection/loader/factory'];
128
            return $factory->create($factory->defaultClass());
129
        });
130
131
        /**
132
         * @param Container $container A Pimple DI container.
133
         * @return \Charcoal\Factory\FactoryInterface
134
         */
135
        $container['model/collection/loader/factory'] = function (Container $container) {
136
            return new Factory([
137
                'default_class' => CollectionLoader::class,
138
                'arguments'     => [[
139
                    'logger'        => $container['logger'],
140
                    'factory'       => $container['model/factory'],
141
                    'collection'    => $container['model/collection/class']
142
                ]]
143
            ]);
144
        };
145
    }
146
147
    /**
148
     * @param Container $container A Pimple DI container.
149
     * @return void
150
     */
151
    protected function registerModelDependencies(Container $container)
152
    {
153
        // The model dependencies might be already set from elsewhere; defines it if not.
154
        if (!isset($container['model/dependencies'])) {
155
            /**
156
             * @param Container $container A Pimple DI container.
157
             * @return array The model dependencies array.
158
             */
159
            $container['model/dependencies'] = function (Container $container) {
160
                return [
161
                    'container'        => $container,
162
                    'logger'           => $container['logger'],
163
                    'view'             => $container['view'],
164
                    'property_factory' => $container['property/factory'],
165
                    'metadata_loader'  => $container['metadata/loader'],
166
                    'source_factory'   => $container['source/factory']
167
                ];
168
            };
169
        }
170
171
        // The property factory might be already set from elsewhere; defines it if not.
172
        if (!isset($container['property/factory'])) {
173
            /**
174
             * @param Container $container A Pimple DI container.
175
             * @return \Charcoal\Factory\FactoryInterface
176
             */
177
            $container['property/factory'] = function (Container $container) {
178
                return new Factory([
179
                    'base_class'       => PropertyInterface::class,
180
                    'default_class'    => GenericProperty::class,
181
                    'resolver_options' => [
182
                        'prefix' => '\\Charcoal\\Property\\',
183
                        'suffix' => 'Property'
184
                    ],
185
                    'arguments' => [[
186
                        'container'  => $container,
187
                        'database'   => $container['database'],
188
                        'logger'     => $container['logger'],
189
                        'translator' => $container['translator']
190
                    ]]
191
                ]);
192
            };
193
        }
194
195
        if (!isset($container['source/factory'])) {
196
            /**
197
             * @param Container $container A Pimple DI container.
198
             * @return \Charcoal\Factory\FactoryInterface
199
             */
200
            $container['source/factory'] = function (Container $container) {
201
                return new Factory([
202
                    'map' => [
203
                        'database' => DatabaseSource::class
204
                    ],
205
                    'base_class' => SourceInterface::class,
206
                    'arguments'  => [[
207
                        'logger' => $container['logger'],
208
                        'cache'  => $container['cache'],
209
                        'pdo'    => $container['database']
210
                    ]]
211
                ]);
212
            };
213
        }
214
    }
215
216
    /**
217
     * @param Container $container A Pimple DI container.
218
     * @return void
219
     */
220
    protected function registerMetadataDependencies(Container $container)
221
    {
222
        if (!isset($container['metadata/config'])) {
223
            /**
224
             * The application's configset for "config.metadata".
225
             *
226
             * @param  Container $container Pimple DI container.
227
             * @return MetadataConfig
228
             */
229
            $container['metadata/config'] = function (Container $container) {
230
                $appConfig  = isset($container['config']) ? $container['config'] : [];
231
                $metaConfig = isset($appConfig['metadata']) ? $appConfig['metadata'] : null;
232
233
                $config = new MetadataConfig($metaConfig);
234
235
                $extraMetadataPaths = [];
236
                $basePath = $appConfig['base_path'];
237
                foreach ($container['module/classes'] as $module) {
238
                    if (defined(sprintf('%s::APP_CONFIG', $module))) {
239
                        $moduleConfig = $module::APP_CONFIG;
240
                        $extraMetadataPaths = array_merge(
241
                            $extraMetadataPaths,
242
                            $config->loadFile($basePath.$moduleConfig)['metadata']['paths']
243
                        );
244
                    };
245
                }
246
247
                if (!empty($extraMetadataPaths)) {
248
                    $config->addPaths($extraMetadataPaths);
249
                }
250
251
                return $config;
252
            };
253
        }
254
255
        if (!isset($container['metadata/cache'])) {
256
            /**
257
             * The application's metadata source cache.
258
             *
259
             * @param  Container $container A container instance.
260
             * @return \Psr\Cache\CacheItemPoolInterface|null
261
             */
262
            $container['metadata/cache'] = function (Container $container) {
263
                $cache = $container['metadata/config']['cache'];
264
                if (!is_object($cache)) {
265
                    if (is_bool($cache)) {
266
                        return $cache
267
                               ? $container['cache']
268
                               : $container['cache/builder']->build('memory');
269
                    }
270
271
                    if (is_array($cache)) {
272
                        return $container['cache/builder']->build($cache);
273
                    }
274
                }
275
276
                return $cache;
277
            };
278
        }
279
280
        if (!isset($container['metadata/loader'])) {
281
            /**
282
             * The application's metadata source loader and factory.
283
             *
284
             * @param  Container $container A Pimple DI container.
285
             * @return MetadataLoader
286
             */
287
            $container['metadata/loader'] = function (Container $container) {
288
                $appConfig  = $container['config'];
289
                $metaConfig = $container['metadata/config'];
290
291
                return new MetadataLoader([
292
                    'logger'    => $container['logger'],
293
                    'cache'     => $container['metadata/cache'],
294
                    'paths'     => $metaConfig['paths'],
295
                    'base_path' => $appConfig['base_path'],
296
                ]);
297
            };
298
        }
299
    }
300
}
301