Completed
Push — 2.1 ( 8c7631...c23fe5 )
by David
11s
created

ResetPatchesCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Mouf\Utils\Patcher\Commands;
3
4
use Mouf\Utils\Patcher\PatchInterface;
5
use Mouf\Utils\Patcher\PatchService;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Command\Command;
12
13
/**
14
 * Command to reset and reapply all patches
15
 */
16
class ResetPatchesCommand extends Command
17
{
18
    /**
19
     * @var PatchService
20
     */
21
    private $patchService;
22
23
    public function __construct(PatchService $patchService)
24
    {
25
        $this->patchService = $patchService;
26
        parent::__construct();
27
    }
28
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
34
    {
35
        $this
36
        ->setName('patches:reset')
37
        ->setDescription('Reset database and reapply all patches.')
38
        ->setDefinition(array(
39
40
        ))
41
        ->setHelp(<<<EOT
42
Reset the database and reapplies all pending patches. You can select the type of patches to be applied using the options. Default patches are always applied.
43
44
Use patches:apply-all if you want to apply remaining patches without resetting the database.
45
EOT
46
        );
47
48
        foreach ($this->patchService->getTypes() as $type) {
49
            if ($type->getName() !== '') {
0 ignored issues
show
Bug introduced by
Consider using $type->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
50
                $this->addOption($type->getName(), null, InputOption::VALUE_NONE, 'Applies patches of type "'.$type->getName().'". '.$type->getDescription());
0 ignored issues
show
Bug introduced by
Consider using $type->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
51
            }
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $this->patchService->reset();
61
62
        $patchesArray = $this->patchService->getView();
63
64
        $count = 0;
65
        try {
66
            foreach ($patchesArray as $patch) {
67
                $this->patchService->apply($patch['uniqueName']);
68
                $count++;
69
            }
70
        } catch (\Exception $e) {
71
            $output->writeln(sprintf(
72
                    'An error occurred while applying patch <info>%s</info>: <error>%s</error>', $patch['uniqueName'], $e->getMessage()
73
                ));
74
            throw $e;
75
        }
76
77
        if ($count) {
78
            $output->writeln(sprintf(
79
                    'Database has been reset, <info>%d</info> patches successfully applied', $count
80
                ));
81
        } else {
82
            $output->writeln('<info>Database has been reset, no patches to apply</info>');
83
        }
84
    }
85
}
86