Completed
Push — master ( a7be56...b163e4 )
by Derek Stephen
01:08
created

Application   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 0
loc 117
ccs 25
cts 40
cp 0.625
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bone;
4
5
use Barnacle\Container;
6
use Barnacle\RegistrationInterface;
7
use Bone\Http\Middleware\Stack;
8
use Bone\Router\Router;
9
use Bone\Router\Decorator\ExceptionDecorator;
10
use Bone\Router\Decorator\NotFoundDecorator;
11
use Bone\Router\NotFoundException;
12
use Bone\Router\PlatesStrategy;
13
use Bone\Router\Router;ConfigInterface;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Bone\Router\Router as Router because the name is already in use
Loading history...
14
use Bone\View\PlatesEngine;
15
use Bone\Server\Environment;
16
use Bone\I18n\Http\Middleware\I18nHandler;
17
use Bone\Server\SiteConfig;
18
use Del\SessionManager;
19
use League\Route\RouteGroup;
20
use League\Route\Strategy\ApplicationStrategy;
21
use League\Route\Strategy\JsonStrategy;
22
use PDO;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Laminas\Diactoros\ResponseFactory;
26
use Laminas\Diactoros\ServerRequestFactory;
27
use Laminas\Diactoros\Response;
28
use Laminas\Diactoros\Response\RedirectResponse;
29
use Laminas\Diactoros\Uri;
30
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
31
use Laminas\I18n\Translator\Translator;
32
use Psr\Http\Server\RequestHandlerInterface;
33
34
class Application
35
{
36
    /** @var Container $registry */
37
    private $treasureChest;
38
39
    /** @var string $configFolder */
40
    private $configFolder = 'config';
41
42
    /** @var string $environment */
43
    private $environment = 'production';
44
45
    /**
46
     *  There be nay feckin wi' constructors on board this ship
47
     *  There be nay copyin' o' th'ship either
48
     *  This ship is a singleton!
49
     */
50
    private function __construct(){}
51
52
    private function __clone(){}
53
54
55
    /**
56
     *  Ahoy! There nay be boardin without yer configuration
57
     *
58
     * @param array $config
59
     * @return Application
60
     */
61 10
    public static function ahoy()
62
    {
63 10
        static $inst = null;
64 10
        if ($inst === null) {
65 1
            $inst = new Application();
66 1
            $session = SessionManager::getInstance();
67 1
            SessionManager::sessionStart('app');
68 1
            $inst->treasureChest = new Container();
69 1
            $inst->treasureChest[SessionManager::class] = $session;
70 1
            $env = getenv('APPLICATION_ENV');
71 1
            if ($env) {
72 1
                $inst->setEnvironment($env);
73
            }
74
        }
75 10
        return $inst;
76
    }
77
78
    /**
79
     *  Use this to bootstrap Bone without dispatching any request
80
     *  i.e. for when using the framework in a CLI application
81
     */
82 8
    public function bootstrap(): Container
83
    {
84 8
        $env = new Environment($_SERVER);
85 8
        $router = $this->treasureChest[Router::class] = new Router();
86
87
        $config = $env->fetchConfig($this->configFolder, $this->environment);
88
        $config[Environment::class] = $env;
89
        $config[SiteConfig::class] = new SiteConfig($config, $env);
90
91
        $package = new ApplicationPackage($config, $router);
92
        $package->addToContainer($this->treasureChest);
93
94
        return $this->treasureChest;
95
    }
96
97
98
    /**
99
     *
100
     * T' the high seas! Garrr!
101
     *
102
     * @return bool
103
     * @throws \Exception
104
     */
105 8
    public function setSail()
106
    {
107
        // load in the config and set up the dependency injection container
108 8
        $this->bootstrap();
109
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
110
        /** @var RequestHandlerInterface $stack */
111
        $stack = $this->treasureChest->get(Stack::class);
112
113
        try {
114
            $response = $stack->handle($request);
115
        } catch (NotFoundException $e) {
116
            $response = new RedirectResponse($e->getMessage());
117
            if ($e->getRequest()->getMethod() !== 'GET') {
118
                $response = $stack->handle($request);
119
            }
120
        }
121
122
        (new SapiEmitter)->emit($response);
123
124
        return true;
125
    }
126
127
    /**
128
     * @return Container
129
     */
130 1
    public function getContainer(): Container
131
    {
132 1
        return $this->treasureChest;
133
    }
134
135
    /**
136
     * @param string $configFolder
137
     */
138 8
    public function setConfigFolder(string $configFolder)
139
    {
140 8
        $this->configFolder = $configFolder;
141 8
    }
142
143
    /**
144
     * @param string $environment
145
     */
146 1
    public function setEnvironment(string $environment)
147
    {
148 1
        $this->environment = $environment;
149
    }
150
}