Completed
Push — dev-master ( c11256...d5c375 )
by Derek Stephen
02:03
created

Application::isMultilingual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
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\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
29
class Application
30
{
31
    /** @var Container $registry */
32
    private $treasureChest;
33
34
    /** @var string $configFolder */
35
    private $configFolder = 'config';
36
37
    /** @var string $environment */
38
    private $environment = 'production';
39
40
    /**
41
     *  There be nay feckin wi' constructors on board this ship
42
     *  There be nay copyin' o' th'ship either
43
     *  This ship is a singleton!
44
     */
45
    private function __construct(){}
46
47
    public function __clone(){}
48
49
50
    /**
51
     *  Ahoy! There nay be boardin without yer configuration
52
     *
53
     * @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...
54
     * @return Application
55
     */
56 10
    public static function ahoy()
57
    {
58 10
        static $inst = null;
59 10
        if ($inst === null) {
60 1
            $inst = new Application();
61 1
            $inst->treasureChest = new Container();
62 1
            $env = getenv('APPLICATION_ENV');
63 1
            if ($env) {
64 1
                $inst->setEnvironment($env);
65
            }
66
        }
67 10
        return $inst;
68
    }
69
70
71
    /**
72
     *
73
     * T' the high seas! Garrr!
74
     *
75
     * @return bool
76
     * @throws \Exception
77
     */
78 8
    public function setSail()
79
    {
80
        // load in the config and set up the dependency injection container
81 8
        $env = new Environment($_SERVER);
82 8
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
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['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...
88
        
89 8
        $package = new ApplicationPackage($config, $router);
90 8
        $package->addToContainer($this->treasureChest);
91
92 8
        if ($this->isMultilingual()) {
93
94
            try {
95 7
                $request = $this->i18nRequestCheck($request);
96 6
                $response = $router->dispatch($request);
97 1
            } catch (NotFoundException $e) {
98 7
                $response = new RedirectResponse($e->getMessage());
99
            }
100
101
        } else {
102 1
            $request = $this->i18nRequestCheck($request, false);
103 1
            $response = $router->dispatch($request);
104
        }
105
106 8
        (new SapiEmitter)->emit($response);
107
108 8
        return true;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 8
    public function isMultilingual(): bool
115
    {
116 8
        $i18n = $this->treasureChest->get('i18n');
117 8
        return $i18n['enabled'];
118
    }
119
120
121
    /**
122
     * @param ServerRequestInterface $request
123
     * @param bool $handle
124
     * @return ServerRequestInterface
125
     * @throws NotFoundException
126
     */
127 8
    private function i18nRequestCheck(ServerRequestInterface $request, bool $handle = true): ServerRequestInterface
128
    {
129 8
        $i18n = $this->treasureChest->get('i18n');
130 8
        $translator = $this->treasureChest->get('translator');
131 8
        $i18nHandler = new I18nHandler($translator, $i18n['supported_locales']);
132 8
        if ($handle) {
133 7
            $request = $i18nHandler->handleI18n($request);
134
        } else {
135 1
            $request = $i18nHandler->removeI18n($request);
136
        }
137
138 7
        return $request;
139
    }
140
141
    /**
142
     * @return Container
143
     */
144 1
    public function getContainer(): Container
145
    {
146 1
        return $this->treasureChest;
147
    }
148
149
    /**
150
     * @param string $configFolder
151
     */
152 8
    public function setConfigFolder(string $configFolder)
153
    {
154 8
        $this->configFolder = $configFolder;
155 8
    }
156
157
    /**
158
     * @param string $environment
159
     */
160 1
    public function setEnvironment(string $environment)
161
    {
162 1
        $this->environment = $environment;
163
    }
164
}