Completed
Push — dev-master ( b7e24f...52af7d )
by Derek Stephen
03:02
created

ApplicationPackage   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 233
Duplicated Lines 6.87 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 10
dl 16
loc 233
ccs 0
cts 134
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addToContainer() 0 10 1
A setConfigArray() 0 7 2
A setLocale() 0 8 1
A setSerializer() 0 4 1
B setupViewEngine() 16 55 2
A setupModules() 0 24 5
A setupTranslator() 0 17 3
A setupPaginator() 0 12 1
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\Mvc\Router\Decorator\ExceptionDecorator;
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\Mvc\View\Helper\Paginator;
14
use Bone\Mvc\View\PlatesEngine;
15
use Bone\Mvc\View\ViewRenderer;
16
use Bone\Service\TranslatorFactory;
17
use League\Route\Router;
18
use Locale;
19
use Psr\Http\Server\MiddlewareInterface;
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
    public function __construct(array $config, Router $router)
42
    {
43
        $this->config = $config;
44
        $this->router = $router;
45
    }
46
47
    /**
48
     * @param Container $c
49
     */
50
    public function addToContainer(Container $c)
51
    {
52
        $this->setConfigArray($c);
53
        $this->setLocale($c);
54
        $this->setupPdoConnection($c);
55
        $this->setupViewEngine($c);
56
        $this->setupTranslator($c);
57
        $this->setupPaginator($c);
58
        $this->setupModules($c);
59
    }
60
61
    /**
62
     * @param Container $c
63
     */
64
    private function setConfigArray(Container $c)
65
    {
66
        foreach($this->config as $key => $value)
67
        {
68
            $c[$key] = $value;
69
        }
70
    }
71
72
    /**
73
     * @param Container $c
74
     */
75
    private function setLocale(Container $c)
76
    {
77
        $i18n = $c->get('i18n');
78
        $this->i18nEnabledSite = $i18n['enabled'];
79
        $this->supportedLocales = $i18n['supported_locales'];
80
        $defaultLocale = $i18n['default_locale'];
81
        Locale::setDefault($defaultLocale);
82
    }
83
84
    /**
85
     * @param Container $c
86
     */
87
    private function setSerializer(Container $c)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
88
    {
89
        $c[SerializerInterface::class] = $serializer = SerializerBuilder::create()->build();;
0 ignored issues
show
Unused Code introduced by
$serializer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
    }
91
92
    /**
93
     * @param Container $c
94
     */
95
    private function setupViewEngine(Container $c)
96
    {
97
        // set up the view engine dependencies
98
        $viewEngine = new PlatesEngine($c->get('viewFolder'));
99
//        $i18nExtension = new Translate();
100
//        $viewEngine->loadExtension();
101
102
        $c[PlatesEngine::class] = $viewEngine;
103
104 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...
105
            $layout = $c->get('default_layout');
106
            $viewEngine = $c->get(PlatesEngine::class);
107
            $notFoundDecorator = new NotFoundDecorator($viewEngine);
108
            $notFoundDecorator->setLayout($layout);
109
110
            return $notFoundDecorator;
111
        });
112
113 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...
114
            $layout = $c->get('default_layout');
115
            $viewEngine = $c->get(PlatesEngine::class);
116
            $notAllowedDecorator = new NotAllowedDecorator($viewEngine);
117
            $notAllowedDecorator->setLayout($layout);
118
119
            return $notAllowedDecorator;
120
        });
121
122
        $c[ExceptionDecorator::class] = $c->factory(function (Container $c) {
123
            $viewEngine = $c->get(PlatesEngine::class);
124
            $notFoundDecorator = new ExceptionDecorator($viewEngine);
125
126
            return $notFoundDecorator;
127
        });
