Completed
Push — master ( 118f52...c5048d )
by Thomas
10:09
created

GenerateEmberSerializerCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace keeko\tools\command;
3
4
use keeko\framework\utils\NameUtils;
5
use keeko\tools\generator\ember\EmberSerializerGenerator;
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 GenerateEmberSerializerCommand extends AbstractEmberCommand {
13
14
	protected function configure() {
15
		$this
16
			->setName('generate:ember:serializer')
17
			->setDescription('Generates ember serializers');
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 EmberSerializerGenerator($this->service, $project);
37
		$output = $this->io->getOutput();
38
39
		foreach ($models as $model) {
40
			$contents = $generator->generate($model);
41
			if ($contents !== null) {
42
				$filename = sprintf('%s/ember/app/serializers/%s/%s.js', $this->project->getRootPath(),
43
					str_replace('.', '/', $module->getSlug()), NameUtils::dasherize($model->getPhpName()));
44
				$file = new File($filename);
45
				$file->write($contents);
46
				$output->writeln(sprintf('Serializer <info>%s</info> written at <info>%s</info>',
47
					$model->getOriginCommonName(), $filename));
48
			}
49
		}
50
	}
51
52
}