Completed
Push — master ( bbdfc2...ebac7c )
by Thomas
05:30
created

GenerateApiCommand::generateRelationshipOperation()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 93
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 93
ccs 0
cts 63
cp 0
rs 5.0515
cc 10
eloc 67
nc 14
nop 2
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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());
60
		$swagger->getPaths()->addAll($custom->getPaths());
0 ignored issues
show
Bug introduced by
The method addAll() does not seem to exist on object<gossi\swagger\collections\Paths>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
		$swagger->getDefinitions()->setAll($swagger->getDefinitions());
0 ignored issues
show
Bug introduced by
The method setAll() does not seem to exist on object<gossi\swagger\collections\Definitions>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
}