Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

Magento2Initializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace N98\Magento\Application;
4
5
use Composer\Autoload\ClassLoader;
6
use Magento\Framework\Autoload\AutoloaderRegistry;
7
use N98\Magento\Framework\App\Magerun;
8
9
/**
10
 * Class Magento2Initializer
11
 * @package N98\Magento\Application
12
 */
13
class Magento2Initializer
14
{
15
    /**
16
     * @var \Composer\Autoload\ClassLoader
17
     */
18
    private $autoloader;
19
20
    /**
21
     * Magento2Initializer constructor.
22
     * @param \Composer\Autoload\ClassLoader $autoloader
23
     */
24
    public function __construct(ClassLoader $autoloader)
25
    {
26
        $this->autoloader = $autoloader;
27
    }
28
29
    /**
30
     * @param string $magentoRootFolder
31
     * @return \N98\Magento\Framework\App\Magerun
32
     * @throws \Exception
33
     */
34
    public function init($magentoRootFolder)
35
    {
36
        $this->requireOnce($magentoRootFolder . '/app/bootstrap.php');
37
38
        // Magento 2.3.1 removes phar stream wrapper.
39
        if (!in_array('phar', \stream_get_wrappers(), true)) {
40
            \stream_wrapper_restore('phar');
41
        }
42
43
        $magentoAutoloader = AutoloaderRegistry::getAutoloader();
44
45
        // Prevent an infinite loop of autoloaders
46
        if (!$magentoAutoloader instanceof AutoloaderDecorator) {
47
            AutoloaderRegistry::registerAutoloader(
48
                new AutoloaderDecorator(
49
                    $magentoAutoloader,
50
                    $this->autoloader
51
                )
52
            );
53
        }
54
55
        $params = $_SERVER;
56
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'admin';
57
        $params[\Magento\Store\Model\Store::CUSTOM_ENTRY_POINT_PARAM] = true;
58
        $params['entryPoint'] = basename(__FILE__);
59
60
        $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
61
        /** @var \Magento\Framework\App\Cron $app */
62
        $app = $bootstrap->createApplication(Magerun::class, []);
63
        /* @var $app \N98\Magento\Framework\App\Magerun */
64
        $app->launch();
0 ignored issues
show
Unused Code introduced by
The call to the method N98\Magento\Framework\App\Magerun::launch() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
65
66
        return $app;
67
    }
68
69
    /**
70
     * use require-once inside a function with it's own variable scope w/o any other variables
71
     * and $this unbound.
72
     *
73
     * @param string $path
74
     */
75 View Code Duplication
    private function requireOnce($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $requireOnce = function () {
78
            require_once func_get_arg(0);
79
        };
80
        if (50400 <= PHP_VERSION_ID) {
81
            $requireOnce->bindTo(null);
82
        }
83
84
        $requireOnce($path);
85
    }
86
}
87