Completed
Pull Request — master (#1105)
by Tim
42:55
created

ApplicationFactory::visit()   B

Complexity

Conditions 10
Paths 55

Size

Total Lines 110
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 52
nc 55
nop 2
dl 0
loc 110
ccs 0
cts 62
cp 0
crap 110
rs 7.1806
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * AppserverIo\Appserver\Application\ApplicationFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\Application;
22
23
use AppserverIo\Storage\GenericStackable;
24
use AppserverIo\Appserver\Core\LoggerFactory;
25
use AppserverIo\Appserver\Core\Api\Node\ContextNode;
26
use AppserverIo\Appserver\Core\Utilities\PermissionHelper;
27
use AppserverIo\Psr\ApplicationServer\ContainerInterface;
28
29
/**
30
 * Application factory implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2015 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/appserver-io/appserver
36
 * @link      http://www.appserver.io
37
 */
38
class ApplicationFactory
39
{
40
41
    /**
42
     * Visitor method that registers the application in the container.
43
     *
44
     * @param \AppserverIo\Psr\ApplicationServer\ContainerInterface $container The container instance bind the application to
45
     * @param \AppserverIo\Appserver\Core\Api\Node\ContextNode      $context   The application configuration
46
     *
47
     * @return void
48
     */
49
    public static function visit(ContainerInterface $container, ContextNode $context)
50
    {
51
52
        // load the applications base directory
53
        $webappPath = $context->getWebappPath();
54
55
        // declare META-INF and WEB-INF directory
56
        $webInfDir = $webappPath . DIRECTORY_SEPARATOR . 'WEB-INF';
57
        $metaInfDir = $webappPath . DIRECTORY_SEPARATOR . 'META-INF';
58
59
        // check if we've a directory containing a valid application,
60
        // at least a WEB-INF or META-INF folder has to be available
61
        if (!is_dir($webInfDir) && !is_dir($metaInfDir)) {
62
            return;
63
        }
64
65
        // load the naming directory + initial context
66
        $initialContext = $container->getInitialContext();
67
        $namingDirectory = $container->getNamingDirectory();
68
69
        // load the application service
70
        $appService = $container->newService('AppserverIo\Appserver\Core\Api\AppService');
71
72
        // load the application type
73
        $contextType = $context->getType();
74
        $containerName = $container->getName();
75
        $environmentName = $context->getEnvironmentName();
76
        $applicationName = $context->getName();
77
        $containerRunlevel = $container->getRunlevel();
78
79
        // create a new application instance
80
        /** @var \AppserverIo\Appserver\Application\Application $application */
81
        $application = new $contextType();
82
83
        // initialize the storage for managers, virtual hosts an class loaders
84
        $loggers = new GenericStackable();
85
        $managers = new GenericStackable();
86
        $provisioners = new GenericStackable();
87
        $classLoaders = new GenericStackable();
88
89
        // initialize the generic instances and information
90
        $application->injectLoggers($loggers);
91
        $application->injectManagers($managers);
92
        $application->injectName($applicationName);
93
        $application->injectEnvironmentName($environmentName);
94
        $application->injectProvisioners($provisioners);
95
        $application->injectClassLoaders($classLoaders);
96
        $application->injectContainerName($containerName);
97
        $application->injectInitialContext($initialContext);
98
        $application->injectNamingDirectory($namingDirectory);
99
        $application->injectContainerRunlevel($containerRunlevel);
100
101
        // prepare the application instance
102
        $application->prepare($container, $context);
103
104
        // create the applications temporary folders and cleans the folders up
105
        /** @var \AppserverIo\Appserver\Core\Api\AppService $appService */
106
        PermissionHelper::sudo(array($appService, 'createTmpFolders'), array($application));
107
        $appService->cleanUpFolders($application);
108
109
        // add the configured loggers
110
        /** @var \AppserverIo\Appserver\Core\Api\Node\LoggerNode $loggerNode */
111
        foreach ($context->getLoggers() as $loggerNode) {
112
            $application->addLogger(LoggerFactory::factory($loggerNode), $loggerNode);
113
        }
114
115
        // add the configured class loaders
116
        /** @var \AppserverIo\Appserver\Core\Api\Node\ClassLoaderNode $classLoader */
117
        foreach ($context->getClassLoaders() as $classLoader) {
118
            /** @var \AppserverIo\Appserver\Core\Interfaces\ClassLoaderFactoryInterface $classLoaderFactory */
119
            if ($classLoaderFactory = $classLoader->getFactory()) {
120
                // use the factory if available
121
                $classLoaderFactory::visit($application, $classLoader);
122
            } else {
123
                // if not, try to instanciate the class loader directly
124
                $classLoaderType = $classLoader->getType();
125
                $application->addClassLoader(new $classLoaderType($classLoader), $classLoader);
126
            }
127
        }
128
129
        // add the configured managers
130
        /** @var \AppserverIo\Appserver\Core\Api\Node\ManagerNode $manager */
131
        foreach ($context->getManagers() as $manager) {
132
            /** @var \AppserverIo\Appserver\Core\Interfaces\ManagerFactoryInterface $managerFactory */
133
            if ($managerFactory = $manager->getFactory()) {
134
                // use the factory if available
135
                $managerFactory::visit($application, $manager);
136
            } else {
137
                // if not, try to instanciate the manager directly
138
                $managerType = $manager->getType();
139
                $application->addManager(new $managerType($manager), $manager);
140
            }
141
        }
142
143
        // add the configured provisioners
144
        /** @var \AppserverIo\Appserver\Core\Api\Node\ProvisionerNode $provisioner */
145
        foreach ($context->getProvisioners() as $provisioner) {
146
            /** @var \AppserverIo\Appserver\Provisioning\StandardProvisionerFactory $provisionerFactory */
147
            if ($provisionerFactory = $provisioner->getFactory()) {
148
                // use the factory if available
149
                $provisionerFactory::visit($application, $provisioner);
150
            } else {
151
                // if not, try to instanciate the provisioner directly
152
                $provisionerType = $provisioner->getType();
153
                $application->addProvisioner(new $provisionerType($provisioner), $provisioner);
154
            }
155
        }
156
157
        // add the application to the container
158
        $container->addApplication($application);
159
    }
160
}
161