Completed
Push — master ( ac3ea1...176022 )
by Derek Stephen
02:14
created

ApplicationPackage::getEntityPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 9
    public function __construct(array $config, Router $router)
40
    {
41 9
        $this->config = $config;
42 9
        $this->router = $router;
43 9
    }
44
45
    /**
46
     * @param Container $c
47
     */
48 9
    public function addToContainer(Container $c)
49
    {
50 9
        $this->setConfigArray($c);
51 9
        $this->setLocale($c);
52 9
        $this->setupPdoConnection($c);
53 9
        $this->setupViewEngine($c);
54 9
        $this->setupTranslator($c);
55 9
        $this->setupModules($c);
56 9
    }
57
58
    /**
59
     * @param Container $c
60
     */
61 9
    private function setConfigArray(Container $c)
62
    {
63 9
        foreach($this->config as $key => $value)
64
        {
65 9
            $c[$key] = $value;
66
        }
67 9
    }
68
69
    /**
70
     * @param Container $c
71
     */
72 9
    private function setLocale(Container $c)
73
    {
74 9
        $i18n = $c->get('i18n');
75 9
        $this->i18nEnabledSite = $i18n['enabled'];
76 9
        $this->supportedLocales = $i18n['supported_locales'];
77 9
        $defaultLocale = $i18n['default_locale'];
78 9
        Locale::setDefault($defaultLocale);
79 9
    }
80
81
    /**
82
     * @param Container $c
83
     */
84 9
    private function setupViewEngine(Container $c)
85
    {
86
        // set up the view engine dependencies
87 9
        $viewEngine = new PlatesEngine($c->get('viewFolder'));
88
//        $i18nExtension = new Translate();
89
//        $viewEngine->loadExtension();
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
141 9
        foreach ($packages as $packageName) {
142 9
            if (class_exists($packageName)) {
143
                /** @var RegistrationInterface $package */
144 9
                $package = new $packageName();
145
146 9
                if ($package->hasEntityPath()) {
147 9
                    $paths = $c['entity_paths'];
148 9
                    $paths[] = $package->getEntityPath();
149 9
                    $c['entity_paths'] = $paths;
150
                }
151
            }
152
        }
153 9
        reset($packages);
154 9
        foreach ($packages as $packageName) {
155 9
            if (class_exists($packageName)) {
156
                /** @var RegistrationInterface $package */
157 9
                $package = new $packageName();
158 9
                $package->addToContainer($c);
159
160 9
                if ($package instanceof RouterConfigInterface) {
161 9
                    $package->addRoutes($c, $this->router);
162
                }
163
            }
164
        }
165 9
    }
166
167
    /**
168
     * @param Container $c
169
     */
170 9
    private function setupTranslator(Container $c)
171
    {
172 9
        $config = $c->get('i18n');
173 9
        $engine = $c->get(PlatesEngine::class);
174 9
        if (is_array($config)) {
175 9
            $factory = new TranslatorFactory();
176 9
            $translator = $factory->createTranslator($config);
177 9
            $c['translator'] = $translator;
178 9
            $engine->loadExtension(new Translate($translator));
179 9
            $defaultLocale = $config['default_locale'] ?: 'en_GB';
180
//            if (!in_array($locale, $config['supported_locales'])) {
181
//                $locale = $defaultLocale;
182
//            }
183 9
            $translator->setLocale($defaultLocale);
184 9
            $c[Translator::class] = $translator;
185
        }
186 9
    }
187
188
189
    /**
190
     * @param Container $c
191
     */
192 9
    private function setupPdoConnection(Container $c)
193
    {
194
        // set up a db connection
195
        $c[PDO::class] = $c->factory(function(Container $c): PDO {
196 1
            $credentials = $c->get('db');
197 1
            $host =$credentials['host'];
198 1
            $db = $credentials['database'];
199 1
            $user = $credentials['user'];
200 1
            $pass = $credentials['pass'];
201
202 1
            $dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [
203 1
                PDO::ATTR_EMULATE_PREPARES => false,
204
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
205
            ]);
206
207 1
            return $dbConnection;
208 9
        });
209 9
    }
210
211
    /**
212
     * @return string
213
     */
214 1
    function getEntityPath(): string
215
    {
216 1
        return '';
217
    }
218
219
    /**
220
     * @return bool
221
     */
222 1
    function hasEntityPath(): bool
223
    {
224 1
        return false;
225
    }
226
}