Completed
Push — master ( 43a701...9aa864 )
by Thomas
13:49
created

GenerateEmberModelsCommand::execute()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 30
rs 8.439
cc 5
eloc 23
nc 6
nop 2
1
<?php
2
namespace keeko\tools\command;
3
4
use keeko\framework\utils\NameUtils;
5
use keeko\tools\generator\ember\EmberModelGenerator;
6
use keeko\tools\model\Project;
7
use keeko\tools\ui\EmberUI;
8
use phootwork\file\File;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use phootwork\lang\Text;
12
13
class GenerateEmberModelsCommand extends AbstractEmberCommand {
14
15
	protected function configure() {
16
		$this
17
			->setName('generate:ember:models')
18
			->setDescription('Generates ember models');
19
20
		parent::configure();
21
	}
22
23
	protected function interact(InputInterface $input, OutputInterface $output) {
24
		$ui = new EmberUI($this);
25
		$ui->show();
26
	}
27
28
	protected function execute(InputInterface $input, OutputInterface $output) {
29
		$project = $this->getProject();
30
		$package = $project->getPackage();
31
		if ($package->getType() != 'keeko-module') {
32
			throw new \RuntimeException('Package must be of type `keeko-module`');
33
		}
34
		$module = $project->getPackage()->getKeeko()->getModule();
35
		$this->modelService->read($project);
36
		$models = $this->modelService->getModels();
37
		$generator = new EmberModelGenerator($this->service, $project);
38
		$output = $this->io->getOutput();
39
40
		foreach ($models as $model) {
41
			$contents = $generator->generate($model);
42
			$filename = sprintf('%s/ember/app/models/%s/%s.js', $this->project->getRootPath(),
43
				str_replace('.', '/', $module->getSlug()), NameUtils::dasherize($model->getPhpName()));
44
			$file = new File($filename);
45
			$overwrite = true;
46
			if ($file->exists()) {
47
				$contents = new Text($file->read());
48
				$overwrite = !$contents->startsWith('// overwrite: false');
49
			}
50
51
			if ($overwrite) {
52
				$file->write($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by new \phootwork\lang\Text($file->read()) on line 47 can also be of type object<phootwork\lang\Text>; however, phootwork\file\File::write() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
				$output->writeln(sprintf('Model <info>%s</info> written at <info>%s</info>',
54
					$model->getOriginCommonName(), $filename));
55
			}
56
		}
57
	}
58
}