GenerateTenantCommand::execute()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.8571
cc 2
eloc 18
nc 2
nop 2
1
<?php
2
3
	namespace Vivait\AuthBundle\Command;
4
5
	use Vivait\AuthBundle\Entity\Tenant;
6
	use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
	use Symfony\Component\Console\Input\InputArgument;
8
	use Symfony\Component\Console\Input\InputInterface;
9
	use Symfony\Component\Console\Output\OutputInterface;
10
11
	class GenerateTenantCommand extends ContainerAwareCommand {
12
13
		protected function configure() {
14
			$this
15
				->setName('auth:tenant:add')
16
				->setDescription('Create a single tenant into the database')
17
				->addArgument('code', InputArgument::REQUIRED, 'Enter the short code of the tenant?')
18
				->addArgument('name', InputArgument::REQUIRED, 'Enter the name of the tenant?')
19
				->addArgument('date', InputArgument::OPTIONAL, 'When does their license expire?', '+1 month');
20
		}
21
22
23
		protected function execute(InputInterface $input, OutputInterface $output) {
24
			$code = $input->getArgument('code');
25
			$name = $input->getArgument('name');
26
			$date = $input->getArgument('date');
27
28
			$db = $this->getContainer()->get('doctrine')
29
				->getRepository('VivaitAuthBundle:Tenant')
30
				->findOneBy(array('code'=>$code));
31
32
			if($db) {
33
				$output->writeln(sprintf("There is already a tenant (%s) with code: %s",$db->getTenant(),$db->getCode()));
34
				return null;
35
			}
36
37
			$tenant = new Tenant();
38
			$tenant->setCode($code);
39
			$tenant->setTenant($name);
40
			$tenant->setLicenseduntil(new \DateTime($date));
41
42
			$em = $this->getContainer()->get('doctrine')->getManager();
43
			$em->persist($tenant);
44
			$em->flush();
45
46
			$output->writeln(sprintf("A new tenant has been created (%s: %s)", $tenant->getCode(),$tenant->getTenant()));
47
		}
48
	}