Completed
Push — dev-master ( 3f66ae...92266b )
by Derek Stephen
01:52
created

ApplicationPackage   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 214
Duplicated Lines 7.48 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 97.03%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 10
dl 16
loc 214
c 0
b 0
f 0
ccs 98
cts 101
cp 0.9703
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
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
A setupTranslator() 0 16 3
A setupPdoConnection() 0 18 1
A getEntityPath() 0 4 1
A hasEntityPath() 0 4 1

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