Completed
Push — develop ( 9ac21c...a157fb )
by Tom
04:58
created

CheckRootUser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 0
cbo 3
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
B checkRunningAsRootUser() 0 19 5
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\Magento\Application\OptionParser;
8
use N98\Util\OperatingSystem;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
class CheckRootUser implements EventSubscriberInterface
12
{
13
    const WARNING_ROOT_USER = "<error>It's not recommended to run n98-magerun as root user</error>";
14
15
    /**
16
     * Returns an array of event names this subscriber wants to listen to.
17
     *
18
     * @return array The event names to listen to
19
     *
20
     * @api
21
     */
22
    public static function getSubscribedEvents()
23
    {
24
        return [
25
            Events::RUN_BEFORE => 'checkRunningAsRootUser'
26
        ];
27
    }
28
29
    /**
30
     * Display a warning if a running n98-magerun as root user
31
     *
32
     * @param Event $event
33
     * @return void
34
     */
35
    public function checkRunningAsRootUser(Event $event)
36
    {
37
        if ($this->_isSkipRootCheck()) {
38
            return;
39
        }
40
41
        $config = $event->getApplication()->getConfig();
42
        if (!$config['application']['check-root-user']) {
43
            return;
44
        }
45
46
        // display if current user is root
47
        if (function_exists('posix_getuid') && posix_getuid() === 0) {
48
            $output = $event->getOutput();
49
            $output->writeln('');
50
            $output->writeln(self::WARNING_ROOT_USER);
51
            $output->writeln('');
52
        }
53
    }
54
55
    protected function _isSkipRootCheck()
56
    {
57
        return OptionParser::init()->hasLongOption('skip-root-check');
58
    }
59
}
60