ahilles107/updater

View on GitHub
src/Updater/Command/UpdateCommand.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php

/*
 * This file is part of Updater.
 *
 * (c) Paweł Mikołajczuk <mikolajczuk.private@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
 */

namespace Updater\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Updater\Updater;
use Updater\Service\PackageService;
use Updater\Service\UpdateService;
use Updater\Tools\Json\JsonManager;

class UpdateCommand extends Command
{
    /**
     * Configure console command.
     */
    public function configure()
    {
        $this
            ->setName('update')
            ->setDescription('Updates application using generated package')
            ->setDefinition(array(
                new InputArgument('target', InputArgument::REQUIRED, 'Your application directory you want to update'),
                new InputArgument('temp_dir', InputArgument::REQUIRED, 'Directory to your application temp/cache folder'),
                new InputArgument('package_dir', InputArgument::REQUIRED, 'Package real path (path to your zip package)'),
                new InputOption('rollback', null, InputOption::VALUE_NONE, 'If set, then changes will be rollbacked.'),
            ))
            ->setHelp(
<<<EOT
This command allows you to update your application by using package generated by "generate" command.

EOT
            );
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $tempDir = $input->getArgument('temp_dir');
        $targetDir = $input->getArgument('target');
        $packageDir = $input->getArgument('package_dir');
        $rollback = $input->getOption('rollback');
        $updater = new Updater();
        $jsonManager = new JsonManager($packageDir);
        $updater->setPackageService(new PackageService())
            ->setTempDir($tempDir)
            ->setWorkingDir($targetDir);

        $updateService = new UpdateService($updater);
        $packageService = $updater->getPackageService();
        $diffFile = $jsonManager->getJsonFromFile();
        $packageJson = json_decode($diffFile, true);
        $package = $packageService->fillPackage($packageJson);
        $package->setPackageDir(realpath($packageDir));
        $updateService->setPackage($package);
        if ($rollback) {
            $updateService->rollbackUpdate();
            $output->writeln('<info>Changes have been rollbacked.</info>');

            return true;
        }

        $isUpdated = $updateService->doUpdate();

        if ($isUpdated) {
            $output->writeln('<info>Your application has been successfully updated.</info>');

            return true;
        }

        $output->writeln('<error>Error occured. Your application was not updated. Rolling back changes.</error>');

        return false;
    }
}