Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

PatcherApplyCommand::applyPatch()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 48
Code Lines 29

Duplication

Lines 11
Ratio 22.92 %

Importance

Changes 0
Metric Value
dl 11
loc 48
rs 5.5102
c 0
b 0
f 0
cc 9
eloc 29
nc 9
nop 3
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Yaml\Yaml;
19
20
class PatcherApplyCommand extends ContainerAwareCommand
21
{
22
    use PatcherConfig,
23
        PatcherLogger;
24
25
    private $dryRun = true;
26
27
    /**
28
     * Command applies patch file [targetFilePath, patchPath].
29
     *
30
     * @var string
31
     */
32
    private $command = 'patch --no-backup-if-mismatch %3$s -f %1$s < %2$s > /dev/null';
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('blast:patchs:apply')
38
            ->setDescription('Apply Patches from Librinfo on misc vendors')
39
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force patch apply');
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $this->loadConfig();
45
46
        foreach ($this->config['patches'] as $patch) {
47
            if ($patch['enabled'] === true && ($patch['patched'] == false || $input->getOption('force') === true)) {
48
                $this->applyPatch($patch['targetFile'], $patch['patchFile'], $patch['id']);
49
            }
50
        }
51
52
        $this->displayMessages($output);
53
    }
54
55
    private function applyPatch($targetFile, $patchFile, $patchId)
56
    {
57
        $targetFile = $this->config['paths']['rootDir'] . '/' . $targetFile;
58
59 View Code Duplication
        if (!file_exists($targetFile) || !file_exists($patchFile)) {
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...
60
            $this->error('Missing patches :');
61
            if (!file_exists($targetFile)) {
62
                $this->comment(' - ' . $targetFile);
63
            }
64
            if (!file_exists($patchFile)) {
65
                $this->comment(' - ' . $patchFile);
66
            }
67
68
            return;
69
        }
70
71
        $command = $this->getCommand($targetFile, $patchFile);
72
        $out = null;
73
        system($command, $out);
74
75
        if ($out == 0) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $out of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
76
            $this->dryRun = false;
77
            $command = $this->getCommand($targetFile, $patchFile);
78
            system($command, $out);
79
            $this->dryRun = true;
80
81
            if ($out != 0) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $out of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
82
                $this->error('The patch ' . $patchFile . ' cannot be applyed on file ' . $targetFile);
83
84
                return;
85
            }
86
87
            foreach ($this->config['patches'] as $key => $patch) {
88
                if ($patch['id'] == $patchId) {
89
                    $this->config['patches'][$key]['patched'] = true;
90
                }
91
            }
92
93
            file_put_contents(
94
                $this->config['paths']['configFile'],
95
                Yaml::dump(
96
                    ['patches' => $this->config['patches']]
97
                )
98
            );
99
        } else {
100
            $this->comment('The patch ' . $patchFile . ' cannot be applyed on file ' . $targetFile);
101
        }
102
    }
103
104
    private function getCommand($targetFile, $patchFile)
105
    {
106
        return sprintf(
107
            $this->command,
108
            $targetFile,
109
            $patchFile,
110
            $this->dryRun ? '--dry-run' : ''
111
        );
112
    }
113
}
114