Completed
Push — master ( 3260ca...0c0ec5 )
by Christian
02:12
created

CheckRootUser::setApplication()   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\Console\EventSubscriber;
4
5
use N98\Magento\Application;
6
use N98\Magento\Application\ApplicationAwareInterface;
7
use N98\Magento\Application\Console\Event;
8
use N98\Magento\Application\Console\Events;
9
use N98\Util\OperatingSystem;
10
use Symfony\Component\Console\Event\ConsoleEvent;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14
15
class CheckRootUser implements EventSubscriberInterface, ApplicationAwareInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    const WARNING_ROOT_USER = '<error>It\'s not recommended to run n98-magerun as root user</error>';
21
22
    /**
23
     * @var Application
24
     */
25
    private $application;
26
27
    /**
28
     * Returns an array of event names this subscriber wants to listen to.
29
     *
30
     * @return array The event names to listen to
31
     *
32
     * @api
33
     */
34
    public static function getSubscribedEvents()
35
    {
36
        return [
37
            Events::RUN_BEFORE => 'checkRunningAsRootUser',
38
        ];
39
    }
40
41
    /**
42
     * Display a warning if a running n98-magerun as root user
43
     *
44
     * @param ConsoleEvent $event
45
     * @return void
46
     */
47
    public function checkRunningAsRootUser(ConsoleEvent $event)
48
    {
49
        $skipRootCheck = $this->_isSkipRootCheck($event->getInput());
50
        if ($skipRootCheck) {
51
            $this->debugWriteln($event, "Skipping root-check by '--skip-root-check' option ");
0 ignored issues
show
Documentation introduced by
$event is of type object<Symfony\Component...ole\Event\ConsoleEvent>, but the function expects a object<N98\Magento\Application\Console\Event>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
            return;
53
        }
54
55
        $config = $this->application->getConfig();
56
        if (!$config['application']['check-root-user']) {
57
            return;
58
        }
59
60
        if (OperatingSystem::isRoot()) {
61
            $output = $event->getOutput();
62
            $output->writeln([
0 ignored issues
show
Documentation introduced by
array('', self::WARNING_ROOT_USER, '') is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
                '',
64
                self::WARNING_ROOT_USER,
65
                '',
66
            ]);
67
        }
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    protected function _isSkipRootCheck(InputInterface $input)
74
    {
75
        return $input->hasParameterOption('--skip-root-check');
76
    }
77
78
    /**
79
     * @param Event $event
80
     * @param string $message
81
     */
82
    private function debugWriteln(Event $event, $message)
83
    {
84
        $output = $event->getOutput();
85
        if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
86
            $output->writeln($message);
87
        }
88
    }
89
90
    public function setApplication($application)
91
    {
92
        $this->application = $application;
93
    }
94
}
95