Completed
Push — dev-master ( f1d44b...511541 )
by Derek Stephen
01:34
created

Application   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 12
dl 0
loc 142
ccs 48
cts 51
cp 0.9412
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A __clone() 0 1 1
A ahoy() 0 16 3
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
A setSail() 0 35 4
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Barnacle\Container;
6
use Barnacle\RegistrationInterface;
7
use Bone\Mvc\Router;
8
use Bone\Mvc\Router\Decorator\ExceptionDecorator;
9
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
10
use Bone\Mvc\Router\NotFoundException;
11
use Bone\Mvc\Router\PlatesStrategy;
12
use Bone\Mvc\Router\RouterConfigInterface;
13
use Bone\Mvc\View\PlatesEngine;
14
use Bone\Server\Environment;
15
use Bone\Server\I18nHandler;
16
use Bone\Server\SiteConfig;
17
use Del\SessionManager;
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 Laminas\Diactoros\ResponseFactory;
25
use Laminas\Diactoros\ServerRequestFactory;
26
use Laminas\Diactoros\Response;
27
use Laminas\Diactoros\Response\RedirectResponse;
28
use Laminas\Diactoros\Uri;
29
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
30
use Laminas\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
    /**
78
     *
79
     * T' the high seas! Garrr!
80
     *
81
     * @return bool
82
     * @throws \Exception
83
     */
84 8
    public function setSail()
85
    {
86
        // load in the config and set up the dependency injection container
87 8
        $env = new Environment($_SERVER);
88 8
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
89 8
        $router = $this->treasureChest[Router::class] = new Router();
90
        
91 8
        $config = $env->fetchConfig($this->configFolder, $this->environment);
92 8
        $config[Environment::class] = $env;
93 8
        $config[SiteConfig::class] = new SiteConfig($config, $env);
94
        
95 8
        $package = new ApplicationPackage($config, $router);
96 8
        $package->addToContainer($this->treasureChest);
97
98 8
        if ($this->isMultilingual()) {
99
100
            try {
101 7
                $request = $this->i18nRequestCheck($request);
102 7
                $response = $router->dispatch($request);
103
            } catch (NotFoundException $e) {
104
                $response = new RedirectResponse($e->getMessage());
105
                if ($e->getRequest()->getMethod() !== 'GET') {
106 7
                    $response = $router->dispatch($request);
107
                }
108
            }
109
110
        } else {
111 1
            $request = $this->i18nRequestCheck($request, false);
112 1
            $response = $router->dispatch($request);
113
        }
114
115 8
        (new SapiEmitter)->emit($response);
116
117 8
        return true;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123 8
    public function isMultilingual(): bool
124
    {
125 8
        $i18n = $this->treasureChest->get('i18n');
126 8
        return $i18n['enabled'];
127
    }
128
129
130
    /**
131
     * @param ServerRequestInterface $request
132
     * @param bool $handle
133
     * @return ServerRequestInterface
134
     * @throws NotFoundException
135
     */
136 8
    private function i18nRequestCheck(ServerRequestInterface $request, bool $handle = true): ServerRequestInterface
137
    {
138 8
        $i18n = $this->treasureChest->get('i18n');
139 8
        $translator = $this->treasureChest->get(Translator::class);
140 8
        $i18nHandler = new I18nHandler($translator, $i18n['supported_locales'], $i18n['default_locale']);
141 8
        if ($handle) {
142 7
            $request = $i18nHandler->handleI18n($request);
143
        } else {
144 1
            $request = $i18nHandler->removeI18n($request);
145
        }
146
147 8
        return $request;
148
    }
149
150
    /**
151
     * @return Container
152
     */
153 1
    public function getContainer(): Container
154
    {
155 1
        return $this->treasureChest;
156
    }
157
158
    /**
159
     * @param string $configFolder
160
     */
161 8
    public function setConfigFolder(string $configFolder)
162
    {
163 8
        $this->configFolder = $configFolder;
164 8
    }
165
166
    /**
167
     * @param string $environment
168
     */
169 1
    public function setEnvironment(string $environment)
170
    {
171 1
        $this->environment = $environment;
172
    }
173
}