Completed
Push — master ( a7be56...b163e4 )
by Derek Stephen
01:08
created

ApplicationPackage::setupDownloadController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\Router\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\Router;ConfigInterface;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Bone\Router\Router as Router because the name is already in use
Loading history...
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) {
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
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