Completed
Push — master ( 5a2095...cba5ab )
by Derek Stephen
02:24 queued 11s
created

Application   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 94.55%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 12
dl 0
loc 154
ccs 52
cts 55
cp 0.9455
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A __clone() 0 1 1
A ahoy() 0 16 3
A bootstrap() 0 14 1
A setSail() 0 28 4
A isMultilingual() 0 5 1
A i18nRequestCheck() 0 13 2
A getContainer() 0 4 1
A setConfigFolder() 0 4 1
A setEnvironment() 0 4 1
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\NotFoundDecorator;
9
use Bone\Mvc\Router\NotFoundException;
10
use Bone\Mvc\Router\PlatesStrategy;
11
use Bone\Mvc\Router\RouterConfigInterface;
12
use Bone\Mvc\View\PlatesEngine;
13
use Bone\Server\Environment;
14
use Bone\Server\I18nHandler;
15
use Bone\Server\SiteConfig;
16
use Del\SessionManager;
17
use League\Route\Router;
18
use League\Route\RouteGroup;
19
use League\Route\Strategy\ApplicationStrategy;
20
use League\Route\Strategy\JsonStrategy;
21
use PDO;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Zend\Diactoros\ResponseFactory;
25
use Zend\Diactoros\ServerRequestFactory;
26
use Zend\Diactoros\Response;
27
use Zend\Diactoros\Response\RedirectResponse;
28
use Zend\Diactoros\Uri;
29
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;
30
use Zend\I18n\Translator\Translator;
31
32
class Application
33
{
34
    /** @var Container $registry */
35
    private $treasureChest;
36
37
    /** @var string $configFolder */
38
    private $configFolder = 'config';
39
40
    /** @var string $environment */
41
    private $environment = 'production';
42
43
    /**
44
     *  There be nay feckin wi' constructors on board this ship
45
     *  There be nay copyin' o' th'ship either
46
     *  This ship is a singleton!
47
     */
48
    private function __construct(){}
49
50
    private function __clone(){}
51
52
53
    /**
54
     *  Ahoy! There nay be boardin without yer configuration
55
     *
56
     * @param array $config
0 ignored issues
show
Bug introduced by
There is no parameter named $config. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
     * @return Application
58
     */
59 10
    public static function ahoy()
60
    {
61 10
        static $inst = null;
62 10
        if ($inst === null) {
63 1
            $inst = new Application();
64 1
            $session = SessionManager::getInstance();
65 1
            SessionManager::sessionStart('app');
66 1
            $inst->treasureChest = new Container();
67 1
            $inst->treasureChest[SessionManager::class] = $session;
68 1
            $env = getenv('APPLICATION_ENV');
69 1
            if ($env) {
70 1
                $inst->setEnvironment($env);
71
            }
72
        }
73 10
        return $inst;
74
    }
75
76
    /**
77
     *  Use this to bootstrap Bone without dispatching any request
78
     *  i.e. for when using the framework in a CLI application
79
     */
80 8
    public function bootstrap(): Container
81
    {
82 8
        $env = new Environment($_SERVER);
83 8
        $router = $this->treasureChest[Router::class] = new Router();
84
85 8
        $config = $env->fetchConfig($this->configFolder, $this->environment);
86 8
        $config[Environment::class] = $env;
87 8
        $config[SiteConfig::class] = new SiteConfig($config, $env);
88
89 8
        $package = new ApplicationPackage($config, $router);
90 8
        $package->addToContainer($this->treasureChest);
91
92 8
        return $this->treasureChest;
93
    }
94
95
96
    /**
97
     *
98
     * T' the high seas! Garrr!
99
     *
100
     * @return bool
101
     * @throws \Exception
102
     */
103 8
    public function setSail()
104
    {
105
        // load in the config and set up the dependency injection container
106 8
        $this->bootstrap();
107 8
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
108 8
        $router = $this->treasureChest->get(Router::class);
109
110 8
        if ($this->isMultilingual()) {
111
112
            try {
113 7
                $request = $this->i18nRequestCheck($request);
114 7
                $response = $router->dispatch($request);
115
            } catch (NotFoundException $e) {
116
                $response = new RedirectResponse($e->getMessage());
117
                if ($e->getRequest()->getMethod() !== 'GET') {
118 7
                    $response = $router->dispatch($request);
119
                }
120
            }
121
122
        } else {
123 1
            $request = $this->i18nRequestCheck($request, false);
124 1
            $response = $router->dispatch($request);
125
        }
126
127 8
        (new SapiEmitter)->emit($response);
128
129 8
        return true;
130
    }
131
132
    /**
133
     * @return bool
134
     */
135 8
    public function isMultilingual(): bool
136
    {
137 8
        $i18n = $this->treasureChest->get('i18n');
138 8
        return $i18n['enabled'];
139
    }
140
141
142
    /**
143
     * @param ServerRequestInterface $request
144
     * @param bool $handle
145
     * @return ServerRequestInterface
146
     * @throws NotFoundException
147
     */
148 8
    private function i18nRequestCheck(ServerRequestInterface $request, bool $handle = true): ServerRequestInterface
149
    {
150 8
        $i18n = $this->treasureChest->get('i18n');
151 8
        $translator = $this->treasureChest->get(Translator::class);
152 8
        $i18nHandler = new I18nHandler($translator, $i18n['supported_locales'], $i18n['default_locale']);
153 8
        if ($handle) {
154 7
            $request = $i18nHandler->handleI18n($request);
155
        } else {
156 1
            $request = $i18nHandler->removeI18n($request);
157
        }
158
159 8
        return $request;
160
    }
161
162
    /**
163
     * @return Container
164
     */
165 1
    public function getContainer(): Container
166
    {
167 1
        return $this->treasureChest;
168
    }
169
170
    /**
171
     * @param string $configFolder
172
     */
173 8
    public function setConfigFolder(string $configFolder)
174
    {
175 8
        $this->configFolder = $configFolder;
176 8
    }
177
178
    /**
179
     * @param string $environment
180
     */
181 1
    public function setEnvironment(string $environment)
182
    {
183 1
        $this->environment = $environment;
184
    }
185
}