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

CheckRootUser::_isSkipRootCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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