Completed
Push — dev-master ( 86a9d6...915e4b )
by Derek Stephen
05:17
created

Application   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 79.41%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 104
ccs 27
cts 34
cp 0.7941
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 16 3
A setConfig() 0 8 2
A setSail() 0 14 2
A getContainer() 0 4 1
A setConfigFolder() 0 4 1
A setEnvironment() 0 4 1
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Bone\Server\Environment;
6
use Pimple\Container;
7
use Zend\Diactoros\ServerRequestFactory;
8
use Zend\Diactoros\Response;
9
10
class Application
11
{
12
    /** @var Registry $registry */
13
    private $registry;
14
15
    /** @var Container $registry */
16
    private $treasureChest;
17
18
    /** @var string $configFolder */
19
    private $configFolder = 'config';
20
21
    /** @var string $environment */
22
    private $environment = 'production';
23
24
    /**
25
     *  There be nay feckin wi' constructors on board this ship
26
     *  There be nay copyin' o' th'ship either
27
     *  This ship is a singleton!
28
     */
29
    public function __construct(){}
30
    public function __clone(){}
31
32
33
    /**
34
     *  Ahoy! There nay be boardin without yer configuration
35
     *
36
     * @param array $config
37
     * @return Application
38
     */
39 2
    public static function ahoy(array $config = [])
40
    {
41 2
        static $inst = null;
42 2
        if ($inst === null)
43
        {
44 1
            $inst = new Application();
45 1
            $inst->registry = Registry::ahoy();
46 1
            $inst->treasureChest = new Container();
47 1
            $inst->setConfig($config);
48 1
            $env = getenv('APPLICATION_ENV');
49 1
            if ($env) {
50 1
                $inst->setEnvironment($env);
51
            }
52
        }
53 2
        return $inst;
54
    }
55
56
    /**
57
     * @param array $config
58
     */
59 1
    private function setConfig(array $config)
60
    {
61 1
        foreach($config as $key => $value)
62
        {
63 1
            $this->registry->set($key,$value);
64 1
            $this->treasureChest[$key] = $value;
65
        }
66 1
    }
67
68
    /**
69
     *
70
     * T' the high seas! Garrr!
71
     *
72
     * @return bool
73
     * @throws \Exception
74
     */
75 1
    public function setSail()
76
    {
77 1
        $env = new Environment($_SERVER);
78 1
        if (!count($this->registry->getAll())) {
79
            $config = $env->fetchConfig($this->configFolder, $this->environment);
80
            $this->setConfig($config);
81
        }
82 1
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
83 1
        $response = new Response();
84 1
        $dispatcher = new Dispatcher($request, $response, $env);
85 1
        $dispatcher->fireCannons();
86
87 1
        return true;
88
    }
89
90
    /**
91
     * @return Container
92
     */
93
    public function getContainer(): Container
94
    {
95
        return $this->treasureChest;
96
    }
97
98
    /**
99
     * @param string $configFolder
100
     */
101
    public function setConfigFolder(string $configFolder)
102
    {
103
        $this->configFolder = $configFolder;
104
    }
105
106
    /**
107
     * @param string $environment
108
     */
109 1
    public function setEnvironment(string $environment)
110
    {
111 1
        $this->environment = $environment;
112
    }
113
}