Passed
Push — master ( 4b954d...b01812 )
by Gabor
03:38
created

AbstractApplication::getEnvironmentManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Application;
13
14
use InvalidArgumentException;
15
use WebHemi\Adapter\Http\HttpAdapterInterface;
16
use WebHemi\Adapter\Renderer\RendererAdapterInterface;
17
use WebHemi\Adapter\Router\RouterAdapterInterface;
18
use WebHemi\Adapter\DependencyInjection\DependencyInjectionAdapterInterface;
19
20
/**
21
 * Class AbstractApplication.
22
 */
23
abstract class AbstractApplication implements ApplicationInterface
24
{
25
    /** @var DependencyInjectionAdapterInterface */
26
    private $container;
27
28
    /**
29
     * ApplicationInterface constructor.
30
     *
31
     * @param DependencyInjectionAdapterInterface $container
32
     */
33 5
    public function __construct(DependencyInjectionAdapterInterface $container)
34
    {
35 5
        $this->container = $container;
36
37
        // Final touches.
38 5
        $this->prepareContainer();
39 5
    }
40
41
    /**
42
     * Get ready to run the application: set final data for specific services.
43
     *
44
     * @codeCoverageIgnore - Check the EnvironmentManager and Container adapter tests.
45
     */
46
    private function prepareContainer()
47
    {
48
        /** @var EnvironmentManager $environmentManager */
49
        $environmentManager = $this->container->get(EnvironmentManager::class);
50
51
        // Set proper arguments for the HTTP adapter.
52
        $this->container
53
            ->setServiceArgument(
54
                HttpAdapterInterface::class,
55
                $environmentManager->getEnvironmentData('GET')
56
            )
57
            ->setServiceArgument(
58
                HttpAdapterInterface::class,
59
                $environmentManager->getEnvironmentData('POST')
60
            )
61
            ->setServiceArgument(
62
                HttpAdapterInterface::class,
63
                $environmentManager->getEnvironmentData('SERVER')
64
            )
65
            ->setServiceArgument(
66
                HttpAdapterInterface::class,
67
                $environmentManager->getEnvironmentData('COOKIE')
68
            )
69
            ->setServiceArgument(
70
                HttpAdapterInterface::class,
71
                $environmentManager->getEnvironmentData('FILES')
72
            );
73
74
        try {
75
            $themeConfig = $environmentManager
76
                ->getApplicationTemplateSettings($environmentManager->getSelectedTheme());
77
            $themeResourcePath = $environmentManager->getResourcePath();
78
        } catch (InvalidArgumentException $e) {
79
            $themeConfig = $environmentManager->getApplicationTemplateSettings(EnvironmentManager::DEFAULT_THEME);
80
            $themeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
81
        }
82
83
        // Set proper arguments for the renderer.
84
        $this->container
85
            ->setServiceArgument(
86
                RendererAdapterInterface::class,
87
                $themeConfig
88
            )
89
            ->setServiceArgument(
90
                RendererAdapterInterface::class,
91
                $themeResourcePath
92
            )
93
            ->setServiceArgument(
94
                RendererAdapterInterface::class,
95
                $environmentManager->getSelectedApplicationUri()
96
            );
97
98
        // Set proper arguments for the router.
99
        $this->container
100
            ->setServiceArgument(
101
                RouterAdapterInterface::class,
102
                $environmentManager->getModuleRouteSettings()
103
            )
104
            ->setServiceArgument(
105
                RouterAdapterInterface::class,
106
                $environmentManager->getSelectedApplicationUri()
107
            );
108
    }
109
110
    /**
111
     * Returns the DI Adapter instance.
112
     *
113
     * @return DependencyInjectionAdapterInterface
114
     */
115 5
    final public function getContainer()
116
    {
117 5
        return $this->container;
118
    }
119
120
   /**
121
     * Runs the application. This is where the magic happens.
122
     * For example for a web application this initializes the Request and Response objects, builds the middleware
123
     * pipeline, applies the Routing and the Dispatch.
124
     *
125
     * @return void
126
     */
127
    abstract public function run();
128
}
129