Completed
Push — master ( cf9db0...4e5156 )
by Derek Stephen
01:40
created

Application   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 107
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A __clone() 0 1 1
A ahoy() 0 17 3
A bootstrap() 0 12 1
A setSail() 0 13 1
A getContainer() 0 4 1
A setConfigFolder() 0 4 1
A setEnvironment() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Bone;
4
5
use Barnacle\Container;
6
use Bone\Http\Middleware\Stack;
7
use Bone\Router\Router;
8
use Bone\Router\NotFoundException;
9
use Bone\Server\Environment;
10
use Bone\Server\SiteConfig;
11
use Del\SessionManager;
12
use Laminas\Diactoros\ServerRequestFactory;
13
use Laminas\Diactoros\Response\RedirectResponse;
14
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
15
use Psr\Http\Server\RequestHandlerInterface;
16
17
class Application
18
{
19
    /** @var Container $container */
20
    private $container;
21
22
    /** @var string $configFolder */
23
    private $configFolder = 'config';
24
25
    /** @var string $environment */
26
    private $environment = 'production';
27
28
    /**
29
     *  There be nay feckin wi' constructors on board this ship
30
     *  There be nay copyin' o' th'ship either
31
     *  This ship is a singleton!
32
     */
33
    private function __construct(){}
34
    private function __clone(){}
35
36
37
    /**
38
     *  Ahoy! There nay be boardin without yer configuration
39
     *
40
     * @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...
41
     * @return Application
42
     */
43 10
    public static function ahoy()
44
    {
45 10
        static $inst = null;
46 10
        if ($inst === null) {
47 1
            $inst = new Application();
48 1
            $session = SessionManager::getInstance();
49 1
            SessionManager::sessionStart('app');
50 1
            $inst->container = new Container();
51 1
            $inst->container[SessionManager::class] = $session;
52 1
            $env = getenv('APPLICATION_ENV');
53
54 1
            if ($env) {
55 1
                $inst->setEnvironment($env);
56
            }
57
        }
58 10
        return $inst;
59
    }
60
61
    /**
62
     *  Use this to bootstrap Bone without dispatching any request
63
     *  i.e. for when using the framework in a CLI application
64
     */
65 8
    public function bootstrap(): Container
66
    {
67 8
        $env = new Environment($_SERVER);
68 8
        $router = $this->container[Router::class] = new Router();
69 8
        $config = $env->fetchConfig($this->configFolder, $this->environment);
70 8
        $config[Environment::class] = $env;
71 8
        $config[SiteConfig::class] = new SiteConfig($config, $env);
72 8
        $package = new ApplicationPackage($config, $router);
73 8
        $package->addToContainer($this->container);
74
75 8
        return $this->container;
76
    }
77
78
79
    /**
80
     *
81
     * T' the high seas! Garrr!
82
     *
83
     * @return bool
84
     * @throws \Exception
85
     */
86 8
    public function setSail()
87
    {
88
        // load in the config and set up the dependency injection container
89 8
        $this->bootstrap();
90 8
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
91
        /** @var RequestHandlerInterface $stack */
92 8
        $stack = $this->container->get(Stack::class);
93 8
        $response = $stack->handle($request);
94
95 8
        (new SapiEmitter)->emit($response);
96
97 8
        return true;
98
    }
99
100
    /**
101
     * @return Container
102
     */
103 1
    public function getContainer(): Container
104
    {
105 1
        return $this->container;
106
    }
107
108
    /**
109
     * @param string $configFolder
110
     */
111 8
    public function setConfigFolder(string $configFolder)
112
    {
113 8
        $this->configFolder = $configFolder;
114 8
    }
115
116
    /**
117
     * @param string $environment
118
     */
119 1
    public function setEnvironment(string $environment)
120
    {
121 1
        $this->environment = $environment;
122
    }
123
}