Completed
Pull Request — master (#7)
by Paul
01:48
created

Application   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 116
ccs 22
cts 40
cp 0.55
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 1 1
A getContainer() 0 4 1
A setConfigFolder() 0 4 1
A __construct() 0 2 1
A ahoy() 0 21 3
A bootstrap() 0 16 1
A setSail() 0 13 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\Server\Environment;
9
use Bone\Server\SiteConfig;
10
use Del\SessionManager;
11
use Laminas\Diactoros\ServerRequestFactory;
12
use Laminas\Diactoros\Response\RedirectResponse;
13
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
14
use Psr\Http\Server\RequestHandlerInterface;
15
16
class Application
17
{
18
    /** @var Container $container */
19
    private $container;
20
21
    /** @var string $configFolder */
22
    private $configFolder = 'config';
23
24
    /** @var string $environment */
25
    private $environment = 'production';
26
27
    /**
28
     *  There be nay feckin wi' constructors on board this ship
29
     *  There be nay copyin' o' th'ship either
30
     *  This ship is a singleton!
31
     */
32 1
    private function __construct(){
33 1
    }
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 11
    public static function ahoy()
44
    {
45 11
        static $inst = null;
46 11
        if ($inst === null) {
47 1
            $inst = new Application();
48
49 1
            $factories = require './factories.php';
50
            $inst->container = new Container($factories);
51
52
            $session = SessionManager::getInstance();
53
            SessionManager::sessionStart('app');
54
            $inst->container[SessionManager::class] = $session;
55
56
            $env = getenv('APPLICATION_ENV');
57
58
            if ($env) {
59
                $inst->setEnvironment($env);
60
            }
61
        }
62 10
        return $inst;
63
    }
64
65
    /**
66
     *  Use this to bootstrap Bone without dispatching any request
67
     *  i.e. for when using the framework in a CLI application
68
     */
69 9
    public function bootstrap(): Container
70
    {
71 9
        $env = new Environment($_SERVER);
72 9
        $router = $this->container[Router::class] = new Router();
73 9
        $config = $env->fetchConfig($this->configFolder, $this->environment);
74 9
        $config[Environment::class] = $env;
75 9
        $config[SiteConfig::class] = new SiteConfig($config, $env);
76
77 9
        $packageManager = $this->container->get(PackageManager::class);
0 ignored issues
show
Unused Code introduced by
$packageManager is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
79
80
        $package = new ApplicationPackage($config, $router);
81
        $package->addToContainer($this->container);
82
83
        return $this->container;
84
    }
85
86
87
    /**
88
     *
89
     * T' the high seas! Garrr!
90
     *
91
     * @return bool
92
     * @throws \Exception
93
     */
94 8
    public function setSail()
95
    {
96
        // load in the config and set up the dependency injection container
97 8
        $this->bootstrap();
98
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
99
        /** @var RequestHandlerInterface $stack */
100
        $stack = $this->container->get(Stack::class);
101
        $response = $stack->handle($request);
102
103
        (new SapiEmitter)->emit($response);
104
105
        return true;
106
    }
107
108
    /**
109
     * @return Container
110
     */
111 1
    public function getContainer(): Container
112
    {
113 1
        return $this->container;
114
    }
115
116
    /**
117
     * @param string $configFolder
118
     */
119 9
    public function setConfigFolder(string $configFolder)
120
    {
121 9
        $this->configFolder = $configFolder;
122 9
    }
123
124
    /**
125
     * @param string $environment
126
     */
127
    public function setEnvironment(string $environment)
128
    {
129
        $this->environment = $environment;
130
    }
131
}