Completed
Push — master ( 880b7a...33b151 )
by Tom
09:34 queued 06:03
created

CheckRootUser::checkRunningAsRootUser()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace N98\Magento\Application\Console\EventSubscriber;
4
5
use N98\Magento\Application\Console\Event;
6
use N98\Magento\Application\Console\Events;
7
use N98\Util\OperatingSystem;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
class CheckRootUser implements EventSubscriberInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    const WARNING_ROOT_USER = '<error>It\'s not recommended to run n98-magerun as root user</error>';
17
18
    /**
19
     * Returns an array of event names this subscriber wants to listen to.
20
     *
21
     * @return array The event names to listen to
22
     *
23
     * @api
24
     */
25
    public static function getSubscribedEvents()
26
    {
27
        return array(
28
            Events::RUN_BEFORE => 'checkRunningAsRootUser',
29
        );
30
    }
31
32
    /**
33
     * Display a warning if a running n98-magerun as root user
34
     *
35
     * @param Event $event
36
     * @return void
37
     */
38
    public function checkRunningAsRootUser(Event $event)
39
    {
40
        if ($this->_isSkipRootCheck($event->getInput())) {
41
            return;
42
        }
43
44
        $config = $event->getApplication()->getConfig();
45
        if (!$config['application']['check-root-user']) {
46
            return;
47
        }
48
49
        if (OperatingSystem::isRoot()) {
50
            $output = $event->getOutput();
51
            $output->writeln(array(
52
                '',
53
                self::WARNING_ROOT_USER,
54
                '',
55
            ));
56
        }
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    protected function _isSkipRootCheck(InputInterface $input)
63
    {
64
        return $input->hasParameterOption('--skip-root-check');
65
    }
66
}
67