Completed
Push — dev-master ( 61317d...8c136d )
by Derek Stephen
29:12 queued 26:15
created

Application   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 77.5%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 100
ccs 31
cts 40
cp 0.775
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 setConfig() 0 8 2
A getContainer() 0 4 1
A setConfigFolder() 0 4 1
A ahoy() 0 16 3
A setEnvironment() 0 4 1
A setSail() 0 12 2
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 2
    {
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 1
    {
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
     *  T' the high seas! Garrr!
70
     *
71
     * @throws \Exception
72
     */
73 1
    public function setSail()
74 1
    {
75 1
        $env = new Environment($_SERVER);
76 1
        if (!count($this->registry->getAll())) {
77
            $config = $env->fetchConfig($this->configFolder, $this->environment);
78
            $this->setConfig($config);
79
        }
80 1
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
81 1
        $response = new Response();
82 1
        $dispatcher = new Dispatcher($request, $response, $env);
83 1
        $dispatcher->fireCannons();
84 1
    }
85
86
    /**
87
     * @return Container
88
     */
89
    public function getContainer(): Container
90
    {
91
        return $this->treasureChest;
92
    }
93
94
    /**
95
     * @param string $configFolder
96
     */
97
    public function setConfigFolder(string $configFolder)
98
    {
99
        $this->configFolder = $configFolder;
100
    }
101
102
    /**
103
     * @param string $environment
104
     */
105 1
    public function setEnvironment(string $environment)
106 1
    {
107 1
        $this->environment = $environment;
108
    }
109
}