Completed
Push — dev-master ( 645204...21f466 )
by Derek Stephen
03:26
created

ApplicationPackage::getEntityPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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