Completed
Pull Request — master (#1)
by Rafał
14:18
created

CreateTenantCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 14
c 3
b 0
f 2
lcom 1
cbo 10
dl 0
loc 133
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 18 1
A interact() 0 6 2
A askAndValidateInteract() 0 19 4
A createTenant() 0 9 1
A getEntityManager() 0 4 1
A getTenantRepository() 0 4 1
B execute() 0 30 4
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher MultiTenancyBundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\MultiTenancyBundle\Command;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use SWP\Component\MultiTenancy\Model\Tenant;
18
use SWP\Component\MultiTenancy\Model\TenantInterface;
19
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
20
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
/**
27
 * Class CreateTenantCommand.
28
 */
29
class CreateTenantCommand extends ContainerAwareCommand
30
{
31
    /**
32
     * @var array
33
     */
34
    protected $arguments = ['subdomain', 'name'];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('swp:tenant:create')
43
            ->setDescription('Creates a new tenant.')
44
            ->setDefinition([
45
                new InputArgument($this->arguments[0], InputArgument::OPTIONAL, 'Subdomain name'),
46
                new InputArgument($this->arguments[1], InputArgument::OPTIONAL, 'Tenant name'),
47
                new InputOption('disabled', null, InputOption::VALUE_NONE, 'Set the tenant as a disabled'),
48
                new InputOption('default', null, InputOption::VALUE_NONE, 'Creates the default tenant'),
49
            ])
50
            ->setHelp(
51
                <<<EOT
52
                The <info>%command.name%</info> command creates a new tenant.
53
EOT
54
            )
55
        ;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $subdomain = $input->getArgument($this->arguments[0]);
64
        $name = $input->getArgument($this->arguments[1]);
65
        $default = $input->getOption('default');
66
        $disabled = $input->getOption('disabled');
67
68
        if ($default) {
69
            $subdomain = 'default';
70
            $name = 'Default tenant';
71
        }
72
73
        $tenant = $this->getTenantRepository()->findBySubdomain($subdomain);
74
        if (null !== $tenant) {
75
            throw new \InvalidArgumentException(sprintf('Tenant with subdomain "%s" already exists!', $subdomain));
76
        }
77
78
        $tenant = $this->createTenant($subdomain, $name, $disabled);
79
80
        $this->getEntityManager()->persist($tenant);
81
        $this->getEntityManager()->flush();
82
83
        $output->writeln(
84
            sprintf(
85
                'Tenant <info>%s</info> has been created and <info>%s</info>!',
86
                $name,
87
                $tenant->isEnabled() ? 'enabled' : 'disabled'
88
            )
89
        );
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    protected function interact(InputInterface $input, OutputInterface $output)
96
    {
97
        foreach ($this->arguments as $value) {
98
            $this->askAndValidateInteract($input, $output, $value);
99
        }
100
    }
101
102
    /**
103
     * @param InputInterface  $input
104
     * @param OutputInterface $output
105
     * @param string          $name
106
     */
107
    protected function askAndValidateInteract(InputInterface $input, OutputInterface $output, $name)
108
    {
109
        $default = $input->getOption('default');
110
        if (!$input->getArgument($name) && !$default) {
111
            $argument = $this->getHelper('dialog')->askAndValidate(
112
                $output,
113
                '<question>Please enter '.$name.':</question>',
114
                function ($argument) use ($name) {
115
                    if (empty($argument)) {
116
                        throw new \RuntimeException('The '.$name.' can not be empty');
117
                    }
118
119
                    return $argument;
120
                }
121
            );
122
123
            $input->setArgument($name, $argument);
124
        }
125
    }
126
127
    /**
128
     * Creates a new tenant.
129
     *
130
     * @param $subdomain
131
     * @param $name
132
     * @param $disabled
133
     *
134
     * @return TenantInterface
135
     */
136
    protected function createTenant($subdomain, $name, $disabled)
137
    {
138
        $tenant = new Tenant();
139
        $tenant->setSubdomain($subdomain);
140
        $tenant->setName($name);
141
        $tenant->setEnabled(!$disabled);
142
143
        return $tenant;
144
    }
145
146
    /**
147
     * @return EntityManagerInterface
148
     */
149
    protected function getEntityManager()
150
    {
151
        return $this->getContainer()->get('doctrine.orm.entity_manager');
152
    }
153
154
    /**
155
     * @return TenantRepositoryInterface
156
     */
157
    protected function getTenantRepository()
158
    {
159
        return $this->getContainer()->get('swp_multi_tenancy.tenant_repository');
160
    }
161
}
162