Completed
Pull Request — develop (#501)
by ANTHONIUS
56:43
created

Yawik   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 132
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 15 8
A getRequiredModules() 0 24 1
A generateModuleConfiguration() 0 7 1
B initApplication() 0 31 8
B runApplication() 0 28 6
1
<?php
2
3
/**
4
 * YAWIK
5
 *
6
 * @copyright (c) 2013 - 2016 Cross Solution <http://cross-solution.de>
7
 * @license MIT
8
 */
9
10
11
namespace Core;
12
13
use Symfony\Component\Dotenv\Dotenv;
14
use Zend\Mvc\Application as ZendApplication;
15
use Zend\Stdlib\ArrayUtils;
16
17
/**
18
 * Utility class
19
 *
20
 * @package Core
21
 * @since 0.32
22
 */
23
class Yawik
0 ignored issues
show
Coding Style introduced by
Yawik does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
24
{
25
    public static $VERSION;
0 ignored issues
show
Coding Style introduced by
$VERSION does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
26
27
    public static function init()
28
    {
29
        $env = getcwd().'/.env';
30
        if (!is_file($env)) {
31
            $env = getcwd().'/.env.dist';
32
        }
33
        if (is_file($env)) {
34
            $dotenv = new Dotenv();
35
            $dotenv->load($env);
36
        }
37
        $isVendor = strpos(__FILE__, 'modules')!==false || strpos(__FILE__, 'vendor') !== false;
38
        $version = getenv('TRAVIS') || $isVendor ? "undefined":exec('git describe');
39
        $branch = getenv('TRAVIS') || $isVendor ? "undefined":exec('git rev-parse --abbrev-ref HEAD', $output, $retVal);
40
        static::$VERSION = $version.'['.$branch.']';
41
    }
42
43
    /**
44
     * Get required modules for Yawik
45
     *
46
     * @return array
47
     */
48
    public static function getRequiredModules()
49
    {
50
        return array(
51
            'Zend\ServiceManager\Di',
52
            'Zend\Session',
53
            'Zend\Router',
54
            'Zend\Navigation',
55
            'Zend\I18n',
56
            'Zend\Filter',
57
            'Zend\InputFilter',
58
            'Zend\Form',
59
            'Zend\Validator',
60
            'Zend\Log',
61
            'Zend\Mvc\Plugin\Prg',
62
            'Zend\Mvc\Plugin\Identity',
63
            'Zend\Mvc\Plugin\FlashMessenger',
64
            'Zend\Mvc\I18n',
65
            'Zend\Mvc\Console',
66
            'Zend\Hydrator',
67
            'Zend\Serializer',
68
            'DoctrineModule',
69
            'DoctrineMongoODMModule',
70
        );
71
    }
72
73
    /**
74
     * Generate modules to be loaded for Yawik application
75
     *
76
     * @param array $loadModules
77
     * @return array
78
     */
79
    public static function generateModuleConfiguration($loadModules=[])
80
    {
81
        return ArrayUtils::merge(
82
            static::getRequiredModules(),
83
            $loadModules
84
        );
85
    }
86
87
    /**
88
     * Run application
89
     * @param mixed|array $appConfig
90
     *
91
     * @param bool $run
0 ignored issues
show
Bug introduced by
There is no parameter named $run. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
92
     * @return bool|ZendApplication
0 ignored issues
show
Documentation introduced by
Should the return type not be null|ZendApplication?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
93
     */
94
    public static function initApplication($appConfig = null)
95
    {
96
        static::init();
97
        if (is_string($appConfig) && is_file($appConfig)) {
98
            $appConfig = include $appConfig;
99
        }
100
101
        if (empty($appConfig)) {
102
            // Retrieve configuration
103
            $file = null;
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
104
            if (is_file($test = getcwd().'/test/config/config.php')) {
105
                $file = $test;
106
            } elseif (is_file($test = getcwd(). '/config/config.php')) {
107
                $file = $test;
108
            } elseif (is_file($test = __DIR__.'/../config/config.php')) {
109
                $file = $test;
110
            } elseif (is_file($test = __DIR__.'/../../../../config/config.php')) {
111
                $file = $test;
112
            } else {
113
                fwrite(
114
                    STDERR,
115
                    'You must set up the project dependencies, run the following commands:'.PHP_EOL.
116
                    'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
117
                    'php composer.phar install'.PHP_EOL
118
                );
119
                exit(1);
120
            }
121
            $appConfig = include $file;
122
        }
123
        return ZendApplication::init($appConfig);
124
    }
125
126
    public static function runApplication($appConfig=null)
127
    {
128
        ini_set('display_errors', true);
129
        ini_set('error_reporting', E_ALL | E_STRICT);
130
131
        date_default_timezone_set('Europe/Berlin');
132
133
        if (!version_compare(PHP_VERSION, '5.6.0', 'ge')) {
134
            echo sprintf('<p>Sorry, YAWIK requires at least PHP 5.6.0 to run, but this server currently provides PHP %s</p>', PHP_VERSION);
135
            echo '<p>Please ask your servers\' administrator to install the proper PHP version.</p>';
136
            exit;
137
        }
138
139
        if (php_sapi_name() == 'cli-server') {
140
            $parseUrl = parse_url(substr($_SERVER["REQUEST_URI"], 1));
141
            $route = isset($parseUrl['path']) ? $parseUrl['path']:null;
142
            if (is_file(__DIR__ . '/' . $route)) {
143
                if (substr($route, -4) == ".php") {
144
                    require __DIR__ . '/' . $route;     // Include requested script files
145
                    exit;
146
                }
147
                return false;           // Serve file as is
148
            } else {                    // Fallback to index.php
149
                $_GET["q"] = $route;    // Try to emulate the behaviour of a .htaccess here.
150
            }
151
        }
152
        return static::initApplication($appConfig)->run();
153
    }
154
}
155
156
Yawik::init();
157