Completed
Push — master ( 81b7a1...80c875 )
by Derek Stephen
01:34
created

ApplicationPackage::setupTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
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\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
        $this->setupTranslator($c);
76
        $this->setupModules($c);
77
        $this->setupModuleViewOverrides($c);
78
        $this->setupDownloadController($c);
79
        $this->setupRouteFirewall($c);
80
        $this->setupMiddlewareStack($c);
81
    }
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
    private function setupModules(Container $c)
106
    {
107
        // set up the modules and vendor package modules
108
        $packages = $c->get('packages');
109
        $i18n = $c->get('i18n');
110
        /** @var Translator $translator */
111
        $translator = $c->get(Translator::class);
112
113
        foreach ($packages as $packageName) {
114
            if (class_exists($packageName)) {
115
                /** @var RegistrationInterface $package */
116
                $package = new $packageName();
117
118
                if ($package->hasEntityPath()) {
119
                    $paths = $c['entity_paths'];
120
                    $paths[] = $package->getEntityPath();
121
                    $c['entity_paths'] = $paths;
122
                }
123
            }
124
        }
125
        reset($packages);
126
        foreach ($packages as $packageName) {
127
            if (class_exists($packageName)) {
128
                /** @var RegistrationInterface $package */
129
                $package = new $packageName();
130
                $package->addToContainer($c);
131
132
                if ($package instanceof RouterConfigInterface) {
133
                    $package->addRoutes($c, $this->router);
134
                }
135
136
                if ($package instanceof I18nRegistrationInterface) {
137
                    foreach ($i18n['supported_locales'] as $locale) {
138
                        $factory = new TranslatorFactory();
139
                        $factory->addPackageTranslations($translator, $package, $locale);
140
                    }
141
                }
142
143
                if ($package instanceof MiddlewareAwareInterface) {
0 ignored issues
show
Bug introduced by
The class Bone\Http\MiddlewareAwareInterface 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...
144
                    $stack = $c->get(Stack::class);
145
                    $package->addMiddleware($stack);
146
                }
147
            }
148
        }
149
    }
150
151
    /**
152
     * @param Container $c
153
     */
154
    private function setupTranslator(Container $c)
155
    {
156
        $package = new I18nPackage();
157
        $package->addToContainer($c);
158
        $package->addMiddleware($c->get(Stack::class), $c);
159
    }
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
    private function setupDownloadController(Container $c): void
175
    {
176
        $uploadDirectory = $c->get('uploads_dir');
177
        $c[DownloadController::class] = new DownloadController($uploadDirectory);
178
        $strategy = new JsonStrategy(new ResponseFactory());
179
        $strategy->setContainer($c);
180
        $this->router->map('GET', '/download', [DownloadController::class, 'downloadAction'])->setStrategy($strategy);
181
    }
182
183
    /**
184
     * @param Container $c
185
     */
186
    private function setupRouteFirewall(Container $c): void
187
    {
188
        $pckage = new FirewallPackage();
189
        $pckage->addToContainer($c);
190
    }
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
    private function setupModuleViewOverrides(Container $c): void
205
    {
206
        /** @var PlatesEngine $viewEngine */
207
        $viewEngine = $c->get(PlatesEngine::class);
208
        $views = $c->get('views');
209
        $registeredViews = $viewEngine->getFolders();
210
211
        foreach ($views as $view => $folder) {
212
            $this->overrideViewFolder($view, $folder, $registeredViews);
213
        }
214
    }
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
    private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void
223
    {
224
        if ($registeredViews->exists($view)) {
225
            /** @var \League\Plates\Template\Folder $currentFolder */
226
            $currentFolder = $registeredViews->get($view);
227
            $currentFolder->setPath($folder);
228
        }
229
    }
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
    }
255
256
    /**
257
     * @param Container $c
258
     */
259
    private function setupMiddlewareStack(Container $c): void
260
    {
261
        $stack = $c->get(Stack::class);
262
        $middlewareStack = $c->has('stack') ? $c->get('stack') : [];
263
264
        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
    }
274
}
275