Completed
Push — master ( 239a7d...29ee36 )
by Tom
9s
created

CheckRootUser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 0
cbo 4
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
B checkRunningAsRootUser() 0 22 4
A _isSkipRootCheck() 0 4 1
A debugWriteln() 0 7 2
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\Console\Output\OutputInterface;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
class CheckRootUser implements EventSubscriberInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    const WARNING_ROOT_USER = '<error>It\'s not recommended to run n98-magerun as root user</error>';
18
19
    /**
20
     * Returns an array of event names this subscriber wants to listen to.
21
     *
22
     * @return array The event names to listen to
23
     *
24
     * @api
25
     */
26
    public static function getSubscribedEvents()
27
    {
28
        return array(
29
            Events::RUN_BEFORE => 'checkRunningAsRootUser',
30
        );
31
    }
32
33
    /**
34
     * Display a warning if a running n98-magerun as root user
35
     *
36
     * @param Event $event
37
     * @return void
38
     */
39
    public function checkRunningAsRootUser(Event $event)
40
    {
41
        $skipRootCheck = $this->_isSkipRootCheck($event->getInput());
42
        if ($skipRootCheck) {
43
            $this->debugWriteln($event, "Skipping root-check by '--skip-root-check' option ");
44
            return;
45
        }
46
47
        $config = $event->getApplication()->getConfig();
48
        if (!$config['application']['check-root-user']) {
49
            return;
50
        }
51
52
        if (OperatingSystem::isRoot()) {
53
            $output = $event->getOutput();
54
            $output->writeln(array(
55
                '',
56
                self::WARNING_ROOT_USER,
57
                '',
58
            ));
59
        }
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    protected function _isSkipRootCheck(InputInterface $input)
66
    {
67
        return $input->hasParameterOption('--skip-root-check');
68
    }
69
70
    /**
71
     * @param Event $event
72
     * @param string $message
73
     */
74
    private function debugWriteln(Event $event, $message)
75
    {
76
        $output = $event->getOutput();
77
        if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
78
            $output->writeln($message);
79
        }
80
    }
81
}
82