Completed
Push — dev-master ( 24ccf8...ee40ad )
by Derek Stephen
01:01
created

ApplicationPackage::setupViewEngine()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 60

Duplication

Lines 27
Ratio 45 %

Code Coverage

Tests 40
CRAP Score 2

Importance

Changes 0
Metric Value
dl 27
loc 60
ccs 40
cts 40
cp 1
rs 8.8727
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ExceptionDecorator;
9
use Bone\Mvc\Router\Decorator\NotAllowedDecorator;
10
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
11
use Bone\Mvc\Router\PlatesStrategy;
12
use Bone\Mvc\Router\RouterConfigInterface;
13
use Bone\Mvc\View\Extension\Plates\AlertBox;
14
use Bone\Mvc\View\Extension\Plates\LocaleLink;
15
use Bone\Mvc\View\Extension\Plates\Translate;
16
use Bone\View\Helper\Paginator;
17
use Bone\Mvc\View\PlatesEngine;
18
use Bone\Service\TranslatorFactory;
19
use League\Route\Router;
20
use Locale;
21
use PDO;
22
use Zend\I18n\Translator\Loader\Gettext;
23
use Zend\I18n\Translator\Translator;
24
25
class ApplicationPackage implements RegistrationInterface
26
{
27
    /** @var array $config */
28
    private $config;
29
30
    /** @var Router $router */
31
    private $router;
32
33
   /** @var bool $i18nEnabledSite */
34
    private $i18nEnabledSite = false;
35
36
    /** @var array $supportedLocales */
37
    private $supportedLocales = [];
38
39
    /**
40
     * ApplicationPackage constructor.
41
     * @param array $config
42
     * @param \League\Route\Router $router
43
     */
44 9
    public function __construct(array $config, Router $router)
45
    {
46 9
        $this->config = $config;
47 9
        $this->router = $router;
48 9
    }
49
50
    /**
51
     * @param Container $c
52
     */
53 9
    public function addToContainer(Container $c)
54
    {
55 9
        $this->setConfigArray($c);
56 9
        $this->setLocale($c);
57 9
        $this->setupPdoConnection($c);
58 9
        $this->setupViewEngine($c);
59 9
        $this->setupTranslator($c);
60 9
        $this->setupModules($c);
61 9
    }
62
63
    /**
64
     * @param Container $c
65
     */
66 9
    private function setConfigArray(Container $c)
67
    {
68 9
        foreach($this->config as $key => $value)
69
        {
70 9
            $c[$key] = $value;
71
        }
72 9
    }
73
74
    /**
75
     * @param Container $c
76
     */
77 9
    private function setLocale(Container $c)
78
    {
79 9
        $i18n = $c->get('i18n');
80 9
        $this->i18nEnabledSite = $i18n['enabled'];
81 9
        $this->supportedLocales = $i18n['supported_locales'];
82 9
        $defaultLocale = $i18n['default_locale'];
83 9
        Locale::setDefault($defaultLocale);
84 9
    }
85
86
    /**
87
     * @param Container $c
88
     */
89 9
    private function setupViewEngine(Container $c)
90
    {
91
        // set up the view engine dependencies
92 9
        $viewEngine = new PlatesEngine($c->get('viewFolder'));
93 9
        $viewEngine->loadExtension(new AlertBox());
94
95 9
        $c[PlatesEngine::class] = $viewEngine;
96
97 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...
98 9
            $layout = $c->get('default_layout');
99 9
            $templates = $c->get('error_pages');
100 9
            $viewEngine = $c->get(PlatesEngine::class);
101 9
            $notFoundDecorator = new NotFoundDecorator($viewEngine, $templates);
102 9
            $notFoundDecorator->setLayout($layout);
103
104 9
            return $notFoundDecorator;
105 9
        });
106
107 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...
108 9
            $layout = $c->get('default_layout');
109 9
            $templates = $c->get('error_pages');
110 9
            $viewEngine = $c->get(PlatesEngine::class);
111 9
            $notAllowedDecorator = new NotAllowedDecorator($viewEngine, $templates);
112 9
            $notAllowedDecorator->setLayout($layout);
113
114 9
            return $notAllowedDecorator;
115 9
        });
116
117 View Code Duplication
        $c[ExceptionDecorator::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...
118 9
            $viewEngine = $c->get(PlatesEngine::class);
119 9
            $layout = $c->get('default_layout');
120 9
            $templates = $c->get('error_pages');
121 9
            $decorator = new ExceptionDecorator($viewEngine, $templates);
122 9
            $decorator->setLayout($layout);
123
124 9
            return $decorator;
125 9
        });
