Completed
Push — master ( 89196b...3c9f0f )
by Derek Stephen
01:52
created

ApplicationPackage   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 215
Duplicated Lines 7.44 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 98.04%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 11
dl 16
loc 215
ccs 100
cts 102
cp 0.9804
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setupTranslator() 0 17 3
A setupPdoConnection() 0 18 1
A getEntityPath() 0 4 1
A hasEntityPath() 0 4 1
A __construct() 0 5 1
A addToContainer() 0 9 1
A setConfigArray() 0 7 2
A setLocale() 0 8 1
A setupViewEngine() 16 46 2
B setupModules() 0 40 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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