|
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
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function configure() |
|
35
|
|
|
{ |
|
36
|
|
|
$this |
|
37
|
|
|
->setName('swp:tenant:create') |
|
38
|
|
|
->setDescription('Creates a new tenant.') |
|
39
|
|
|
->setDefinition([ |
|
40
|
|
|
new InputArgument('subdomain', InputArgument::OPTIONAL, 'Subdomain name'), |
|
41
|
|
|
new InputArgument('name', InputArgument::OPTIONAL, 'Tenant name'), |
|
42
|
|
|
new InputOption('disabled', null, InputOption::VALUE_NONE, 'Set the tenant as a disabled'), |
|
43
|
|
|
new InputOption('default', null, InputOption::VALUE_NONE, 'Creates the default tenant'), |
|
44
|
|
|
]) |
|
45
|
|
|
->setHelp( |
|
46
|
|
|
<<<EOT |
|
47
|
|
|
The <info>%command.name%</info> command creates a new tenant. |
|
48
|
|
|
EOT |
|
49
|
|
|
) |
|
50
|
|
|
; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
57
|
|
|
{ |
|
58
|
|
|
$subdomain = $input->getArgument('subdomain'); |
|
59
|
|
|
$name = $input->getArgument('name'); |
|
60
|
|
|
$default = $input->getOption('default'); |
|
61
|
|
|
$disabled = $input->getOption('disabled'); |
|
62
|
|
|
|
|
63
|
|
|
if ($default) { |
|
64
|
|
|
$subdomain = 'default'; |
|
65
|
|
|
$name = 'Default tenant'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$tenant = $this->getTenantRepository()->findOneBySubdomain($subdomain); |
|
|
|
|
|
|
69
|
|
|
if (null !== $tenant) { |
|
70
|
|
|
throw new \InvalidArgumentException(sprintf('Tenant with subdomain "%s" already exists!', $subdomain)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$tenant = $this->createTenant($subdomain, $name, $disabled); |
|
74
|
|
|
|
|
75
|
|
|
$this->getEntityManager()->persist($tenant); |
|
76
|
|
|
$this->getEntityManager()->flush(); |
|
77
|
|
|
|
|
78
|
|
|
$output->writeln(sprintf('Tenant <info>%s</info> has been created!', $name)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
|
85
|
|
|
{ |
|
86
|
|
|
$default = $input->getOption('default'); |
|
87
|
|
View Code Duplication |
if (!$input->getArgument('subdomain') && !$default) { |
|
|
|
|
|
|
88
|
|
|
$subdomain = $this->getHelper('dialog')->askAndValidate( |
|
89
|
|
|
$output, |
|
90
|
|
|
'<question>Please enter subdomain:</question>', |
|
91
|
|
|
function ($subdomain) { |
|
92
|
|
|
if (empty($subdomain)) { |
|
93
|
|
|
throw new \RuntimeException('Subdomain can not be empty'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $subdomain; |
|
97
|
|
|
} |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$input->setArgument('subdomain', $subdomain); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
View Code Duplication |
if (!$input->getArgument('name') && !$default) { |
|
|
|
|
|
|
104
|
|
|
$name = $this->getHelper('dialog')->askAndValidate( |
|
105
|
|
|
$output, |
|
106
|
|
|
'<question>Please enter name:</question>', |
|
107
|
|
|
function ($name) { |
|
108
|
|
|
if (empty($name)) { |
|
109
|
|
|
throw new \RuntimeException('Name can not be empty'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $name; |
|
113
|
|
|
} |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$input->setArgument('name', $name); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Creates a new tenant. |
|
122
|
|
|
* |
|
123
|
|
|
* @param $subdomain |
|
124
|
|
|
* @param $name |
|
125
|
|
|
* @param $disabled |
|
126
|
|
|
* |
|
127
|
|
|
* @return TenantInterface |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function createTenant($subdomain, $name, $disabled) |
|
130
|
|
|
{ |
|
131
|
|
|
$tenant = new Tenant(); |
|
132
|
|
|
$tenant->setSubdomain($subdomain); |
|
133
|
|
|
$tenant->setName($name); |
|
134
|
|
|
$tenant->setEnabled(!$disabled); |
|
135
|
|
|
|
|
136
|
|
|
return $tenant; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return EntityManagerInterface |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getEntityManager() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->getContainer()->get('doctrine.orm.entity_manager'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return TenantRepositoryInterface |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getTenantRepository() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->getContainer()->get('swp_multi_tenancy.tenant_repository'); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.