126
127
        $c[PlatesStrategy::class] = $c->factory(function (Container $c) {
128 9
            $viewEngine = $c->get(PlatesEngine::class);
129 9
            $notFoundDecorator = $c->get(NotFoundDecorator::class);
130 9
            $notAllowedDecorator = $c->get(NotAllowedDecorator::class);
131 9
            $exceptionDecorator = $c->get(ExceptionDecorator::class);
132 9
            $layout = $c->get('default_layout');
133 9
            $strategy = new PlatesStrategy($viewEngine, $notFoundDecorator, $notAllowedDecorator, $layout, $exceptionDecorator);
134
135 9
            return $strategy;
136 9
        });
137
138
        /** @var PlatesStrategy $strategy */
139 9
        $strategy = $c->get(PlatesStrategy::class);
140 9
        $strategy->setContainer($c);
141
142 9
        if ($this->i18nEnabledSite === true) {
143 8
            $strategy->setI18nEnabled(true);
144 8
            $strategy->setSupportedLocales($this->supportedLocales);
145
        }
146
147 9
        $this->router->setStrategy($strategy);
148 9
    }
149
150
    /**
151
     * @param Container $c
152
     */
153 9
    private function setupModules(Container $c)
154
    {
155
        // set up the modules and vendor package modules
156 9
        $packages = $c->get('packages');
157 9
        $i18n = $c->get('i18n');
158
        /** @var Translator $translator */
159 9
        $translator = $c->get(Translator::class);
160
161 9
        foreach ($packages as $packageName) {
162 9
            if (class_exists($packageName)) {
163
                /** @var RegistrationInterface $package */
164 9
                $package = new $packageName();
165
166 9
                if ($package->hasEntityPath()) {
167 9
                    $paths = $c['entity_paths'];
168 9
                    $paths[] = $package->getEntityPath();
169 9
                    $c['entity_paths'] = $paths;
170
                }
171
            }
172
        }
173 9
        reset($packages);
174 9
        foreach ($packages as $packageName) {
175 9
            if (class_exists($packageName)) {
176
                /** @var RegistrationInterface $package */
177 9
                $package = new $packageName();
178 9
                $package->addToContainer($c);
179
180 9
                if ($package instanceof RouterConfigInterface) {
181 9
                    $package->addRoutes($c, $this->router);
182
                }
183
184 9
                if ($package instanceof I18nRegistrationInterface) {
185
                    foreach ($i18n['supported_locales'] as $locale) {
186
                        $factory = new TranslatorFactory();
187
                        $factory->addPackageTranslations($translator, $package, $locale);
188
                    }
189
                }
190
            }
191
        }
192 9
    }
193
194
    /**
195
     * @param Container $c
196
     */
197 9
    private function setupTranslator(Container $c)
198
    {
199 9
        $config = $c->get('i18n');
200 9
        $engine = $c->get(PlatesEngine::class);
201 9
        if (is_array($config)) {
202 9
            $factory = new TranslatorFactory();
203 9
            $translator = $factory->createTranslator($config);
204 9
            $engine->loadExtension(new Translate($translator));
205 9
            $engine->loadExtension(new LocaleLink());
206 9
            $defaultLocale = $config['default_locale'] ?: 'en_GB';
207
//            if (!in_array($locale, $config['supported_locales'])) {
208
//                $locale = $defaultLocale;
209
//            }
210 9
            $translator->setLocale($defaultLocale);
211 9
            $c[Translator::class] = $translator;
212
        }
213 9
    }
214
215
216
    /**
217
     * @param Container $c
218
     */
219 9
    private function setupPdoConnection(Container $c)
220
    {
221
        // set up a db connection
222
        $c[PDO::class] = $c->factory(function(Container $c): PDO {
223 1
            $credentials = $c->get('db');
224 1
            $host =$credentials['host'];
225 1
            $db = $credentials['database'];
226 1
            $user = $credentials['user'];
227 1
            $pass = $credentials['pass'];
228
229 1
            $dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [
230 1
                PDO::ATTR_EMULATE_PREPARES => false,
231
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
232
            ]);
233
234 1
            return $dbConnection;
235 9
        });
236 9
    }
237
238
    /**
239
     * @return string
240
     */
241 1
    function getEntityPath(): string
242
    {
243 1
        return '';
244
    }
245
246
    /**
247
     * @return bool
248
     */
249 1
    function hasEntityPath(): bool
250
    {
251 1
        return false;
252
    }
253
}