CheckRootUser::_isSkipRootCheck()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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