128
129
        $c[PlatesStrategy::class] = $c->factory(function (Container $c) {
130
            $viewEngine = $c->get(PlatesEngine::class);
131
            $notFoundDecorator = $c->get(NotFoundDecorator::class);
132
            $notAllowedDecorator = $c->get(NotAllowedDecorator::class);
133
            $layout = $c->get('default_layout');
134
            $strategy = new PlatesStrategy($viewEngine, $notFoundDecorator, $notAllowedDecorator, $layout);
135
136
            return $strategy;
137
        });
138
139
        /** @var PlatesStrategy $strategy */
140
        $strategy = $c->get(PlatesStrategy::class);
141
        $strategy->setContainer($c);
142
143
        if ($this->i18nEnabledSite === true) {
144
            $strategy->setI18nEnabled(true);
145
            $strategy->setSupportedLocales($this->supportedLocales);
146
        }
147
148
        $this->router->setStrategy($strategy);
149
    }
150
151
    /**
152
     * @param Container $c
153
     */
154
    private function setupModules(Container $c)
155
    {
156
        // set up the modules and vendor package modules
157
        $packages = $c->get('packages');
158
159
        foreach ($packages as $packageName) {
160
            if (class_exists($packageName)) {
161
                /** @var RegistrationInterface $package */
162
                $package = new $packageName();
163
164
                if ($package->hasEntityPath()) {
165
                    $paths = $c['entity_paths'];
166
                    $paths[] = $package->getEntityPath();
167
                    $c['entity_paths'] = $paths;
168
                }
169
170
                $package->addToContainer($c);
171
172
                if ($package instanceof RouterConfigInterface) {
173
                    $package->addRoutes($c, $this->router);
174
                }
175
            }
176
        }
177
    }
178
179
    /**
180
     * @param Container $c
181
     */
182
    private function setupTranslator(Container $c)
183
    {
184
        $config = $c->get('i18n');
185
        $engine = $c->get(PlatesEngine::class);
186
        if (is_array($config)) {
187
            $factory = new TranslatorFactory();
188
            $translator = $factory->createTranslator($config);
189
            $c['translator'] = $translator;
190
            $engine->loadExtension(new Translate($translator));
191
            $defaultLocale = $config['default_locale'] ?: 'en_GB';
192
//            if (!in_array($locale, $config['supported_locales'])) {
193
//                $locale = $defaultLocale;
194
//            }
195
            $translator->setLocale($defaultLocale);
196
            $c[Translator::class] = $translator;
197
        }
198
    }
199
200
    /**
201
     * @param Container $c
202
     */
203
    private function setupPaginator(Container $c)
204
    {
205
        $c[Paginator::class] = $c->factory(function (Container $c, string $urlPart) {
0 ignored issues
show
Unused Code introduced by
The parameter $urlPart is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
            $config = $c->get('paginator');
207
            $numPerPage = $config['num _per_page'];
0 ignored issues
show
Unused Code introduced by
$numPerPage is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
208
            $pagerSize = $config['pager_size'];
0 ignored issues
show
Unused Code introduced by
$pagerSize is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
209
            $urlPart = $config['url_part'];
210
            $pager = new Paginator();
211
            $pager->setPagerSize(5);
212
            $pager->setUrlPart($urlPart);
213
        });
214
    }
215
216
217
    /**
218
     * @param Container $c
219
     */
220
    private function setupPdoConnection(Container $c)
221
    {
222
        // set up a db connection
223
        $c[PDO::class] = $c->factory(function(Container $c): PDO {
224
            $credentials = $c->get('db');
225
            $host =$credentials['host'];
226
            $db = $credentials['database'];
227
            $user = $credentials['user'];
228
            $pass = $credentials['pass'];
229
230
            $dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [
231
                PDO::ATTR_EMULATE_PREPARES => false,
232
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
233
            ]);
234
235
            return $dbConnection;
236
        });
237
    }
238
239
    /**
240
     * @return string
241
     */
242
    function getEntityPath(): string
243
    {
244
        return '';
245
    }
246
247
    /**
248
     * @return bool
249
     */
250
    function hasEntityPath(): bool
251
    {
252
        return false;
253
    }
254
}