GenerateDomainCommand::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace keeko\tools\command;
3
4
use keeko\tools\generator\domain\base\ModelDomainTraitGenerator;
5
use keeko\tools\generator\domain\base\ReadOnlyModelDomainTraitGenerator;
6
use keeko\tools\generator\domain\ModelDomainGenerator;
7
use keeko\tools\generator\domain\SkeletonDomainGenerator;
8
use keeko\tools\helpers\QuestionHelperTrait;
9
use keeko\tools\ui\DomainUI;
10
use phootwork\lang\Text;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Propel\Generator\Model\Table;
16
17
class GenerateDomainCommand extends AbstractKeekoCommand {
18
19
	use QuestionHelperTrait;
20
21
	private $twig;
22
23
	protected function configure() {
24
		$this
25
			->setName('generate:domain')
26
			->setDescription('Generates a domain object')
27
			->addArgument(
28
				'name',
29
				InputArgument::OPTIONAL,
30
				'The name of the action, which should be generated. Typically in the form %nomen%-%verb% (e.g. user-create)'
31
			)
32
			->addOption(
33
				'model',
34
				'm',
35
				InputOption::VALUE_OPTIONAL,
36
				'The model for which the actions should be generated, when there is no name argument (if ommited all models will be generated)'
37
			)
38
		;
39
40
		$this->configureGenerateOptions();
41
42
		parent::configure();
43
	}
44
45
	protected function initialize(InputInterface $input, OutputInterface $output) {
46
		parent::initialize($input, $output);
47
48
		$loader = new \Twig_Loader_Filesystem($this->service->getConfig()->getTemplateRoot() . '/domain');
49
		$this->twig = new \Twig_Environment($loader);
50
	}
51
52
	/**
53
	 * Checks whether actions can be generated at all by reading composer.json and verify
54
	 * all required information are available
55
	 */
56
	private function check() {
57
		$module = $this->packageService->getModule();
58
		if ($module === null) {
59
			throw new \DomainException('No module definition found in composer.json - please run `keeko init`.');
60
		}
61
	}
62
63
	protected function interact(InputInterface $input, OutputInterface $output) {
64
		$this->check();
65
66
		$ui = new DomainUI($this);
67
		$ui->show();
68
	}
69
70
	protected function execute(InputInterface $input, OutputInterface $output) {
71
		$this->check();
72
73
		// 1. find out which action(s) to generate
74
		// 2. generate the information in the package
75
		// 3. generate the code for the action
76
77
		$name = $input->getArgument('name');
78
		$modelName = $input->getOption('model');
79
80
		// generate a skeleton
81
		if ($name) {
82
			$this->generateSkeleton($name);
83
		}
84
85
		// create domain for one model
86
		else if ($modelName) {
87
			if (!$this->modelService->hasModel($modelName)) {
88
				throw new \RuntimeException(sprintf('Model (%s) does not exist.', $modelName));
89
			}
90
			$this->generateModel($this->modelService->getModel($modelName));
0 ignored issues
show
Bug introduced by
It seems like $this->modelService->getModel($modelName) can be null; however, generateModel() 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...
91
		}
92
93
		// generate domain for all models
94
		else {
95
			foreach ($this->modelService->getModels() as $model) {
96
				$this->generateModel($model);
97
			}
98
		}
99
	}
100
101
	private function generateModel(Table $model) {
102
		$this->logger->info('Generate Domain from Model: ' . $model->getOriginCommonName());
103
104
		// generate class
105
		$generator = new ModelDomainGenerator($this->service);
106
		$class = $generator->generate($model);
107
		$this->codeService->dumpStruct($class, true);
108
109
		// generate trait
110
		$generator = $model->isReadOnly()
111
			? new ReadOnlyModelDomainTraitGenerator($this->service)
112
			: new ModelDomainTraitGenerator($this->service);
113
		$trait = $generator->generate($model);
114
		$this->codeService->dumpStruct($trait, true);
115
	}
116
117
	private function generateSkeleton($name) {
118
		$this->logger->info('Generate Skeleton Domain: ' . $name);
119
		$input = $this->io->getInput();
120
121
		$namespace = $this->factory->getNamespaceGenerator()->getDomainNamespace();
122
		$className = $namespace . '\\' . $name;
123
124
		if (!Text::create($className)->endsWith('Domain')) {
125
			$className .= 'Domain';
126
		}
127
128
		// generate code
129
		$generator = new SkeletonDomainGenerator($this->service);
130
		$class = $generator->generate($className);
131
		$this->codeService->dumpStruct($class, $input->getOption('force'));
132
	}
133
134
}
135