Completed
Push — master ( aafa3b...fcd775 )
by Derek Stephen
02:04 queued 17s
created

ApplicationPackage   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 14
dl 0
loc 240
ccs 0
cts 138
cp 0
rs 9.84
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addToContainer() 0 14 1
A setConfigArray() 0 6 2
A setupViewEngine() 0 5 1
B setupModules() 0 45 10
A setupTranslator() 0 6 1
A setupPdoConnection() 0 5 1
A setupDownloadController() 0 8 1
A setupRouteFirewall() 0 5 1
A setupLogs() 0 5 1
A setupModuleViewOverrides() 0 11 2
A overrideViewFolder() 0 8 2
A getEntityPath() 0 4 1
A hasEntityPath() 0 4 1
A initMiddlewareStack() 0 5 1
A setupMiddlewareStack() 0 15 5
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Barnacle\Container;
6
use Barnacle\Exception\NotFoundException;
7
use Barnacle\RegistrationInterface;
8
use Bone\Db\DbPackage;
9
use Bone\Firewall\FirewallPackage;
10
use Bone\Http\Middleware\Stack;
11
use Bone\Http\MiddlewareAwareInterface;
12
use Bone\I18n\I18nPackage;
13
use Bone\I18n\I18nRegistrationInterface;
14
use Bone\I18n\View\Extension\LocaleLink;
15
use Bone\I18n\View\Extension\Translate;
16
use Bone\Log\LogPackage;
17
use Bone\Controller\DownloadController;
18
use Bone\Mvc\Router;
19
use Bone\Router\Decorator\ExceptionDecorator;
20
use Bone\Router\Decorator\NotAllowedDecorator;
21
use Bone\Router\Decorator\NotFoundDecorator;
22
use Bone\Router\PlatesStrategy;
23
use Bone\Router\RouterConfigInterface;
24
use Bone\View\Extension\Plates\AlertBox;
25
use Bone\View\ViewEngine;
26
use Bone\View\Helper\Paginator;
27
use Bone\View\PlatesEngine;
28
use Bone\I18n\Service\TranslatorFactory;
29
use Bone\View\ViewPackage;
30
use League\Plates\Template\Folders;
31
use League\Route\Strategy\ApplicationStrategy;
32
use League\Route\Strategy\JsonStrategy;
33
use Locale;
34
use PDO;
35
use Laminas\Diactoros\ResponseFactory;
36
use Laminas\I18n\Translator\Loader\Gettext;
37
use Laminas\I18n\Translator\Translator;
38
use Psr\Http\Server\MiddlewareInterface;
39
40
class ApplicationPackage implements RegistrationInterface
41
{
42
    /** @var array $config */
43
    private $config;
44
45
    /** @var Router $router */
46
    private $router;
47
48
    /** @var bool $i18nEnabledSite */
49
    private $i18nEnabledSite = false;
50
51
    /** @var array $supportedLocales */
52
    private $supportedLocales = [];
53
54
    /**
55
     * ApplicationPackage constructor.
56
     * @param array $config
57
     * @param Router $router
58
     */
59
    public function __construct(array $config, Router $router)
60
    {
61
        $this->config = $config;
62
        $this->router = $router;
63
    }
64
65
    /**
66
     * @param Container $c
67
     * @throws \Bone\Exception
68
     * @throws \Bone\Exception
69
     */
70
    public function addToContainer(Container $c)
71
    {
72
        $this->setConfigArray($c);
73
        $this->setupLogs($c);
74
        $this->setupPdoConnection($c);
75
        $this->setupViewEngine($c);
76
        $this->initMiddlewareStack($c);
77
        $this->setupTranslator($c);
78
        $this->setupModules($c);
79
        $this->setupModuleViewOverrides($c);
80
        $this->setupDownloadController($c);
81
        $this->setupRouteFirewall($c);
82
        $this->setupMiddlewareStack($c);
83
    }
84
85
    /**
86
     * @param Container $c
87
     */
88
    private function setConfigArray(Container $c)
89
    {
90
        foreach ($this->config as $key => $value) {
91
            $c[$key] = $value;
92
        }
93
    }
94
95
    /**
96
     * @param Container $c
97
     */
98
    private function setupViewEngine(Container $c)
99
    {
100
        $package = new ViewPackage();
101
        $package->addToContainer($c);
102
    }
103
104
    /**
105
     * @param Container $c
106
     */
107
    private function setupModules(Container $c)
108
    {
109
        // set up the modules and vendor package modules
110
        $packages = $c->get('packages');
111
        $i18n = $c->get('i18n');
112
        /** @var Translator $translator */
113
        $translator = $c->get(Translator::class);
114
115
        foreach ($packages as $packageName) {
116
            if (class_exists($packageName)) {
117
                /** @var RegistrationInterface $package */
118
                $package = new $packageName();
119
120
                if ($package->hasEntityPath()) {
121
                    $paths = $c['entity_paths'];
122
                    $paths[] = $package->getEntityPath();
123
                    $c['entity_paths'] = $paths;
124
                }
125
            }
126
        }
127
        reset($packages);
128
        foreach ($packages as $packageName) {
129
            if (class_exists($packageName)) {
130
                /** @var RegistrationInterface $package */
131
                $package = new $packageName();
132
                $package->addToContainer($c);
133
134
                if ($package instanceof RouterConfigInterface) {
0 ignored issues
show
Bug introduced by
The class Bone\Router\RouterConfigInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
135
                    $package->addRoutes($c, $this->router);
136
                }
137
138
                if ($package instanceof I18nRegistrationInterface) {
139
                    foreach ($i18n['supported_locales'] as $locale) {
140
                        $factory = new TranslatorFactory();
141
                        $factory->addPackageTranslations($translator, $package, $locale);
142
                    }
143
                }
144
145
                if ($package instanceof MiddlewareAwareInterface) {
146
                    $stack = $c->get(Stack::class);
147
                    $package->addMiddleware($stack);
148
                }
149
            }
150
        }
151
    }
152
153
    /**
154
     * @param Container $c
155
     * @throws \Bone\Exception
156
     */
157
    private function setupTranslator(Container $c)
158
    {
159
        $package = new I18nPackage();
160
        $package->addToContainer($c);
161
        $package->addMiddleware($c->get(Stack::class), $c);
162
    }
163
164
165
    /**
166
     * @param Container $c
167
     * @throws \Bone\Exception
168
     */
169
    private function setupPdoConnection(Container $c)
170
    {
171
        $package = new DbPackage();
172
        $package->addToContainer($c);
173
    }
174
175
    /**
176
     * @param Container $c
177
     */
178
    private function setupDownloadController(Container $c): void
179
    {
180
        $uploadDirectory = $c->get('uploads_dir');
181
        $c[DownloadController::class] = new DownloadController($uploadDirectory);
182
        $strategy = new JsonStrategy(new ResponseFactory());
183
        $strategy->setContainer($c);
184
        $this->router->map('GET', '/download', [DownloadController::class, 'downloadAction'])->setStrategy($strategy);
185
    }
186
187
    /**
188
     * @param Container $c
189
     */
190
    private function setupRouteFirewall(Container $c): void
191
    {
192
        $pckage = new FirewallPackage();
193
        $pckage->addToContainer($c);
194
    }
195
196
    /**
197
     * @param Container $c
198
     * @throws \Exception
199
     */
200
    private function  setupLogs(Container $c)
201
    {
202
        $package = new LogPackage();
203
        $package->addToContainer($c);
204
    }
205
206
    /**
207
     * @param Container $c
208
     */
209
    private function setupModuleViewOverrides(Container $c): void
210
    {
211
        /** @var PlatesEngine $viewEngine */
212
        $viewEngine = $c->get(PlatesEngine::class);
213
        $views = $c->get('views');
214
        $registeredViews = $viewEngine->getFolders();
215
216
        foreach ($views as $view => $folder) {
217
            $this->overrideViewFolder($view, $folder, $registeredViews);
218
        }
219
    }
220
221
    /**
222
     * @param string $view
223
     * @param string $folder
224
     * @param array $registeredViews
225
     * @param PlatesEngine $viewEngine
0 ignored issues
show
Bug introduced by
There is no parameter named $viewEngine. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
226
     */
227
    private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void
228
    {
229
        if ($registeredViews->exists($view)) {
230
            /** @var \League\Plates\Template\Folder $currentFolder */
231
            $currentFolder = $registeredViews->get($view);
232
            $currentFolder->setPath($folder);
233
        }
234
    }
235
236
    /**
237
     * @return string
238
     */
239
    public function getEntityPath(): string
240
    {
241
        return '';
242
    }
243
244
    /**
245
     * @return bool
246
     */
247
    public function hasEntityPath(): bool
248
    {
249
        return false;
250
    }
251
252
    /**
253
     * @param Container $c
254
     */
255
    private function initMiddlewareStack(Container $c): void
256
    {
257
        $router = $c->get(Router::class);
258
        $c[Stack::class] = new Stack($router);
259
    }
260
261
    /**
262
     * @param Container $c
263
     */
264
    private function setupMiddlewareStack(Container $c): void
265
    {
266
        $stack = $c->get(Stack::class);
267
        $middlewareStack = $c->has('stack') ? $c->get('stack') : [];
268
269
        foreach ($middlewareStack as $middleware) {
270
            if ($middleware instanceof MiddlewareInterface) {
271
                $stack->addMiddleWare($middleware);
272
            } elseif ($c->has($middleware)) {
273
                $stack->addMiddleWare($c->get($middleware));
274
            } else {
275
                $stack->addMiddleWare(new $middleware());
276
            }
277
        }
278
    }
279
}
280