|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancyBundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. 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
|
|
|
|
|
15
|
|
|
namespace SWP\Bundle\MultiTenancyBundle\Command; |
|
16
|
|
|
|
|
17
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
18
|
|
|
use SWP\Component\MultiTenancy\Model\OrganizationInterface; |
|
19
|
|
|
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface; |
|
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
|
|
|
use Symfony\Component\Console\Question\Question; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class CreateOrganizationCommand. |
|
29
|
|
|
*/ |
|
30
|
|
|
class CreateOrganizationCommand extends ContainerAwareCommand |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function configure() |
|
36
|
|
|
{ |
|
37
|
|
|
$this |
|
38
|
|
|
->setName('swp:organization:create') |
|
39
|
|
|
->setDescription('Creates a new organization.') |
|
40
|
|
|
->setDefinition([ |
|
41
|
|
|
new InputArgument('name', InputArgument::OPTIONAL, 'Organization name'), |
|
42
|
|
|
new InputOption('disabled', null, InputOption::VALUE_NONE, 'Set the organization as a disabled'), |
|
43
|
|
|
new InputOption('default', null, InputOption::VALUE_NONE, 'Creates the default organization'), |
|
44
|
|
|
]) |
|
45
|
|
|
->setHelp( |
|
46
|
|
|
<<<'EOT' |
|
47
|
|
|
The <info>%command.name%</info> command creates a new organization. |
|
48
|
|
|
EOT |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
56
|
|
|
{ |
|
57
|
|
|
$name = $input->getArgument('name'); |
|
58
|
|
|
$default = $input->getOption('default'); |
|
59
|
|
|
if ($default) { |
|
60
|
|
|
$name = OrganizationInterface::DEFAULT_NAME; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$organization = $this->getOrganizationRepository()->findOneByName($name); |
|
64
|
|
|
|
|
65
|
|
|
if (null !== $organization) { |
|
66
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" organization already exists!', $name)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$organization = $this->createOrganization($name, $input); |
|
70
|
|
|
|
|
71
|
|
|
$this->getObjectManager()->persist($organization); |
|
72
|
|
|
$this->getObjectManager()->flush(); |
|
73
|
|
|
|
|
74
|
|
|
$this->sendOutput($output, $organization); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param OutputInterface $output |
|
79
|
|
|
* @param OrganizationInterface $organization |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function sendOutput(OutputInterface $output, $organization) |
|
82
|
|
|
{ |
|
83
|
|
|
$output->writeln( |
|
84
|
|
|
sprintf( |
|
85
|
|
|
'Organization <info>%s</info> (code: <info>%s</info>) has been created and <info>%s</info>!', |
|
86
|
|
|
$organization->getName(), |
|
87
|
|
|
$organization->getCode(), |
|
88
|
|
|
$organization->isEnabled() ? 'enabled' : 'disabled' |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param InputInterface $input |
|
95
|
|
|
* @param OutputInterface $output |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
|
98
|
|
|
{ |
|
99
|
|
|
$default = $input->getOption('default'); |
|
100
|
|
|
if (!$default) { |
|
101
|
|
|
$this->askAndValidateInteract($input, $output, 'name'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param InputInterface $input |
|
107
|
|
|
* @param OutputInterface $output |
|
108
|
|
|
* @param string $name |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function askAndValidateInteract(InputInterface $input, OutputInterface $output, $name) |
|
111
|
|
|
{ |
|
112
|
|
|
if (!$input->getArgument($name)) { |
|
113
|
|
|
$question = new Question(sprintf('<question>Please enter %s:</question>', $name)); |
|
114
|
|
|
$question->setValidator(function ($argument) use ($name) { |
|
115
|
|
|
if (empty($argument)) { |
|
116
|
|
|
throw new \RuntimeException(sprintf('The %s can not be empty', $name)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $argument; |
|
120
|
|
|
}); |
|
121
|
|
|
|
|
122
|
|
|
$question->setMaxAttempts(3); |
|
123
|
|
|
|
|
124
|
|
|
$argument = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
$input->setArgument($name, $argument); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $name |
|
132
|
|
|
* @param bool $disabled |
|
|
|
|
|
|
133
|
|
|
* |
|
134
|
|
|
* @return OrganizationInterface |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function createOrganization($name, $input) |
|
137
|
|
|
{ |
|
138
|
|
|
$organizationFactory = $this->getContainer()->get('swp.factory.organization'); |
|
139
|
|
|
/** @var OrganizationInterface $organization */ |
|
140
|
|
|
$organization = $organizationFactory->createWithCode(); |
|
141
|
|
|
$organization->setName($name); |
|
142
|
|
|
$organization->setEnabled(!$input->getOption('disabled')); |
|
143
|
|
|
|
|
144
|
|
|
return $organization; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return ObjectManager |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getObjectManager() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->getContainer()->get('swp.object_manager.organization'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return OrganizationRepositoryInterface |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getOrganizationRepository() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->getContainer()->get('swp.repository.organization'); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: