Completed
Push — dev-master ( edea8b...32aa4f )
by Derek Stephen
01:51 queued 11s
created

ApplicationPackage::setupModules()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9.0533

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
ccs 21
cts 23
cp 0.913
rs 7.7404
cc 9
nc 32
nop 1
crap 9.0533
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Barnacle\Container;
6
use Barnacle\RegistrationInterface;
7
use Bone\I18n\I18nRegistrationInterface;
8
use Bone\Mvc\Router\Decorator\NotAllowedDecorator;
9
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
10
use Bone\Mvc\Router\PlatesStrategy;
11
use Bone\Mvc\Router\RouterConfigInterface;
12
use Bone\Mvc\View\Extension\Plates\Translate;
13
use Bone\View\Helper\Paginator;
14
use Bone\Mvc\View\PlatesEngine;
15
use Bone\Service\TranslatorFactory;
16
use League\Route\Router;
17
use Locale;
18
use PDO;
19
use Zend\I18n\Translator\Loader\Gettext;
20
use Zend\I18n\Translator\Translator;
21
22
class ApplicationPackage implements RegistrationInterface
23
{
24
    /** @var array $config */
25
    private $config;
26
27
    /** @var Router $router */
28
    private $router;
29
30
   /** @var bool $i18nEnabledSite */
31
    private $i18nEnabledSite = false;
32
33
    /** @var array $supportedLocales */
34
    private $supportedLocales = [];
35
36
    /**
37
     * ApplicationPackage constructor.
38
     * @param array $config
39
     * @param \League\Route\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
     */
50 9
    public function addToContainer(Container $c)
51
    {
52 9
        $this->setConfigArray($c);
53 9
        $this->setLocale($c);
54 9
        $this->setupPdoConnection($c);
55 9
        $this->setupViewEngine($c);
56 9
        $this->setupTranslator($c);
57 9
        $this->setupModules($c);
58 9
    }
59
60
    /**
61
     * @param Container $c
62
     */
63 9
    private function setConfigArray(Container $c)
64
    {
65 9
        foreach($this->config as $key => $value)
66
        {
67 9
            $c[$key] = $value;
68
        }
69 9
    }
70
71
    /**
72
     * @param Container $c
73
     */
74 9
    private function setLocale(Container $c)
75
    {
76 9
        $i18n = $c->get('i18n');
77 9
        $this->i18nEnabledSite = $i18n['enabled'];
78 9
        $this->supportedLocales = $i18n['supported_locales'];
79 9
        $defaultLocale = $i18n['default_locale'];
80 9
        Locale::setDefault($defaultLocale);
81 9
    }
82
83
    /**
84
     * @param Container $c
85
     */
86 9
    private function setupViewEngine(Container $c)
87
    {
88
        // set up the view engine dependencies
89 9
        $viewEngine = new PlatesEngine($c->get('viewFolder'));
90
//        $i18nExtension = new Translate();
91
//        $viewEngine->loadExtension();
92
93 9
        $c[PlatesEngine::class] = $viewEngine;
94
95 View Code Duplication
        $c[NotFoundDecorator::class] = $c->factory(function (Container $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96 9
            $layout = $c->get('default_layout');
97 9
            $viewEngine = $c->get(PlatesEngine::class);
98 9
            $notFoundDecorator = new NotFoundDecorator($viewEngine);
99 9
            $notFoundDecorator->setLayout($layout);
100
101 9
            return $notFoundDecorator;
102 9
        });
103
104 View Code Duplication
        $c[NotAllowedDecorator::class] = $c->factory(function (Container $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105 9
            $layout = $c->get('default_layout');
106 9
            $viewEngine = $c->get(PlatesEngine::class);
107 9
            $notAllowedDecorator = new NotAllowedDecorator($viewEngine);
108 9
            $notAllowedDecorator->setLayout($layout);
109
110 9
            return $notAllowedDecorator;
111 9
        });
112
113
        $c[PlatesStrategy::class] = $c->factory(function (Container $c) {
114 9
            $viewEngine = $c->get(PlatesEngine::class);
115 9
            $notFoundDecorator = $c->get(NotFoundDecorator::class);
116 9
            $notAllowedDecorator = $c->get(NotAllowedDecorator::class);
117 9
            $layout = $c->get('default_layout');
118 9
            $strategy = new PlatesStrategy($viewEngine, $notFoundDecorator, $notAllowedDecorator, $layout);
119
120 9
            return $strategy;
121 9
        });
122
123
        /** @var PlatesStrategy $strategy */
124 9
        $strategy = $c->get(PlatesStrategy::class);
125 9
        $strategy->setContainer($c);
126
127 9
        if ($this->i18nEnabledSite === true) {
128 8
            $strategy->setI18nEnabled(true);
129 8
            $strategy->setSupportedLocales($this->supportedLocales);
130
        }
131
132 9
        $this->router->setStrategy($strategy);
133 9
    }
134
135
    /**
136
     * @param Container $c
137
     */
138 9
    private function setupModules(Container $c)
139
    {
140
        // set up the modules and vendor package modules
141 9
        $packages = $c->get('packages');
142 9
        $translator = $c->get(Translator::class);
143 9
        $config = $c->get('i18n');
144
145 9
        foreach ($packages as $packageName) {
146 9
            if (class_exists($packageName)) {
147
                /** @var RegistrationInterface $package */
148 9
                $package = new $packageName();
149
150 9
                if ($package->hasEntityPath()) {
151 9
                    $paths = $c['entity_paths'];
152 9
                    $paths[] = $package->getEntityPath();
153 9
                    $c['entity_paths'] = $paths;
154
                }
155
            }
156
        }
157 9
        reset($packages);
158 9
        foreach ($packages as $packageName) {
159 9
            if (class_exists($packageName)) {
160
                /** @var RegistrationInterface $package */
161 9
                $package = new $packageName();
162 9
                $package->addToContainer($c);
163
164 9
                if ($package instanceof RouterConfigInterface) {
165 9
                    $package->addRoutes($c, $this->router);
166
                }
167
168 9
                if ($package instanceof I18nRegistrationInterface) {
169
                    $dir = $package->getTranslationsDirectory();
170
                    foreach ($config['supported_locales'] as $locale) {
171 9
                        $translator->addTranslationFilePattern(Gettext::class, $dir, '%1$s/' . $locale . '.mo');
172
                    }
173
                }
174
            }
175
        }
176 9
    }
177
178
    /**
179
     * @param Container $c
180
     */
181 9
    private function setupTranslator(Container $c)
182
    {
183 9
        $config = $c->get('i18n');
184 9
        $engine = $c->get(PlatesEngine::class);
185 9
        if (is_array($config)) {
186 9
            $factory = new TranslatorFactory();
187 9
            $translator = $factory->createTranslator($config);
188 9
            $c['translator'] = $translator;
189 9
            $engine->loadExtension(new Translate($translator));
190 9
            $defaultLocale = $config['default_locale'] ?: 'en_GB';
191
//            if (!in_array($locale, $config['supported_locales'])) {
192
//                $locale = $defaultLocale;
193
//            }
194 9
            $translator->setLocale($defaultLocale);
195 9
            $c[Translator::class] = $translator;
196
        }
197 9
    }
198
199
200
    /**
201
     * @param Container $c
202
     */
203 9
    private function setupPdoConnection(Container $c)
204
    {
205
        // set up a db connection
206
        $c[PDO::class] = $c->factory(function(Container $c): PDO {
207 1
            $credentials = $c->get('db');
208 1
            $host =$credentials['host'];
209 1
            $db = $credentials['database'];
210 1
            $user = $credentials['user'];
211 1
            $pass = $credentials['pass'];
212
213 1
            $dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [
214 1
                PDO::ATTR_EMULATE_PREPARES => false,
215
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
216
            ]);
217
218 1
            return $dbConnection;
219 9
        });
220 9
    }
221
222
    /**
223
     * @return string
224
     */
225 1
    function getEntityPath(): string
226
    {
227 1
        return '';
228
    }
229
230
    /**
231
     * @return bool
232
     */
233 1
    function hasEntityPath(): bool
234
    {
235 1
        return false;
236
    }
237
}