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

GenerateApiCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 14.63%

Importance

Changes 21
Bugs 1 Features 0
Metric Value
wmc 4
c 21
b 1
f 0
lcom 1
cbo 14
dl 0
loc 58
ccs 6
cts 41
cp 0.1463
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
A check() 0 6 2
B execute() 0 24 1
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
}