Completed
Push — master ( 753e69...36642f )
by Derek Stephen
02:06 queued 13s
created

ApplicationPackage::setupViewEngine()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 60

Duplication

Lines 27
Ratio 45 %

Code Coverage

Tests 13
CRAP Score 3.2301

Importance

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

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 1
    public function addToContainer(Container $c)
54
    {
55 1
        $this->setConfigArray($c);
56 1
        $this->setLocale($c);
57 1
        $this->setupPdoConnection($c);
58 1
        $this->setupViewEngine($c);
59
        $this->setupTranslator($c);
60
        $this->setupModules($c);
61
    }
62
63
    /**
64
     * @param Container $c
65
     */
66 1
    private function setConfigArray(Container $c)
67
    {
68 1
        foreach($this->config as $key => $value)
69
        {
70 1
            $c[$key] = $value;
71
        }
72 1
    }
73
74
    /**
75
     * @param Container $c
76
     */
77 1
    private function setLocale(Container $c)
78
    {
79 1
        $i18n = $c->get('i18n');
80 1
        $this->i18nEnabledSite = $i18n['enabled'];
81 1
        $this->supportedLocales = $i18n['supported_locales'];
82 1
        $defaultLocale = $i18n['default_locale'];
83 1
        Locale::setDefault($defaultLocale);
84 1
    }
85
86
    /**
87
     * @param Container $c
88
     */
89 1
    private function setupViewEngine(Container $c)
90
    {
91
        // set up the view engine dependencies
92 1
        $viewEngine = new PlatesEngine($c->get('viewFolder'));
93 1
        $viewEngine->loadExtension(new AlertBox());
94
95 1
        $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 1
            $layout = $c->get('default_layout');
99 1
            $templates = $c->get('error_pages');
100
            $viewEngine = $c->get(PlatesEngine::class);
101
            $notFoundDecorator = new NotFoundDecorator($viewEngine, $templates);
102
            $notFoundDecorator->setLayout($layout);
103
104
            return $notFoundDecorator;
105 1
        });
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
            $layout = $c->get('default_layout');
109
            $templates = $c->get('error_pages');
110
            $viewEngine = $c->get(PlatesEngine::class);
111
            $notAllowedDecorator = new NotAllowedDecorator($viewEngine, $templates);
112
            $notAllowedDecorator->setLayout($layout);
113
114
            return $notAllowedDecorator;
115 1
        });
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
            $viewEngine = $c->get(PlatesEngine::class);
119
            $layout = $c->get('default_layout');
120
            $templates = $c->get('error_pages');
121
            $decorator = new ExceptionDecorator($viewEngine, $templates);
122
            $decorator->setLayout($layout);
123
124
            return $decorator;
125 1
        });
126
127
        $c[PlatesStrategy::class] = $c->factory(function (Container $c) {
128 1
            $viewEngine = $c->get(PlatesEngine::class);
129 1
            $notFoundDecorator = $c->get(NotFoundDecorator::class);
130
            $notAllowedDecorator = $c->get(NotAllowedDecorator::class);
131
            $exceptionDecorator = $c->get(ExceptionDecorator::class);
132
            $layout = $c->get('default_layout');
133
            $strategy = new PlatesStrategy($viewEngine, $notFoundDecorator, $notAllowedDecorator, $layout, $exceptionDecorator);
134
135
            return $strategy;
136 1
        });
137
138
        /** @var PlatesStrategy $strategy */
139 1
        $strategy = $c->get(PlatesStrategy::class);
140
        $strategy->setContainer($c);
141
142
        if ($this->i18nEnabledSite === true) {
143
            $strategy->setI18nEnabled(true);
144
            $strategy->setSupportedLocales($this->supportedLocales);
145
        }
146
147
        $this->router->setStrategy($strategy);
148
    }
149
150
    /**
151
     * @param Container $c
152
     */
153
    private function setupModules(Container $c)
154
    {
155
        // set up the modules and vendor package modules
156
        $packages = $c->get('packages');
157
        $i18n = $c->get('i18n');
158
        /** @var Translator $translator */
159
        $translator = $c->get(Translator::class);
160
161
        foreach ($packages as $packageName) {
162
            if (class_exists($packageName)) {
163
                /** @var RegistrationInterface $package */
164
                $package = new $packageName();
165
166
                if ($package->hasEntityPath()) {
167
                    $paths = $c['entity_paths'];
168
                    $paths[] = $package->getEntityPath();
169
                    $c['entity_paths'] = $paths;
170
                }
171
            }
172
        }
173
        reset($packages);
174
        foreach ($packages as $packageName) {
175
            if (class_exists($packageName)) {
176
                /** @var RegistrationInterface $package */
177
                $package = new $packageName();
178
                $package->addToContainer($c);
179
180
                if ($package instanceof RouterConfigInterface) {
181
                    $package->addRoutes($c, $this->router);
182
                }
183
184
                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
    }
193
194
    /**
195
     * @param Container $c
196
     */
197
    private function setupTranslator(Container $c)
198
    {
199
        $config = $c->get('i18n');
200
        $engine = $c->get(PlatesEngine::class);
201
        if (is_array($config)) {
202
            $factory = new TranslatorFactory();
203
            $translator = $factory->createTranslator($config);
204
            $engine->loadExtension(new Translate($translator));
205
            $engine->loadExtension(new LocaleLink());
206
            $defaultLocale = $config['default_locale'] ?: 'en_GB';
207
//            if (!in_array($locale, $config['supported_locales'])) {
208
//                $locale = $defaultLocale;
209
//            }
210
            $translator->setLocale($defaultLocale);
211
            $c[Translator::class] = $translator;
212
        }
213
    }
214
215
216
    /**
217
     * @param Container $c
218
     */
219 1
    private function setupPdoConnection(Container $c)
220
    {
221
        // set up a db connection
222
        $c[PDO::class] = $c->factory(function(Container $c): PDO {
223
            $credentials = $c->get('db');
224
            $host =$credentials['host'];
225
            $db = $credentials['database'];
226
            $user = $credentials['user'];
227
            $pass = $credentials['pass'];
228
229
            $dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [
230
                PDO::ATTR_EMULATE_PREPARES => false,
231
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
232
            ]);
233
234
            return $dbConnection;
235 1
        });
236 1
    }
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
}