Completed
Pull Request — master (#5)
by David
02:07
created

AddAssetCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 17
nc 3
nop 2
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 AddAssetCommand extends BaseCommand
21
{
22
    protected function configure()
23
    {
24
        $this->setName('discovery:add')
25
             ->setDescription('Add an asset')
26
             ->addArgument('asset-type', InputArgument::REQUIRED, 'The asset type')
27
             ->addArgument('value', InputArgument::REQUIRED, 'The asset to add')
28
             ->addOption('priority', 'p', InputOption::VALUE_REQUIRED, 'The priority', 0.0);
29
    }
30
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $assetTypeName = $input->getArgument('asset-type');
34
        $assetValue = $input->getArgument('value');
35
36
        $priority = $input->getOption('priority');
37
38
        if (!is_numeric($priority)) {
39
            $output->writeln('<error>The priority must be a numeric value.</error>');
40
            return;
41
        }
42
43
        $loader = new DiscoveryFileLoader();
44
45
        if (file_exists('discovery.json')) {
46
            $assetOperationTypes = $loader->loadDiscoveryFile(new \SplFileInfo('discovery.json'), '', '');
47
        } else {
48
            $assetOperationTypes = [];
49
        }
50
51
        $assetOperationTypes[$assetTypeName][] = new AssetOperation(AssetOperation::ADD, new Asset($assetValue, '', '', $priority, null));
52
53
        $loader->saveDiscoveryFile($assetOperationTypes, new \SplFileObject('discovery.json', 'w+'));
54
55
        $dumper = new Dumper($this->getComposer(), $this->getIO());
0 ignored issues
show
Bug introduced by
It seems like $this->getComposer() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
56
        $dumper->dumpDiscoveryFiles();
57
58
        $output->writeln('discovery.json file successfully modified.');
59
    }
60
}
61