Completed
Push — dev-master ( 2915d1...bb4a55 )
by Derek Stephen
02:08
created

ApplicationPackage::setupViewEngine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 48

Duplication

Lines 16
Ratio 33.33 %

Code Coverage

Tests 29
CRAP Score 2

Importance

Changes 0
Metric Value
dl 16
loc 48
ccs 29
cts 29
cp 1
rs 9.1344
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 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 3
            $strategy->setI18nEnabled(true);
127 3
            $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 3
                    $paths = $c['entity_paths'];
148 3
                    $paths[] = $package->getEntityPath();
149 3
                    $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
            $dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [
197 1
                PDO::ATTR_EMULATE_PREPARES => false,
198
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
199
            ]);
200
201 1
            return $dbConnection;
202 3
        });
203 3
    }
204
205
    /**
206
     * @return string
207
     */
208 1
    function getEntityPath(): string
209
    {
210 1
        return '';
211
    }
212
213
    /**
214
     * @return bool
215
     */
216 1
    function hasEntityPath(): bool
217
    {
218 1
        return false;
219
    }
220
}