Completed
Pull Request — master (#563)
by Richard
08:33
created

CiInstallCommand::execute()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 37
nc 7
nop 2
dl 0
loc 51
rs 6.9743
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace XoopsConsole\Commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
use Xoops\Core\XoopsTpl;
11
12
class CiInstallCommand extends Command
13
{
14
    /**
15
     * establish the command configuration
16
     * @return void
17
     */
18
    protected function configure()
19
    {
20
        $this->setName("ci-install")
21
            ->setDescription("Install a minimal XOOPS for CI processes")
22
            ->setDefinition(array())
23
            ->setHelp(<<<EOT
24
The <info>ci-install</info> command installs a default XOOPS system for use in the
25
travis-ci continuous integration environment. This command expects on an
26
appropriate mainfile.php to have been previously created, possibly using the
27
<info>ci-bootstrap</info> command (only available if mainfile.php does not exist.)
28
EOT
29
             );
30
    }
31
32
    /**
33
     * execute the command
34
     *
35
     * @param InputInterface  $input  input handler
36
     * @param OutputInterface $output output handler
37
     * @return void
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        // install the 'system' module
42
        $xoops = \Xoops::getInstance();
43
        $module = 'system';
44
        $output->writeln(sprintf('Installing %s', $module));
45
        if (false !== $xoops->getModuleByDirname($module)) {
0 ignored issues
show
introduced by
The condition false !== $xoops->getModuleByDirname($module) can never be false.
Loading history...
46
            $output->writeln(sprintf('<error>%s module is already installed!</error>', $module));
47
            return;
48
        }
49
        $xoops->setTpl(new XoopsTpl());
50
        \XoopsLoad::load('module', 'system');
51
        $sysmod = new \SystemModule();
52
        $result = $sysmod->install($module);
53
        foreach ($sysmod->trace as $message) {
54
            if (is_array($message)) {
55
                foreach ($message as $subMessage) {
56
                    if (!is_array($subMessage)) {
57
                        $output->writeln(strip_tags($subMessage));
58
                    }
59
                }
60
            } else {
61
                $output->writeln(strip_tags($message));
62
            }
63
        }
64
        if ($result===false) {
65
            $output->writeln(sprintf('<error>Install of %s module failed!</error>', $module));
66
        } else {
67
            $output->writeln(sprintf('<info>Install of %s module completed.</info>', $module));
68
        }
69
        $xoops->cache()->delete('system');
70
71
        // add an admin user
72
        $adminname = 'admin';
73
        $adminpass = password_hash($adminname, PASSWORD_DEFAULT); // user: admin pass: admin
74
        $regdate = time();
75
        $result = $xoops->db()->insertPrefix(
76
            'system_user',
77
            array(
78
            //  'uid'             => 1,             // mediumint(8) unsigned NOT NULL auto_increment,
79
                'uname'           => $adminname,    // varchar(25) NOT NULL default '',
80
                'email'           => 'nobody@localhost',    // varchar(60) NOT NULL default '',
81
                'user_regdate'    => $regdate,      // int(10) unsigned NOT NULL default '0',
82
                'user_viewemail'  => 1,             // tinyint(1) unsigned NOT NULL default '0',
83
                'pass'            => $adminpass,    // varchar(255) NOT NULL default '',
84
                'rank'            => 7,             // smallint(5) unsigned NOT NULL default '0',
85
                'level'           => 5,             // tinyint(3) unsigned NOT NULL default '1',
86
                'last_login'      => $regdate,      // int(10) unsigned NOT NULL default '0',
87
            )
88
        );
89
        $output->writeln(sprintf('<info>Inserted %d user.</info>', $result));
90
    }
91
}
92