|
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\CoreBundle\Command; |
|
16
|
|
|
|
|
17
|
|
|
use SWP\Bundle\CoreBundle\Model\OrganizationInterface; |
|
18
|
|
|
use SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand; |
|
|
|
|
|
|
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
23
|
|
|
|
|
24
|
|
|
class UpdateOrganizationCommand extends CreateOrganizationCommand |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function configure() |
|
30
|
|
|
{ |
|
31
|
|
|
$this |
|
32
|
|
|
->setName('swp:organization:update') |
|
33
|
|
|
->setDescription('Updates existing organization.') |
|
34
|
|
|
->setDefinition([ |
|
35
|
|
|
new InputArgument('name', InputArgument::OPTIONAL, 'Organization name'), |
|
36
|
|
|
new InputOption('disabled', null, InputOption::VALUE_NONE, 'Set the organization as a disabled'), |
|
37
|
|
|
new InputOption('secretToken', null, InputOption::VALUE_REQUIRED, 'Organization secret token'), |
|
38
|
|
|
]) |
|
39
|
|
|
->setHelp( |
|
40
|
|
|
<<<'EOT' |
|
41
|
|
|
The <info>%command.name%</info> command updates existing organization. |
|
42
|
|
|
EOT |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param InputInterface $input |
|
48
|
|
|
* @param OutputInterface $output |
|
49
|
|
|
* |
|
50
|
|
|
* @return int|null|void |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
55
|
|
|
{ |
|
56
|
|
|
$name = $input->getArgument('name'); |
|
57
|
|
|
$disabled = $input->getOption('disabled'); |
|
58
|
|
|
$secretToken = $input->getOption('secretToken'); |
|
59
|
|
|
|
|
60
|
|
|
/** @var OrganizationInterface $organization */ |
|
61
|
|
|
$organization = $this->getOrganizationRepository()->findOneByName($name); |
|
62
|
|
|
if (null !== $organization) { |
|
63
|
|
|
$this->updateOrganization($organization, $disabled, $secretToken); |
|
64
|
|
|
} else { |
|
65
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" organization don\'t exists!', $name)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->getObjectManager()->persist($organization); |
|
69
|
|
|
$this->getObjectManager()->flush(); |
|
70
|
|
|
$this->sendOutput($output, $organization); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param OutputInterface $output |
|
75
|
|
|
* @param OrganizationInterface $organization |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function sendOutput(OutputInterface $output, $organization) |
|
78
|
|
|
{ |
|
79
|
|
|
$output->writeln( |
|
80
|
|
|
sprintf( |
|
81
|
|
|
'Organization <info>%s</info> (code: <info>%s</info>%s) has been updated and is <info>%s</info>!', |
|
82
|
|
|
$organization->getName(), |
|
83
|
|
|
$organization->getCode(), |
|
84
|
|
|
$organization->getSecretToken() ? ', secret token: <info>'.$organization->getSecretToken().'</info>' : '', |
|
85
|
|
|
$organization->isEnabled() ? 'enabled' : 'disabled' |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->askAndValidateInteract($input, $output, 'name'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param OrganizationInterface $organization |
|
100
|
|
|
* @param bool $disabled |
|
101
|
|
|
* @param null|string $secretToken |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function updateOrganization(OrganizationInterface $organization, bool $disabled, ?string $secretToken) |
|
104
|
|
|
{ |
|
105
|
|
|
$organization->setEnabled(!$disabled); |
|
106
|
|
|
if ($secretToken) { |
|
|
|
|
|
|
107
|
|
|
$organization->setSecretToken($secretToken); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: