Completed
Push — dev-master ( 645204...21f466 )
by Derek Stephen
03:26
created

Application::setConfigFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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