Completed
Push — master ( 7f2a82...01ce55 )
by Attila
04:30
created

Concord   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
c 1
b 0
f 0
dl 0
loc 270
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAlias() 0 3 1
A model() 0 3 1
A getModules() 0 10 2
A registerHelper() 0 5 1
A __construct() 0 5 1
A registerModel() 0 18 3
A registerModule() 0 11 3
A registerRequest() 0 11 2
A getEnumBindings() 0 3 1
A resetProxy() 0 3 1
A short() 0 3 1
A getConvention() 0 3 1
A helper() 0 3 1
A registerEnum() 0 12 2
A getModelBindings() 0 3 1
A getVersion() 0 3 1
A module() 0 3 1
A getRequestBindings() 0 3 1
A registerShort() 0 5 1
A enum() 0 3 1
A mergeModuleConfig() 0 8 3
1
<?php
2
/**
3
 * Contains the Concord class.
4
 *
5
 * @copyright   Copyright (c) 2016 Attila Fulop
6
 * @author      Attila Fulop
7
 * @license     MIT
8
 * @since       2016-08-14
9
 *
10
 */
11
12
13
namespace Konekt\Concord;
14
15
use Illuminate\Contracts\Foundation\Application;
16
use Illuminate\Foundation\AliasLoader;
17
use Illuminate\Support\Collection;
18
use InvalidArgumentException;
19
use Konekt\Concord\Contracts\Concord as ConcordContract;
20
use Konekt\Concord\Contracts\Convention;
21
use Konekt\Concord\Events\EnumWasRegistered;
22
use Konekt\Concord\Events\HelperWasRegistered;
23
use Konekt\Concord\Events\ModelWasRegistered;
24
use Konekt\Concord\Events\RequestWasRegistered;
25
use Route;
26
27
class Concord implements ConcordContract
28
{
29
    const VERSION = '1.1.0';
30
31
    /** @var Collection  */
32
    protected $modules;
33
34
    /** @var array */
35
    protected $models = [];
36
37
    /** @var array */
38
    protected $enums = [];
39
40
    /** @var array */
41
    protected $requests = [];
42
43
    /** @var  array */
44
    protected $implicitModules = [];
45
46
    /** @var  Loader */
0 ignored issues
show
Bug introduced by
The type Konekt\Concord\Loader 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...
47
    protected $loader;
48
49
    /** @var  Application */
50
    protected $app;
51
52
    /** @var  array */
53
    protected $shorts = [];
54
55
    /** @var Convention */
56
    private $convention;
57
58
    /**
59
     * Concord class constructor
60
     *
61
     * @param Application $app
62
     * @param Convention  $convention
63
     */
64
    public function __construct(Application $app, Convention $convention)
65
    {
66
        $this->modules    = Collection::make();
67
        $this->app        = $app;
68
        $this->convention = $convention;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function registerModule($moduleClass, $config = [])
75
    {
76
        $this->mergeModuleConfig(concord_module_id($moduleClass), $config);
77
78
        $module = $this->app->register($moduleClass);
79
80
        $this->modules->put($module->getId(), $module);
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Illuminate\Support\ServiceProvider. It seems like you code against a sub-type of Illuminate\Support\ServiceProvider such as Illuminate\Foundation\Su...rs\RouteServiceProvider or Konekt\Concord\BaseServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $this->modules->put($module->/** @scrutinizer ignore-call */ getId(), $module);
Loading history...
81
        $implicit = isset($config['implicit']) ? $config['implicit'] : false;
82
83
        if ($implicit) {
84
            $this->implicitModules[get_class($module)] = true;
85
        }
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function registerHelper($name, $class)
92
    {
93
        $this->app->singleton('concord.helpers.' . $name, $class);
94
95
        event(new HelperWasRegistered($name, $class));
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function getModules($includeImplicits = false) : Collection
102
    {
103
        if ($includeImplicits) {
104
            return $this->modules;
105
        }
106
107
        $implicitModules = $this->implicitModules;
108
109
        return $this->modules->reject(function ($module) use ($implicitModules) {
110
            return array_key_exists(get_class($module), $implicitModules);
111
        });
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function registerAlias($alias, $concrete)
118
    {
119
        AliasLoader::getInstance()->alias($alias, $concrete);
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function registerModel(string $abstract, string $concrete, $registerRouteModel = true)
126
    {
127
        if (!is_subclass_of($concrete, $abstract, true)) {
128
            throw new InvalidArgumentException("Class {$concrete} must extend or implement {$abstract}. ");
129
        }
130
131
        $this->models[$abstract] = $concrete;
132
        $this->app->alias($concrete, $abstract);
133
        $this->registerShort($abstract, 'model');
134
135
        // Route models can't resolve models by interface
136
        // so we're registering them explicitly
137
        if ($registerRouteModel) {
138
            Route::model(shorten($abstract), $concrete);
139
        }
140
141
        $this->resetProxy($this->getConvention()->proxyForModelContract($abstract));
142
        event(new ModelWasRegistered($abstract, $concrete, $registerRouteModel));
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    public function model(string $abstract)
149
    {
150
        return array_get($this->models, $abstract);
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function module(string $id)
157
    {
158
        return $this->modules->get($id);
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function getModelBindings() : Collection
165
    {
166
        return collect($this->models);
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function getConvention(): Convention
173
    {
174
        return $this->convention;
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function registerEnum(string $abstract, string $concrete)
181
    {
182
        if (!is_subclass_of($concrete, $abstract, true)) {
183
            throw new InvalidArgumentException("Class {$concrete} must extend or implement {$abstract}. ");
184
        }
185
186
        $this->enums[$abstract] = $concrete;
187
        $this->app->alias($concrete, $abstract);
188
        $this->registerShort($abstract, 'enum');
189
190
        $this->resetProxy($this->getConvention()->proxyForEnumContract($abstract));
191
        event(new EnumWasRegistered($abstract, $concrete));
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197
    public function registerRequest(string $abstract, string $concrete)
198
    {
199
        if (!is_subclass_of($concrete, $abstract, true)) {
200
            throw new InvalidArgumentException("Class {$concrete} must extend or implement {$abstract}. ");
201
        }
202
203
        $this->requests[$abstract] = $concrete;
204
        $this->app->alias($concrete, $abstract);
205
        $this->registerShort($abstract, 'request');
206
207
        event(new RequestWasRegistered($abstract, $concrete));
208
    }
209
210
    /**
211
     * @inheritDoc
212
     */
213
    public function enum(string $abstract)
214
    {
215
        return array_get($this->enums, $abstract);
216
    }
217
218
    /**
219
     * @inheritDoc
220
     */
221
    public function getEnumBindings(): Collection
222
    {
223
        return collect($this->enums);
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229
    public function getRequestBindings(): Collection
230
    {
231
        return collect($this->requests);
232
    }
233
234
    /**
235
     * @inheritdoc
236
     */
237
    public function helper($name, $arguments = [])
238
    {
239
        return $this->app->make('concord.helpers.' . $name, $arguments);
240
    }
241
242
    /**
243
     * @inheritdoc
244
     */
245
    public function getVersion(): string
246
    {
247
        return self::VERSION;
248
    }
249
250
    /**
251
     * @inheritdoc
252
     */
253
    public function short($name)
254
    {
255
        return array_get($this->shorts, "$name.class");
256
    }
257
258
    /**
259
     * Resets the proxy class to ensure no stale instance gets stuck
260
     *
261
     * @param string $proxyClass
262
     */
263
    protected function resetProxy($proxyClass)
264
    {
265
        $proxyClass::__reset();
266
    }
267
268
269
    /**
270
     * Register a model/enum/request shorthand
271
     *
272
     * @param string    $abstract
273
     * @param string    $type
274
     */
275
    protected function registerShort($abstract, $type)
276
    {
277
        $this->shorts[shorten($abstract)] = [
278
            'type'  => $type,
279
            'class' => $abstract
280
        ];
281
    }
282
283
    /**
284
     * Merge the module's config with the existing config
285
     *
286
     * @param string $key
287
     * @param array  $config
288
     */
289
    protected function mergeModuleConfig(string $key, array $config)
290
    {
291
        if (!empty($config)) {
292
            $current = $this->app['config']->get($key);
293
            if (is_array($current)) {
294
                $this->app['config']->set($key, array_merge($current, $config));
295
            } else {
296
                $this->app['config']->set($key, $config);
297
            }
298
        }
299
    }
300
}
301