Completed
Push — master ( be43bf...cf9db0 )
by Derek Stephen
01:35
created

ApplicationPackage   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 86.44%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 16
dl 0
loc 258
ccs 102
cts 118
cp 0.8644
rs 9.6
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
C setupPackages() 0 69 13
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 setupVendorViewOverrides() 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;
4
5
use Barnacle\Container;
6
use Barnacle\RegistrationInterface;
7
use Bone\Console\CommandRegistrationInterface;
8
use Bone\Console\ConsoleApplication;
9
use Bone\Db\DbPackage;
10
use Bone\Firewall\FirewallPackage;
11
use Bone\Http\Middleware\Stack;
12
use Bone\Http\MiddlewareAwareInterface;
13
use Bone\I18n\I18nPackage;
14
use Bone\I18n\I18nRegistrationInterface;
15
use Bone\Log\LogPackage;
16
use Bone\Controller\DownloadController;
17
use Bone\Router\Router;
18
use Bone\Router\RouterConfigInterface;
19
use Bone\View\ViewEngine;
20
use Bone\I18n\Service\TranslatorFactory;
21
use Bone\View\ViewPackage;
22
use League\Plates\Template\Folders;
23
use League\Route\Strategy\JsonStrategy;
24
use Laminas\Diactoros\ResponseFactory;
25
use Laminas\I18n\Translator\Translator;
26
use Psr\Http\Server\MiddlewareInterface;
27
28
class ApplicationPackage implements RegistrationInterface
29
{
30
    /** @var array $config */
31
    private $config;
32
33
    /** @var Router $router */
34
    private $router;
35
36
    /**
37
     * ApplicationPackage constructor.
38
     * @param array $config
39
     * @param Router $router
40
     */
41 9
    public function __construct(array $config, Router $router)
42
    {
43 9
        $this->config = $config;
44 9
        $this->router = $router;
45 9
    }
46
47
    /**
48
     * @param Container $c
49
     * @throws \Bone\Exception
50
     * @throws \Exception
51
     */
52 9
    public function addToContainer(Container $c)
53
    {
54 9
        $this->setConfigArray($c);
55 9
        $this->setupLogs($c);
56 9
        $this->setupPdoConnection($c);
57 9
        $this->setupViewEngine($c);
58 9
        $this->initMiddlewareStack($c);
59 9
        $this->setupTranslator($c);
60 9
        $this->setupPackages($c);
61 9
        $this->setupVendorViewOverrides($c);
62 9
        $this->setupDownloadController($c);
63 9
        $this->setupRouteFirewall($c);
64 9
        $this->setupMiddlewareStack($c);
65 9
    }
66
67
    /**
68
     * @param Container $c
69
     */
70 9
    private function setConfigArray(Container $c)
71
    {
72 9
        foreach ($this->config as $key => $value) {
73 9
            $c[$key] = $value;
74
        }
75 9
    }
76
77
    /**
78
     * @param Container $c
79
     */
80 9
    private function setupViewEngine(Container $c)
81
    {
82 9
        $package = new ViewPackage();
83 9
        $package->addToContainer($c);
84 9
    }
85
86
    /**
87
     * @param Container $c
88
     */
89 9
    private function setupPackages(Container $c)
90
    {
91
        // set up the modules and vendor package modules
92 9
        $packages = $c->get('packages');
93 9
        $i18n = $c->get('i18n');
94
        /** @var Translator $translator */
95 9
        $translator = $c->get(Translator::class);
96
97 9
        foreach ($packages as $packageName) {
98 9
            if (class_exists($packageName)) {
99
                /** @var RegistrationInterface $package */
100 9
                $package = new $packageName();
101
102 9
                if ($package->hasEntityPath()) {
103 9
                    $paths = $c['entity_paths'];
104 9
                    $paths[] = $package->getEntityPath();
105 9
                    $c['entity_paths'] = $paths;
106
                }
107
            }
108
        }
109
110 9
        reset($packages);
111 9
        $consoleCommands = [];
112
113 9
        foreach ($packages as $packageName) {
114 9
            if (class_exists($packageName)) {
115
                /** @var RegistrationInterface $package */
116 9
                $package = new $packageName();
117 9
                $package->addToContainer($c);
118
119 9
                if ($package instanceof RouterConfigInterface) {
120 9
                    $package->addRoutes($c, $this->router);
121
                }
122
123 9
                if ($package instanceof I18nRegistrationInterface) {
124 9
                    foreach ($i18n['supported_locales'] as $locale) {
125 9
                        $factory = new TranslatorFactory();
126 9
                        $factory->addPackageTranslations($translator, $package, $locale);
127
                    }
128
                }
129
130 9
                if ($package instanceof MiddlewareAwareInterface) {
131
                    $stack = $c->get(Stack::class);
132
                    $package->addMiddleware($stack, $c);
133
                }
134
135 9
                if ($package instanceof CommandRegistrationInterface) {
136
                    $stack = $c->get(Stack::class);
0 ignored issues
show
Unused Code introduced by
$stack is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137
                    $commands = $package->registerConsoleCommands($c);
138
139
                    foreach ($commands as $command) {
140
                        $consoleCommands[] = $command;
141
                    }
142
                }
143
            }
144
        }
145
146 9
        $c['consoleCommands'] = $consoleCommands;
147
148
        $c[ConsoleApplication::class] = $c->factory(function(Container $c) {
149
            $app = new ConsoleApplication();
150
            $consoleCommands = $c->get('consoleCommands');
151
            foreach($consoleCommands as $command) {
152
                $app->addCommands($consoleCommands);
153
            }
154
155
            return $app;
156 9
        });
157 9
    }
158
159
    /**
160
     * @param Container $c
161
     * @throws \Bone\Exception
162
     */
163 9
    private function setupTranslator(Container $c)
164
    {
165 9
        $package = new I18nPackage();
166 9
        $package->addToContainer($c);
167 9
        $package->addMiddleware($c->get(Stack::class), $c);
168 9
    }
169
170
171
    /**
172
     * @param Container $c
173
     * @throws \Bone\Exception
174
     */
175 9
    private function setupPdoConnection(Container $c)
176
    {
177 9
        $package = new DbPackage();
178 9
        $package->addToContainer($c);
179 9
    }
180
181
    /**
182
     * @param Container $c
183
     */
184 9
    private function setupDownloadController(Container $c): void
185
    {
186 9
        $uploadDirectory = $c->get('uploads_dir');
187 9
        $c[DownloadController::class] = new DownloadController($uploadDirectory);
188 9
        $strategy = new JsonStrategy(new ResponseFactory());
189 9
        $strategy->setContainer($c);
190 9
        $this->router->map('GET', '/download', [DownloadController::class, 'downloadAction'])->setStrategy($strategy);
191 9
    }
192
193
    /**
194
     * @param Container $c
195
     */
196 9
    private function setupRouteFirewall(Container $c): void
197
    {
198 9
        $pckage = new FirewallPackage();
199 9
        $pckage->addToContainer($c);
200 9
    }
201
202
    /**
203
     * @param Container $c
204
     * @throws \Exception
205
     */
206 9
    private function  setupLogs(Container $c)
207
    {
208 9
        $package = new LogPackage();
209 9
        $package->addToContainer($c);
210 9
    }
211
212
    /**
213
     * @param Container $c
214
     */
215 9
    private function setupVendorViewOverrides(Container $c): void
216
    {
217
        /** @var ViewEngine $viewEngine */
218 9
        $viewEngine = $c->get(ViewEngine::class);
219 9
        $views = $c->get('views');
220 9
        $registeredViews = $viewEngine->getFolders();
221
222 9
        foreach ($views as $view => $folder) {
223 9
            $this->overrideViewFolder($view, $folder, $registeredViews);
224
        }
225 9
    }
226
227
    /**
228
     * @param string $view
229
     * @param string $folder
230
     * @param array $registeredViews
231
     * @param ViewEngine $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...
232
     */
233 9
    private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void
234
    {
235 9
        if ($registeredViews->exists($view)) {
236
            /** @var \League\Plates\Template\Folder $currentFolder */
237 1
            $currentFolder = $registeredViews->get($view);
238 1
            $currentFolder->setPath($folder);
239
        }
240 9
    }
241
242
    /**
243
     * @return string
244
     */
245 1
    public function getEntityPath(): string
246
    {
247 1
        return '';
248
    }
249
250
    /**
251
     * @return bool
252
     */
253 1
    public function hasEntityPath(): bool
254
    {
255 1
        return false;
256
    }
257
258
    /**
259
     * @param Container $c
260
     */
261 9
    private function initMiddlewareStack(Container $c): void
262
    {
263 9
        $router = $c->get(Router::class);
264 9
        $c[Stack::class] = new Stack($router);
265 9
    }
266
267
    /**
268
     * @param Container $c
269
     */
270 9
    private function setupMiddlewareStack(Container $c): void
271
    {
272 9
        $stack = $c->get(Stack::class);
273 9
        $middlewareStack = $c->has('stack') ? $c->get('stack') : [];
274
275 9
        foreach ($middlewareStack as $middleware) {
276
            if ($middleware instanceof MiddlewareInterface) {
277
                $stack->addMiddleWare($middleware);
278
            } elseif ($c->has($middleware)) {
279
                $stack->addMiddleWare($c->get($middleware));
280
            } else {
281
                $stack->addMiddleWare(new $middleware());
282
            }
283
        }
284 9
    }
285
}
286