Completed
Push — master ( 5d27f2...d6c5c0 )
by Thomas
08:45
created

GenerateEmberAbilitiesCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 18
nc 3
nop 2
1
<?php
2
namespace keeko\tools\command;
3
4
use keeko\framework\utils\NameUtils;
5
use keeko\tools\generator\ember\EmberAbilitiesGenerator;
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
12
class GenerateEmberAbilitiesCommand extends AbstractEmberCommand {
13
	
14
	protected function configure() {
15
		$this
16
			->setName('generate:ember:abilities')
17
			->setDescription('Generates ember abilities');
18
	
19
		parent::configure();
20
	}
21
	
22
	protected function interact(InputInterface $input, OutputInterface $output) {
23
		$ui = new EmberUI($this);
24
		$ui->show();
25
	}
26
	
27
	protected function execute(InputInterface $input, OutputInterface $output) {
28
		$project = $this->getProject();
29
		$package = $project->getPackage();
30
		if ($package->getType() != 'keeko-module') {
31
			throw new \RuntimeException('Package must be of type `keeko-module`');
32
		}
33
		$module = $project->getPackage()->getKeeko()->getModule();
34
		$this->modelService->read($project);
35
		$models = $this->modelService->getModels();
36
		$generator = new EmberAbilitiesGenerator($this->service, $project);
37
		$output = $this->io->getOutput();
38
		
39
		foreach ($models as $model) {
40
			$contents = $generator->generate($model);
41
			$filename = sprintf('%s/ember/app/abilities/%s/%s.js', $this->project->getRootPath(), 
42
				str_replace('.', '/', $module->getSlug()), NameUtils::dasherize($model->getPhpName()));
43
			$file = new File($filename);
44
			$file->write($contents);
45
			$output->writeln(sprintf('Model <info>%s</info> written at <info>%s</info>', 
46
				$model->getOriginCommonName(), $filename));
47
		}
48
	}
49
50
}