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

ScannerService::findAll()   D

Complexity

Conditions 9
Paths 439

Size

Total Lines 88
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 35
nc 439
nop 0
dl 0
loc 88
ccs 0
cts 45
cp 0
crap 90
rs 4.668
c 0
b 0
f 0

How to fix   Long Method   

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\Core\Api\ScannerService
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\Core\Api;
22
23
use AppserverIo\Configuration\ConfigurationException;
24
use AppserverIo\Appserver\Core\Api\Node\CronNode;
25
use AppserverIo\Appserver\Core\Utilities\AppEnvironmentHelper;
26
use AppserverIo\Appserver\Core\Utilities\SystemPropertyKeys;
27
28
/**
29
 * A service that handles scanner configuration data.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2015 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/appserver
35
 * @link      http://www.appserver.io
36
 */
37
class ScannerService extends AbstractFileOperationService
38
{
39
40
    /**
41
     * Returns the node with the passed UUID.
42
     *
43
     * @param integer $uuid UUID of the node to return
44
     *
45
     * @return \AppserverIo\Configuration\Interfaces\NodeInterface The node with the UUID passed as parameter
46
     */
47
    public function load($uuid)
48
    {
49
        // not implemented yet
50
    }
51
52
    /**
53
     * Initializes the available CRON configurations and returns them.
54
     *
55
     * @return array The array with the available CRON configurations
56
     */
57
    public function findAll()
58
    {
59
        try {
60
            // initialize the array with the CRON instances
61
            $cronInstances = array();
62
63
            // load the service necessary to validate CRON configuration files
64
            /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
65
            $configurationService = $this->newService('AppserverIo\Appserver\Core\Api\ConfigurationService');
66
67
            // load the base CRON configuration file
68
            $baseCronPath = $this->getConfdDir('cron.xml');
69
70
            // we will need to test our CRON configuration files
71
            $configurationService->validateFile($baseCronPath, null);
72
73
            // validate the base CRON file and load it as default if validation succeeds
74
            $cronInstance = new CronNode();
75
            $cronInstance->initFromFile($baseCronPath);
0 ignored issues
show
Bug introduced by
$baseCronPath of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $filename of AppserverIo\Description\...actNode::initFromFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            $cronInstance->initFromFile(/** @scrutinizer ignore-type */ $baseCronPath);
Loading history...
76
77
            // iterate over all jobs to configure the directory where they has to be executed
78
            /** @var \AppserverIo\Appserver\Core\Api\Node\JobNodeInterface $jobNode */
79
            foreach ($cronInstance->getJobs() as $job) {
80
                // load the execution information
81
                $execute = $job->getExecute();
82
                // query whether or not a base directory has been specified
83
                if ($execute && $execute->getDirectory() == null) {
84
                    // set the directory where the cron.xml file located as base directory, if not
85
                    $execute->setDirectory(dirname($baseCronPath));
86
                }
87
            }
88
89
            // add the default CRON configuration
90
            $cronInstances[] = $cronInstance;
91
92
            // iterate over the configured containers
93
            /** @var \AppserverIo\Psr\ApplicationServer\Configuration\ContainerConfigurationInterface $containerNode */
94
            foreach ($this->getSystemConfiguration()->getContainers() as $containerNode) {
95
                // iterate over all applications and create the CRON configuration
96
                foreach (glob($this->getWebappsDir($containerNode) . '/*', GLOB_ONLYDIR) as $webappPath) {
97
                    // iterate through all CRON configurations (cron.xml), validate and merge them
98
                    foreach ($this->globDir(AppEnvironmentHelper::getEnvironmentAwareGlobPattern($webappPath, 'META-INF/cron')) as $cronFile) {
99
                        try {
100
                            // validate the file, but skip it if validation fails
101
                            $configurationService->validateFile($cronFile, null);
102
103
                            // load the system properties
104
                            $properties = $this->getSystemProperties($containerNode);
105
106
                            // append the application specific properties
107
                            $properties->add(SystemPropertyKeys::WEBAPP, $webappPath);
108
                            $properties->add(SystemPropertyKeys::WEBAPP_NAME, basename($webappPath));
109
110
                            // create a new CRON node instance and replace the properties
111
                            $cronInstance = new CronNode();
112
                            $cronInstance->initFromFile($cronFile);
113
                            $cronInstance->replaceProperties($properties);
114
115
                            // append it to the other CRON configurations
116
                            $cronInstances[] = $cronInstance;
117
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
118
                        } catch (ConfigurationException $ce) {
119
                            // load the logger and log the XML validation errors
120
                            $systemLogger = $this->getInitialContext()->getSystemLogger();
121
                            $systemLogger->error($ce->__toString());
122
123
                            // additionally log a message that CRON configuration will be missing
124
                            $systemLogger->critical(
125
                                sprintf('Will skip app specific CRON configuration %s, configuration might be faulty.', $cronFile)
126
                            );
127
                        }
128
                    }
129
                }
130
            }
131
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
132
        } catch (ConfigurationException $ce) {
133
            // load the logger and log the XML validation errors
134
            $systemLogger = $this->getInitialContext()->getSystemLogger();
135
            $systemLogger->error($ce->__toString());
136
137
            // additionally log a message that DS will be missing
138
            $systemLogger->critical(
139
                sprintf('Problems validating base CRON file %s, this might affect app configurations badly.', $baseCronPath)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $baseCronPath does not seem to be defined for all execution paths leading up to this point.
Loading history...
140
            );
141
        }
142
143
        // return the array with the CRON instances
144
        return $cronInstances;
145
    }
146
147
    /**
148
     * Resolves the passed path. If the passed path is NULL, the webapp path is
149
     * used, if the passed path is relative, the webapp path is prepended.
150
     *
151
     * @param string $webappPath The absolute path to the webapp
152
     * @param string $path       The path to be resolved
153
     *
154
     * @return string The resolved path
155
     */
156
    protected function resolvePath($webappPath, $path = null)
157
    {
158
159
        // query whether or not a base directory has been specified
160
        if ($path == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $path of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
161
            // set the directory where the cron.xml file located as base directory
162
            $path = $webappPath;
163
164
        // query whether or not we found an absolute path or not
165
        } elseif ($path != null) {
166
            if (strpos($path, '/') > 0) {
167
                $path = sprintf('%s/%s', $webappPath, $path);
168
            }
169
        }
170
171
        // return the resolved path
172
        return $path;
173
    }
174
}
175