Completed
Push — develop ( dc3c7e...c4ddb4 )
by Tom
04:09
created

CheckRootUser::debugWriteln()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 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
42
        $skipRootCheck = $this->_isSkipRootCheck($event->getInput());
43
        if ($skipRootCheck) {
44
            $this->debugWriteln($event, "Skipping root-check by '--skip-root-check' option ");
45
            return;
46
        }
47
48
        $config = $event->getApplication()->getConfig();
49
        if (!$config['application']['check-root-user']) {
50
            return;
51
        }
52
53
        if (OperatingSystem::isRoot()) {
54
            $output = $event->getOutput();
55
            $output->writeln(array(
56
                '',
57
                self::WARNING_ROOT_USER,
58
                '',
59
            ));
60
        }
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    protected function _isSkipRootCheck(InputInterface $input)
67
    {
68
        return $input->hasParameterOption('--skip-root-check');
69
    }
70
71
    /**
72
     * @param Event $event
73
     * @param string $message
74
     */
75
    private function debugWriteln(Event $event, $message)
76
    {
77
        $output = $event->getOutput();
78
        if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
79
            $output->writeln($message);
80
        }
81
    }
82
}
83