Completed
Pull Request — 2.1 (#7)
by David
02:07
created

AbstractApplyAllCommand::applyAll()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 9
nop 2
1
<?php
2
3
4
namespace Mouf\Utils\Patcher\Commands;
5
6
7
use Mouf\Utils\Patcher\PatchService;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class AbstractApplyAllCommand extends Command
14
{
15
    /**
16
     * @var PatchService
17
     */
18
    protected $patchService;
19
20
    public function __construct(PatchService $patchService)
21
    {
22
        $this->patchService = $patchService;
23
        parent::__construct();
24
    }
25
26
    protected function registerOptions(): void
27
    {
28
        foreach ($this->patchService->getTypes() as $type) {
29
            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...
30
                $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...
31
            }
32
        }
33
    }
34
35
    protected function applyAll(InputInterface $input, OutputInterface $output)
36
    {
37
        $types = [];
38
        foreach ($this->patchService->getTypes() as $type) {
39
            if ($type->getName() !== '' && $input->getOption($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...
40
                $types[] = $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...
41
            }
42
        }
43
44
        try {
45
46
            [
47
                'applied' => $appliedPatchArray,
0 ignored issues
show
Bug introduced by
The variable $appliedPatchArray does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
48
                'skipped' => $skippedPatchArray
0 ignored issues
show
Bug introduced by
The variable $skippedPatchArray does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
            ] = $this->patchService->applyAll($types);
50
51
        } catch (\Exception $e) {
52
            $output->writeln(sprintf(
53
                'An error occurred while applying patch: <error>%s</error>', $e->getMessage()
54
            ));
55
            throw $e;
56
        }
57
58
        $msg = $this->getNotificationMessage($appliedPatchArray, $skippedPatchArray);
59
        if ($msg) {
60
            $output->writeln($msg);
61
        } else {
62
            $output->writeln('<info>No patches to apply</info>');
63
        }
64
    }
65
66
    private function getNotificationMessage(array $appliedPatchArray, array $skippedPatchArray): string
67
    {
68
        $nbPatchesApplied = array_sum($appliedPatchArray);
69
        $nbPatchesSkipped = array_sum($skippedPatchArray);
70
        $msg = '';
71
        if ($nbPatchesApplied !== 0) {
72
            $patchArr = [];
73
            foreach ($appliedPatchArray as $name => $number) {
74
                $name = $name ?: 'default';
75
                $patchArr[] = $name.': <info>'.$number.'</info>';
76
            }
77
78
            $msg .= sprintf('<info>%d</info> patch%s applied (%s)', $nbPatchesApplied, ($nbPatchesApplied > 1)?'es':'', implode(', ', $patchArr))."\n";
79
        }
80
        if ($nbPatchesSkipped !== 0) {
81
            $patchArr = [];
82
            foreach ($skippedPatchArray as $name => $number) {
83
                $name = $name ?: 'default';
84
                $patchArr[] = $name.': <info>'.$number.'</info>';
85
            }
86
87
            $msg .= sprintf('<info>%d</info><comment> patch%s skipped</comment> (%s)', $nbPatchesSkipped, ($nbPatchesSkipped > 1)?'es':'', implode(', ', $patchArr));
88
        }
89
90
        return $msg;
91
    }
92
}