Completed
Push — master ( 22721b...81b7a1 )
by Derek Stephen
04:02
created

ApplicationPackage::setupRouteFirewall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Mvc\Controller\DownloadController;
18
use Bone\Mvc\Router;
19
use Bone\Mvc\Router\Decorator\ExceptionDecorator;
20
use Bone\Mvc\Router\Decorator\NotAllowedDecorator;
21
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
22
use Bone\Mvc\Router\PlatesStrategy;
23
use Bone\Mvc\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 9
    public function __construct(array $config, Router $router)
60
    {
61 9
        $this->config = $config;
62 9
        $this->router = $router;
63 9
    }
64
65
    /**
66
     * @param Container $c
67
     */
68 9
    public function addToContainer(Container $c)
69
    {
70 9
        $this->setConfigArray($c);
71 9
        $this->setupLogs($c);
72 9
        $this->setupPdoConnection($c);
73 9
        $this->setupViewEngine($c);
74 9
        $this->initMiddlewareStack($c);
75 9
        $this->setupTranslator($c);
76 9
        $this->setupModules($c);
77 9
        $this->setupModuleViewOverrides($c);
78 9
        $this->setupDownloadController($c);
79 9
        $this->setupRouteFirewall($c);
80 9
        $this->setupMiddlewareStack($c);
81 9
    }
82
83
    /**
84
     * @param Container $c
85
     */
86 9
    private function setConfigArray(Container $c)
87
    {
88 9
        foreach ($this->config as $key => $value) {
89 9
            $c[$key] = $value;
90
        }
91 9
    }
92
93
    /**
94
     * @param Container $c
95
     */
96 9
    private function setupViewEngine(Container $c)
97
    {
98 9
        $package = new ViewPackage();
99 9
        $package->addToContainer($c);
100 9
    }
101
102
    /**
103
     * @param Container $c
104
     */
105 9
    private function setupModules(Container $c)
106
    {
107
        // set up the modules and vendor package modules
108 9
        $packages = $c->get('packages');
109 9
        $i18n = $c->get('i18n');
110
        /** @var Translator $translator */
111 9
        $translator = $c->get(Translator::class);
112
113 9
        foreach ($packages as $packageName) {
114 9
            if (class_exists($packageName)) {
115
                /** @var RegistrationInterface $package */
116 9
                $package = new $packageName();
117
118 9
                if ($package->hasEntityPath()) {
119 9
                    $paths = $c['entity_paths'];
120 9
                    $paths[] = $package->getEntityPath();
121 9
                    $c['entity_paths'] = $paths;
122
                }
123
            }
124
        }
125 9
        reset($packages);
126 9
        foreach ($packages as $packageName) {
127 9
            if (class_exists($packageName)) {
128
                /** @var RegistrationInterface $package */
129 9
                $package = new $packageName();
130 9
                $package->addToContainer($c);
131
132 9
                if ($package instanceof RouterConfigInterface) {
133 9
                    $package->addRoutes($c, $this->router);
134
                }
135
136 9
                if ($package instanceof I18nRegistrationInterface) {
137 9
                    foreach ($i18n['supported_locales'] as $locale) {
138 9
                        $factory = new TranslatorFactory();
139 9
                        $factory->addPackageTranslations($translator, $package, $locale);
140
                    }
141
                }
142
143 9
                if ($package instanceof MiddlewareAwareInterface) {
144
                    $stack = $c->get(Stack::class);
145
                    $package->addMiddleware($stack);
146
                }
147
            }
148
        }
149 9
    }
150
151
    /**
152
     * @param Container $c
153
     */
154 9
    private function setupTranslator(Container $c)
155
    {
156 9
        $package = new I18nPackage();
157 9
        $package->addToContainer($c);
158 9
        $package->addMiddleware($c->get(Stack::class), $c);
159 9
    }
160
161
162
    /**
163
     * @param Container $c
164
     */
165 9
    private function setupPdoConnection(Container $c)
166
    {
167 9
        $package = new DbPackage();
168 9
        $package->addToContainer($c);
169 9
    }
170
171
    /**
172
     * @param Container $c
173
     */
174 9
    private function setupDownloadController(Container $c): void
175
    {
176 9
        $uploadDirectory = $c->get('uploads_dir');
177 9
        $c[DownloadController::class] = new DownloadController($uploadDirectory);
178 9
        $strategy = new JsonStrategy(new ResponseFactory());
179 9
        $strategy->setContainer($c);
180 9
        $this->router->map('GET', '/download', [DownloadController::class, 'downloadAction'])->setStrategy($strategy);
181 9
    }
182
183
    /**
184
     * @param Container $c
185
     */
186 9
    private function setupRouteFirewall(Container $c): void
187
    {
188 9
        $pckage = new FirewallPackage();
189 9
        $pckage->addToContainer($c);
190 9
    }
191
192
    /**
193
     * @param Container $c
194
     */
195 9
    private function  setupLogs(Container $c)
196
    {
197 9
        $package = new LogPackage();
198 9
        $package->addToContainer($c);
199 9
    }
200
201
    /**
202
     * @param Container $c
203
     */
204 9
    private function setupModuleViewOverrides(Container $c): void
205
    {
206
        /** @var PlatesEngine $viewEngine */
207 9
        $viewEngine = $c->get(PlatesEngine::class);
208 9
        $views = $c->get('views');
209 9
        $registeredViews = $viewEngine->getFolders();
210
211 9
        foreach ($views as $view => $folder) {
212 9
            $this->overrideViewFolder($view, $folder, $registeredViews);
213
        }
214 9
    }
215
216
    /**
217
     * @param string $view
218
     * @param string $folder
219
     * @param array $registeredViews
220
     * @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...
221
     */
222 9
    private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void
223
    {
224 9
        if ($registeredViews->exists($view)) {
225
            /** @var \League\Plates\Template\Folder $currentFolder */
226 1
            $currentFolder = $registeredViews->get($view);
227 1
            $currentFolder->setPath($folder);
228
        }
229 9
    }
230
231
    /**
232
     * @return string
233
     */
234 1
    public function getEntityPath(): string
235
    {
236 1
        return '';
237
    }
238
239
    /**
240
     * @return bool
241
     */
242 1
    public function hasEntityPath(): bool
243
    {
244 1
        return false;
245
    }
246
247
    /**
248
     * @param Container $c
249
     */
250 9
    private function initMiddlewareStack(Container $c): void
251
    {
252 9
        $router = $c->get(Router::class);
253 9
        $c[Stack::class] = new Stack($router);
254 9
    }
255
256
    /**
257
     * @param Container $c
258
     */
259 9
    private function setupMiddlewareStack(Container $c): void
260
    {
261 9
        $stack = $c->get(Stack::class);
262 9
        $middlewareStack = $c->has('stack') ? $c->get('stack') : [];
263
264 9
        foreach ($middlewareStack as $middleware) {
265
            if ($middleware instanceof MiddlewareInterface) {
266
                $stack->addMiddleWare($middleware);
267
            } elseif ($c->has($middleware)) {
268
                $stack->addMiddleWare($c->get($middleware));
269
            } else {
270
                $stack->addMiddleWare(new $middleware());
271
            }
272
        }
273 9
    }
274
}
275