Bootstrap::setupFolder()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 8
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
crap 5.0342
rs 9.6111
1
<?php
2
/*
3
 * @author  PhileCMS
4
 * @link    https://philecms.github.io
5
 * @license http://opensource.org/licenses/MIT
6
 */
7
8
namespace Phile\Core;
9
10
use Closure;
11
use Phile\Core\Config;
12
use Phile\Core\Event;
13
use Phile\Core\ServiceLocator;
14
use Phile\Exception\PluginException;
15
use Phile\Plugin\PluginRepository;
16
use Phile\ServiceLocator\ErrorHandlerInterface;
17
18
/**
19
 * Bootstrap class
20
 */
21
class Bootstrap
22
{
23
    /**
24
     * Loads $file into $configuration.
25
     *
26
     * @param string $file Path to config file to load
27
     * @param Config $config Phile configuration
28
     * @return void
29
     */
30 34
    public static function loadConfiguration($file, Config $config)
31
    {
32
        // function isolates context of loaded files
33 34
        $load = function (string $file): array {
34 34
            return include $file;
35
        };
36 34
        $config->merge($load($file));
37
    }
38
39
    /**
40
     * Creates and protects folder and path $directory.
41
     *
42
     * @param string $directory Path to $directory
43
     * @param Config $config Phile configuration
44
     * @return void
45
     */
46 1
    public static function setupFolder(string $directory, Config $config)
47
    {
48 1
        if (empty($directory) || strpos($directory, $config->get('root_dir')) !== 0) {
0 ignored issues
show
Bug introduced by
It seems like $config->get('root_dir') can also be of type null; however, parameter $needle of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        if (empty($directory) || strpos($directory, /** @scrutinizer ignore-type */ $config->get('root_dir')) !== 0) {
Loading history...
49
            return;
50
        }
51 1
        if (!file_exists($directory)) {
52 1
            mkdir($directory, 0775, true);
53
        }
54 1
        $htaccessPath = "$directory.htaccess";
55 1
        if (!file_exists($htaccessPath)) {
56 1
            $htaccessContent = "order deny,allow\ndeny from all\nallow from 127.0.0.1";
57 1
            file_put_contents($htaccessPath, $htaccessContent);
58
        }
59
    }
60
61
    /**
62
     * Initializes error handling
63
     *
64
     * @param Config $config Phile configuration
65
     * @return void
66
     */
67 1
    public static function setupErrorHandler(Config $config)
68
    {
69 1
        $cliMode = $config->get('phile_cli_mode');
70 1
        if ($cliMode || !ServiceLocator::hasService('Phile_ErrorHandler')) {
71
            return;
72
        }
73
        /** @var \Phile\ServiceLocator\ErrorHandlerInterface */
74 1
        $errorHandler = ServiceLocator::getService('Phile_ErrorHandler');
75 1
        set_error_handler(Closure::fromCallable([$errorHandler, 'handleError']));
76 1
        set_exception_handler(Closure::fromCallable([$errorHandler, 'handleException']));
77 1
        register_shutdown_function(Closure::fromCallable([$errorHandler, 'handleShutdown']));
78
    }
79
}
80