GenerateApiCommand::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
namespace keeko\tools\command;
3
4
use gossi\swagger\Swagger;
5
use gossi\swagger\Tag;
6
use keeko\tools\generator\api\ApiGenerator;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class GenerateApiCommand extends AbstractKeekoCommand {
12
13 20
	private $needsResourceIdentifier = false;
0 ignored issues
show
Unused Code introduced by
The property $needsResourceIdentifier is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14 20
	private $needsPagedMeta = false;
0 ignored issues
show
Unused Code introduced by
The property $needsPagedMeta is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15 20
16 20
	protected function configure() {
17
		$this
18
			->setName('generate:api')
19 20
			->setDescription('Generates the api for the module')
20 20
			->addOption(
21
				'model',
22
				'm',
23
				InputOption::VALUE_OPTIONAL,
24
				'The model for which the actions should be generated, when there is no name argument (if ommited all models will be generated)'
25
			)
26
		;
27
28
		$this->configureGenerateOptions();
29
30
		parent::configure();
31
	}
32
33
	/**
34
	 * Checks whether api can be generated at all by reading composer.json and verify
35
	 * all required information are available
36
	 */
37
	private function check() {
38
		$module = $this->packageService->getModule();
39
		if ($module === null) {
40
			throw new \DomainException('No module definition found in composer.json - please run `keeko init`.');
41
		}
42
	}
43
44
	protected function execute(InputInterface $input, OutputInterface $output) {
45
		$this->check();
46
47
		$module = $this->package->getKeeko()->getModule();
48
		$swagger = new Swagger();
49
		$swagger->setVersion('2.0');
50
		$swagger->getInfo()->setTitle($module->getTitle() . ' API');
51
		$swagger->getTags()->add(new Tag(['name' => $module->getSlug()]));
52
53
		// generate api from package
54
		$apigen = new ApiGenerator($this->service);
55
		$apigen->generatePaths($swagger);
56
		$apigen->generateDefinitions($swagger);
57
58
		// add custom entries from generator.json
59
		$custom = new Swagger($this->project->getGeneratorDefinition()->getApi());
0 ignored issues
show
Documentation introduced by
$this->project->getGener...rDefinition()->getApi() is of type object<phootwork\collection\Map>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
		$swagger->getPaths()->addAll($custom->getPaths());
61
		$swagger->getDefinitions()->setAll($swagger->getDefinitions());
62
63
		// dump to file
64
		$filename = $this->project->getApiFileName();
65
		$this->jsonService->write($filename, $swagger->toArray());
66
		$this->io->writeln(sprintf('API for <info>%s</info> written at <info>%s</info>', $this->package->getFullName(), $filename));
67
	}
68
}