Completed
Push — master ( 24ccf8...89196b )
by Derek Stephen
01:45
created

Application::i18nRequestCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 8
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\PlatesStrategy;
10
use Bone\Mvc\Router\RouterConfigInterface;
11
use Bone\Mvc\View\PlatesEngine;
12
use Bone\Server\Environment;
13
use Bone\Server\I18nHandler;
14
use Bone\Server\SiteConfig;
15
use League\Route\Http\Exception\NotFoundException;
16
use League\Route\Router;
17
use League\Route\RouteGroup;
18
use League\Route\Strategy\ApplicationStrategy;
19
use League\Route\Strategy\JsonStrategy;
20
use PDO;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Zend\Diactoros\ResponseFactory;
24
use Zend\Diactoros\ServerRequestFactory;
25
use Zend\Diactoros\Response;
26
use Zend\Diactoros\Response\RedirectResponse;
27
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;
28
use Zend\I18n\Translator\Translator;
29
30
class Application
31
{
32
    /** @var Container $registry */
33
    private $treasureChest;
34
35
    /** @var string $configFolder */
36
    private $configFolder = 'config';
37
38
    /** @var string $environment */
39
    private $environment = 'production';
40
41
    /**
42
     *  There be nay feckin wi' constructors on board this ship
43
     *  There be nay copyin' o' th'ship either
44
     *  This ship is a singleton!
45
     */
46
    private function __construct(){}
47
48
    public function __clone(){}
49
50
51
    /**
52
     *  Ahoy! There nay be boardin without yer configuration
53
     *
54
     * @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...
55
     * @return Application
56
     */
57 10
    public static function ahoy()
58
    {
59 10
        static $inst = null;
60 10
        if ($inst === null) {
61 1
            $inst = new Application();
62 1
            $inst->treasureChest = new Container();
63 1
            $env = getenv('APPLICATION_ENV');
64 1
            if ($env) {
65 1
                $inst->setEnvironment($env);
66
            }
67
        }
68 10
        return $inst;
69
    }
70
71
72
    /**
73
     *
74
     * T' the high seas! Garrr!
75
     *
76
     * @return bool
77
     * @throws \Exception
78
     */
79 8
    public function setSail()
80
    {
81
        // load in the config and set up the dependency injection container
82 8
        $env = new Environment($_SERVER);
83 8
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
84 8
        $router = $this->treasureChest[Router::class] = new Router();
85
        
86 8
        $config = $env->fetchConfig($this->configFolder, $this->environment);
87 8
        $config[Environment::class] = $env;
88 8
        $config[SiteConfig::class] = new SiteConfig($config['site'], $env);
0 ignored issues
show
Documentation introduced by
$config['site'] is of type object<Bone\Server\Environment>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
        
90
        $package = new ApplicationPackage($config, $router);
91
        $package->addToContainer($this->treasureChest);
92
93
        if ($this->isMultilingual()) {
94
95
            try {
96
                $request = $this->i18nRequestCheck($request);
97
                $response = $router->dispatch($request);
98
            } catch (NotFoundException $e) {
99
                $response = new RedirectResponse($e->getMessage());
100
            }
101
102
        } else {
103
            $request = $this->i18nRequestCheck($request, false);
104
            $response = $router->dispatch($request);
105
        }
106
107
        (new SapiEmitter)->emit($response);
108
109
        return true;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function isMultilingual(): bool
116
    {
117
        $i18n = $this->treasureChest->get('i18n');
118
        return $i18n['enabled'];
119
    }
120
121
122
    /**
123
     * @param ServerRequestInterface $request
124
     * @param bool $handle
125
     * @return ServerRequestInterface
126
     * @throws NotFoundException
127
     */
128
    private function i18nRequestCheck(ServerRequestInterface $request, bool $handle = true): ServerRequestInterface
129
    {
130
        $i18n = $this->treasureChest->get('i18n');
131
        $translator = $this->treasureChest->get(Translator::class);
132
        $i18nHandler = new I18nHandler($translator, $i18n['supported_locales']);
133
        if ($handle) {
134
            $request = $i18nHandler->handleI18n($request);
135
        } else {
136
            $request = $i18nHandler->removeI18n($request);
137
        }
138
139
        return $request;
140
    }
141
142
    /**
143
     * @return Container
144
     */
145 1
    public function getContainer(): Container
146
    {
147 1
        return $this->treasureChest;
148
    }
149
150
    /**
151
     * @param string $configFolder
152
     */
153 8
    public function setConfigFolder(string $configFolder)
154
    {
155 8
        $this->configFolder = $configFolder;
156 8
    }
157
158
    /**
159
     * @param string $environment
160
     */
161 1
    public function setEnvironment(string $environment)
162
    {
163 1
        $this->environment = $environment;
164
    }
165
}