Completed
Push — master ( 4e5156...45ace6 )
by Derek Stephen
01:47
created

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