Completed
Push — dev-master ( e95ad3...7a0944 )
by Derek Stephen
02:18
created

ApplicationPackage::setupPdoConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1.0005

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.0005
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 PDO;
18
use Zend\I18n\Translator\Translator;
19
20
class ApplicationPackage implements RegistrationInterface
21
{
22
    /** @var array $config */
23
    private $config;
24
25
    /** @var Router $router */
26
    private $router;
27
28
   /** @var bool $i18nEnabledSite */
29
    private $i18nEnabledSite = false;
30
31
    /** @var array $supportedLocales */
32
    private $supportedLocales = [];
33
34
    /**
35
     * ApplicationPackage constructor.
36
     * @param array $config
37
     * @param \League\Route\Router $router
38
     */
39 3
    public function __construct(array $config, Router $router)
40
    {
41 3
        $this->config = $config;
42 3
        $this->router = $router;
43 3
    }
44
45
    /**
46
     * @param Container $c
47
     */
48 3
    public function addToContainer(Container $c)
49
    {
50 3
        $this->setConfigArray($c);
51 3
        $this->setLocale($c);
52 3
        $this->setupPdoConnection($c);
53 3
        $this->setupViewEngine($c);
54 3
        $this->setupTranslator($c);
55 3
        $this->setupModules($c);
56 3
    }
57
58
    /**
59
     * @param Container $c
60
     */
61 3
    private function setConfigArray(Container $c)
62
    {
63 3
        foreach($this->config as $key => $value)
64
        {
65 3
            $c[$key] = $value;
66
        }
67 3
    }
68
69
    /**
70
     * @param Container $c
71
     */
72 3
    private function setLocale(Container $c)
73
    {
74 3
        $i18n = $c->get('i18n');
75 3
        $this->i18nEnabledSite = $i18n['enabled'];
76 3
        $this->supportedLocales = $i18n['supported_locales'];
77 3
        $defaultLocale = $i18n['default_locale'];
78 3
        Locale::setDefault($defaultLocale);
79 3
    }
80
81
    /**
82
     * @param Container $c
83
     */
84 3
    private function setupViewEngine(Container $c)
85
    {
86
        // set up the view engine dependencies
87 3
        $viewEngine = new PlatesEngine($c->get('viewFolder'));
88
//        $i18nExtension = new Translate();
89
//        $viewEngine->loadExtension();
90
91 3
        $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 3
            $layout = $c->get('default_layout');
95 3
            $viewEngine = $c->get(PlatesEngine::class);
96 3
            $notFoundDecorator = new NotFoundDecorator($viewEngine);
97 3
            $notFoundDecorator->setLayout($layout);
98
99 3
            return $notFoundDecorator;
100 3
        });
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 3
            $layout = $c->get('default_layout');
104 3
            $viewEngine = $c->get(PlatesEngine::class);
105 3
            $notAllowedDecorator = new NotAllowedDecorator($viewEngine);
106 3
            $notAllowedDecorator->setLayout($layout);
107
108 3
            return $notAllowedDecorator;
109 3
        });
110
111
        $c[PlatesStrategy::class] = $c->factory(function (Container $c) {
112 3
            $viewEngine = $c->get(PlatesEngine::class);
113 3
            $notFoundDecorator = $c->get(NotFoundDecorator::class);
114 3
            $notAllowedDecorator = $c->get(NotAllowedDecorator::class);
115 3
            $layout = $c->get('default_layout');
116 3
            $strategy = new PlatesStrategy($viewEngine, $notFoundDecorator, $notAllowedDecorator, $layout);
117
118 3
            return $strategy;
119 3
        });
120
121
        /** @var PlatesStrategy $strategy */
122 3
        $strategy = $c->get(PlatesStrategy::class);
123 3
        $strategy->setContainer($c);
124
125 3
        if ($this->i18nEnabledSite === true) {
126
            $strategy->setI18nEnabled(true);
127
            $strategy->setSupportedLocales($this->supportedLocales);
128
        }
129
130 3
        $this->router->setStrategy($strategy);
131 3
    }
132
133
    /**
134
     * @param Container $c
135
     */
136 3
    private function setupModules(Container $c)
137
    {
138
        // set up the modules and vendor package modules
139 3
        $packages = $c->get('packages');
140
141 3
        foreach ($packages as $packageName) {
142 3
            if (class_exists($packageName)) {
143
                /** @var RegistrationInterface $package */
144 3
                $package = new $packageName();
145
146 3
                if ($package->hasEntityPath()) {
147
                    $paths = $c['entity_paths'];
148
                    $paths[] = $package->getEntityPath();
149
                    $c['entity_paths'] = $paths;
150
                }
151
152 3
                $package->addToContainer($c);
153
154 3
                if ($package instanceof RouterConfigInterface) {
155 3
                    $package->addRoutes($c, $this->router);
156
                }
157
            }
158
        }
159 3
    }
160
161
    /**
162
     * @param Container $c
163
     */
164 3
    private function setupTranslator(Container $c)
165
    {
166 3
        $config = $c->get('i18n');
167 3
        $engine = $c->get(PlatesEngine::class);
168 3
        if (is_array($config)) {
169 3
            $factory = new TranslatorFactory();
170 3
            $translator = $factory->createTranslator($config);
171 3
            $c['translator'] = $translator;
172 3
            $engine->loadExtension(new Translate($translator));
173 3
            $defaultLocale = $config['default_locale'] ?: 'en_GB';
174
//            if (!in_array($locale, $config['supported_locales'])) {
175
//                $locale = $defaultLocale;
176
//            }
177 3
            $translator->setLocale($defaultLocale);
178 3
            $c[Translator::class] = $translator;
179
        }
180 3
    }
181
182
183
    /**
184
     * @param Container $c
185
     */
186 3
    private function setupPdoConnection(Container $c)
187
    {
188
        // set up a db connection
189
        $c[PDO::class] = $c->factory(function(Container $c): PDO {
190 1
            $credentials = $c->get('db');
191 1
            $host =$credentials['host'];
192 1
            $db = $credentials['database'];
193 1
            $user = $credentials['user'];
194 1
            $pass = $credentials['pass'];
195
196 1
            codecept_debug('mysql:host='.$host.';dbname='.$db, $user, $pass);
0 ignored issues
show
Unused Code introduced by
The call to codecept_debug() has too many arguments starting with $user.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
197 1
            $dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [
198 1
                PDO::ATTR_EMULATE_PREPARES => false,
199
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
200
            ]);
201
202
            return $dbConnection;
203 3
        });
204 3
    }
205
206
    /**
207
     * @return string
208
     */
209 1
    function getEntityPath(): string
210
    {
211 1
        return '';
212
    }
213
214
    /**
215
     * @return bool
216
     */
217 1
    function hasEntityPath(): bool
218
    {
219 1
        return false;
220
    }
221
}