1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Discovery\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Composer\Command\BaseCommand; |
8
|
|
|
use Composer\Factory; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use TheCodingMachine\Discovery\Asset; |
14
|
|
|
use TheCodingMachine\Discovery\AssetOperation; |
15
|
|
|
use TheCodingMachine\Discovery\AssetsBuilder; |
16
|
|
|
use TheCodingMachine\Discovery\AssetType; |
17
|
|
|
use TheCodingMachine\Discovery\DiscoveryFileLoader; |
18
|
|
|
use TheCodingMachine\Discovery\Dumper; |
19
|
|
|
|
20
|
|
|
class RemoveAssetCommand extends AbstractDiscoveryCommand |
21
|
|
|
{ |
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
|
|
$this->setName('discovery:remove') |
25
|
|
|
->setDescription('Remove an asset') |
26
|
|
|
->addArgument('asset-type', InputArgument::REQUIRED, 'The asset type') |
27
|
|
|
->addArgument('value', InputArgument::REQUIRED, 'The asset to remove'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
31
|
|
|
{ |
32
|
|
|
$assetTypeName = $input->getArgument('asset-type'); |
33
|
|
|
$assetValue = $input->getArgument('value'); |
34
|
|
|
|
35
|
|
|
$loader = new DiscoveryFileLoader(); |
36
|
|
|
|
37
|
|
View Code Duplication |
if (file_exists('discovery.json')) { |
|
|
|
|
38
|
|
|
$assetOperationTypes = $loader->loadDiscoveryFile(new \SplFileInfo('discovery.json'), '', ''); |
39
|
|
|
} else { |
40
|
|
|
$assetOperationTypes = []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($this->isAssetInLocalFile($assetOperationTypes, $assetTypeName, $assetValue)) { |
44
|
|
|
$assetOperationTypes[$assetTypeName] = array_filter($assetOperationTypes[$assetTypeName], function(AssetOperation $assetOperation) use ($assetValue) { |
45
|
|
|
return !($assetOperation->getOperation() === AssetOperation::ADD && $assetOperation->getAsset()->getValue() === $assetValue); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
if (empty($assetOperationTypes[$assetTypeName])) { |
49
|
|
|
unset($assetOperationTypes[$assetTypeName]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
} else { |
53
|
|
|
// Does this value exists in the first place? |
54
|
|
|
|
55
|
|
|
$assetTypes = $this->getAssetTypes(); |
56
|
|
|
|
57
|
|
|
if (!$this->isAssetInProject($assetTypes, $assetTypeName, $assetValue)) { |
58
|
|
|
$output->writeln(sprintf('<error>There is no asset "%s" in asset type "%s".</error>', $assetValue, $assetTypeName)); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$assetOperationTypes[$assetTypeName][] = new AssetOperation(AssetOperation::REMOVE, new Asset($assetValue, '', '', 0.0, null)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
$loader->saveDiscoveryFile($assetOperationTypes, new \SplFileObject('discovery.json', 'w+')); |
67
|
|
|
|
68
|
|
|
$dumper = new Dumper($this->getComposer(), $this->getIO()); |
|
|
|
|
69
|
|
|
$dumper->dumpDiscoveryFiles(); |
70
|
|
|
|
71
|
|
|
$output->writeln('discovery.json file successfully modified.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param AssetOperation[][] $assetOperationTypes |
76
|
|
|
* @param string $assetTypeName |
77
|
|
|
* @param string $assetValue |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
private function isAssetInLocalFile(array $assetOperationTypes, string $assetTypeName, string $assetValue) : bool |
81
|
|
|
{ |
82
|
|
|
if (!isset($assetOperationTypes[$assetTypeName])) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($assetOperationTypes[$assetTypeName] as $assetOperation) { |
87
|
|
|
if ($assetOperation->getOperation() === AssetOperation::ADD && $assetOperation->getAsset()->getValue() === $assetValue) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param AssetType[] $assetTypes |
97
|
|
|
* @param string $assetTypeName |
98
|
|
|
* @param string $assetValue |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
private function isAssetInProject(array $assetTypes, string $assetTypeName, string $assetValue) : bool |
102
|
|
|
{ |
103
|
|
|
if (!isset($assetTypes[$assetTypeName])) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
foreach ($assetTypes[$assetTypeName]->getAssets() as $asset) { |
108
|
|
|
if ($asset->getValue() === $assetValue) { |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.