ContainersServiceProvider   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 45
c 3
b 1
f 0
dl 0
loc 131
ccs 0
cts 43
cp 0
rs 10
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAdminAnnotations() 0 9 1
A registerAdminConfig() 0 6 1
A register() 0 6 2
A hp$0 ➔ registerAdminTranslations() 0 44 2
registerAdminSchema() 0 11 ?
registerAdminModel() 0 10 ?
registerAdminTranslations() 0 44 ?
A hp$0 ➔ registerAdminModel() 0 10 3
registerAdminResource() 0 12 ?
A hp$0 ➔ readonly() 0 7 2
A hp$0 ➔ setReadonly() 0 5 1
A hp$0 ➔ registerAdminSchema() 0 11 2
A hp$0 ➔ registerAdminResource() 0 12 3
A hp$0 ➔ __construct() 0 3 1
1
<?php
2
3
namespace Terranet\Administrator\Providers;
4
5
use Doctrine\Common\Annotations\AnnotationRegistry;
6
use Doctrine\Common\Annotations\SimpleAnnotationReader;
7
use Doctrine\DBAL\Schema\AbstractSchemaManager;
8
use Illuminate\Config\Repository as Config;
0 ignored issues
show
Bug introduced by
The type Illuminate\Config\Repository 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...
9
use Illuminate\Foundation\Application;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Application 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...
10
use Illuminate\Routing\Router;
11
use Illuminate\Support\ServiceProvider;
12
use Terranet\Administrator\Architect;
13
use Terranet\Administrator\Contracts\Services\Finder;
14
use Terranet\Administrator\Schema;
15
use Terranet\Localizer\Locale;
16
17
class ContainersServiceProvider extends ServiceProvider
18
{
19
    protected $containers = [
20
        'AdminConfig' => 'scaffold.config',
21
        'AdminResource' => 'scaffold.module',
22
        'AdminModel' => 'scaffold.model',
23
        'AdminSchema' => 'scaffold.schema',
24
        'AdminTranslations' => 'scaffold.translations',
25
        'AdminAnnotations' => 'scaffold.annotations',
26
    ];
27
28
    public function register()
29
    {
30
        foreach (array_keys($this->containers) as $container) {
31
            $method = "register{$container}";
32
33
            \call_user_func_array([$this, $method], []);
34
        }
35
    }
36
37
    protected function registerAdminAnnotations()
38
    {
39
        $this->app->singleton('scaffold.annotations', function () {
40
            AnnotationRegistry::registerUniqueLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...:registerUniqueLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 ( Ignorable by Annotation )

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

40
            /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerUniqueLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
41
42
            $reader = new SimpleAnnotationReader();
43
            $reader->addNamespace('\\Terranet\\Administrator\\Annotations');
44
45
            return $reader;
46
        });
47
    }
48
49
    protected function registerAdminConfig()
50
    {
51
        $this->app->singleton('scaffold.config', function ($app) {
52
            $config = $app['config']['administrator'];
53
54
            return new Config((array) $config);
55
        });
56
    }
57
58
    protected function registerAdminTranslations()
59
    {
60
        // Draft: Mui configuration
61
        // Goal: sometimes there is a case when few content managers (admins) override the same translatable content (files, db, etc...)
62
        // This service allows to make some locales readonly:
63
        //  1. they are available in UI in order to preserve the context
64
        //  2. they are protected from saving process
65
        // Making locale(s) Readonly remains for Dev's side: the recommended way - use a custom Middleware.
66
        // ex.: app('scaffold.translations')->setReadonly([1, 2, 3])
67
        $this->app->singleton('scaffold.translations', function ($app) {
68
            return new class() {
69
                protected $readonly = [];
70
71
                public function __construct()
72
                {
73
                    $this->setReadonly(config('administrator.translations.readonly', []));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
                    $this->setReadonly(/** @scrutinizer ignore-call */ config('administrator.translations.readonly', []));
Loading history...
74
                }
75
76
                /**
77
                 * Set ReadOnly locales.
78
                 *
79
                 * @param  array  $readonly
80
                 * @return self
81
                 */
82
                public function setReadonly(array $readonly = []): self
83
                {
84
                    $this->readonly = (array) $readonly;
85
86
                    return $this;
87
                }
88
89
                /**
90
                 * Check if a Locale is ReadOnly.
91
                 *
92
                 * @param $locale
93
                 * @return bool
94
                 */
95
                public function readonly($locale)
96
                {
97
                    if ($locale instanceof Locale) {
98
                        $locale = $locale->id();
99
                    }
100
101
                    return \in_array((int) $locale, $this->readonly, true);
102
                }
103
            };
104
        });
105
    }
106
107
    protected function registerAdminResource()
108
    {
109
        $this->app->singleton('scaffold.module', function (Application $app) {
110
            /** @var Router $router */
111
            $router = $app['router']->current();
112
113
            if (in_array($router->getName(), ['scaffold.settings.edit', 'scaffold.settings.update'], true)) {
114
                return Architect::resourceForKey('settings');
115
            }
116
117
            if ($key = $router->parameter('module')) {
118
                return Architect::resourceForKey($key);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type Illuminate\Routing\RouteRegistrar; however, parameter $url of Terranet\Administrator\Architect::resourceForKey() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

118
                return Architect::resourceForKey(/** @scrutinizer ignore-type */ $key);
Loading history...
119
            }
120
        });
121
    }
122
123
    protected function registerAdminModel()
124
    {
125
        $this->app->singleton('scaffold.model', function (Application $app) {
126
            /** @var int $id */
127
            $id = (int) $app['router']->current()->parameter('id');
128
129
            /** @var Finder $finder */
130
            $finder = app('scaffold.module')->finder();
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

130
            $finder = /** @scrutinizer ignore-call */ app('scaffold.module')->finder();
Loading history...
131
            if ($id && $finder) {
132
                return $finder->find($id);
133
            }
134
        });
135
    }
136
137
    protected function registerAdminSchema()
138
    {
139
        $this->app->singleton('scaffold.schema', function (Application $app) {
140
            /** @var AbstractSchemaManager $schema */
141
            if ($schema = $app['db']->connection()->getDoctrineSchemaManager()) {
142
                // fix dbal missing types
143
                $platform = $schema->getDatabasePlatform();
144
                $platform->registerDoctrineTypeMapping('enum', 'string');
145
                $platform->registerDoctrineTypeMapping('set', 'string');
146
147
                return new Schema($schema);
148
            }
149
        });
150
    }
151
}
152