Completed
Push — master ( ff3199...9ab95e )
by De Cramer
9s
created

UpdateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Command;
4
5
use eXpansion\Framework\Core\Helpers\ChatNotification;
6
use eXpansion\Framework\Core\Services\Console;
7
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\NullOutput;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Symfony\Component\Process\Process;
15
16
/**
17
 * Class UpdateCommand
18
 *
19
 * @author    de Cramer Oliver<[email protected]>
20
 * @copyright 2018 Oliverde8
21
 * @package eXpansion\Framework\Core\Command
22
 */
23
class UpdateCommand extends ContainerAwareCommand
24
{
25
    /** @var Factory */
26
    protected $factory;
27
28
    /** @var ChatNotification */
29
    protected $chatNotification;
30
31
    /** @var Console */
32
    protected $console;
33
34
    /**
35
     * RunCommand constructor.
36
     */
37 150
    public function __construct(Factory $factory, ChatNotification $chatNotification, Console $console)
38
    {
39 150
        parent::__construct();
40
41 150
        $this->factory = $factory;
42 150
        $this->chatNotification = $chatNotification;
43 150
        $this->console = $console;
44 150
    }
45
46
47 150
    protected function configure()
48
    {
49 150
        $this->setName('eXpansion:update')
50 150
            ->setDescription("Update eXpansion");
51
52 150
        $this->addArgument("login", InputArgument::OPTIONAL);
53 150
    }
54
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        // Initialize console output.
58
        $this->console->init(new NullOutput(), null);
59
60
        $sfs = new SymfonyStyle($input, $output);
61
        $playerLogin = $this->getPlayerLogin($input, $sfs);
62
63
        $sfs->title("Updating Composer!");
64
        $this->notifyPlayer("", "Updating Composer!", $playerLogin, $sfs);
0 ignored issues
show
Unused Code introduced by
The call to UpdateCommand::notifyPlayer() has too many arguments starting with $sfs.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
65
        $process = $this->runCommand("composer self-update", $playerLogin, $sfs);
66
67 View Code Duplication
        if ($process->isSuccessful()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
            $this->notifyPlayer("", "Composer has been updated", $playerLogin);
69
            $sfs->success('Composer has been updated');
70
        } else {
71
            $this->notifyPlayer("", "Composer update has failed", $playerLogin);
72
            $sfs->success('Composer update has failed');
73
74
            return 1;
75
        }
76
77
        $sfs->title("Updating eXpansion!");
78
        $this->notifyPlayer("", "Updating eXpansion!", $playerLogin, $sfs);
0 ignored issues
show
Unused Code introduced by
The call to UpdateCommand::notifyPlayer() has too many arguments starting with $sfs.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
79
        $this->runCommand("composer update --prefer-dist --prefer-stable --no-suggest -o", $playerLogin, $sfs);
80
81 View Code Duplication
        if ($process->isSuccessful()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
            $this->notifyPlayer("", "eXpansion has been updated. Please restart eXpansion", $playerLogin);
83
            $sfs->success('eXpansion has been updated');
84
        } else {
85
            $this->notifyPlayer("", "eXpansion update has failed", $playerLogin);
86
            $sfs->success('eXpansion update has failed');
87
88
            return 1;
89
        }
90
    }
91
92
    protected function runCommand($command, $playerLogin, SymfonyStyle $sfs)
93
    {
94
        $process = new Process($command);
95
        $process->setWorkingDirectory($this->getContainer()->getParameter('kernel.root_dir') . '/..');
96
97
        $process->run(function ($type, $buffer) use ($playerLogin, $sfs) {
98
            $this->notifyPlayer($type, $buffer, $playerLogin);
99
            $sfs->writeln($buffer);
100
        });
101
102
        return $process;
103
    }
104
105
    protected function notifyPlayer($type, $message, $playerLogin)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        if (!$playerLogin) {
108
            return;
109
        }
110
111
        try {
112
            // TODO on long term replace this with a nice window.
113
            foreach (explode("/n", $message) as $messageLine) {
114
                if (!empty($messageLine)) {
115
                    $this->chatNotification->sendMessage($messageLine, $playerLogin);
116
                }
117
            }
118
        } catch (\Exception $e) {
119
            // Ignore this is not main concern of the command. Probably connection was lost.
120
        }
121
    }
122
123
    protected function getPlayerLogin(InputInterface $input, SymfonyStyle $sfs)
124
    {
125
        if (!$input->hasArgument('login')) {
126
            return false;
127
        }
128
129
        try {
130
            $this->factory->createConnection(1);
131
            return $input->getArgument('login');
132
        } catch (\Exception $e) {
133
            $sfs->warning("Can't connect to dedicated server - Update status won't be shown ingame.");
134
        }
135
136
        return false;
137
    }
138
}
139