UpdateCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 35
ccs 0
cts 29
cp 0
rs 8.439
c 1
b 0
f 1
cc 6
eloc 24
nc 10
nop 2
crap 42
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Command;
13
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class UpdateCommand extends AbstractCommand
19
{
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('update')
24
            ->setDescription('Updates conveyor to the latest version')
25
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force update of latest version')
26
        ;
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
    {
31
        $dsn     = 'http://conveyordeploy.com';
32
        $latest  = trim(file_get_contents(sprintf('%s/version', $dsn)));
33
        $current = trim(file_get_contents('phar://conveyor.phar/VERSION'));
34
        $update  = version_compare($current, $latest, '<');
35
36
        if ($update || $input->getOption('force')) {
37
            $remoteFile = sprintf('%s/conveyor.phar', $dsn);
38
            $localFile  = $_SERVER['argv'][0];
39
            $tempFile   = tempnam(sys_get_temp_dir(), 'conveyor') . '.phar';
40
41
            file_put_contents($tempFile, file_get_contents($remoteFile));
42
43
            try {
44
                chmod($tempFile, 0777 & ~umask());
45
                // test the phar validity
46
                $phar = new \Phar($tempFile);
47
                // free the variable to unlock the file
48
                unset($phar);
49
                rename($tempFile, $localFile);
50
51
                $output->writeln(sprintf("<info>Conveyor has been updated to %s.</info>", $latest));
52
            } catch (\Exception $e) {
53
                @unlink($tempFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
54
                if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
55
                    throw $e;
56
                }
57
                $output->writeln('<error>The download is corrupted ('.$e->getMessage().').</error>');
58
                $output->writeln('<error>Please re-run the update command to try again.</error>');
59
            }
60
        } else {
61
            $output->writeln("<info>You are using the latest version of Conveyor.</info>");
62
        }
63
    }
64
}
65