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

CheckRootUser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
A checkRunningAsRootUser() 0 20 4
A _isSkipRootCheck() 0 4 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