Completed
Push — master ( db30e2...3405a3 )
by Paweł
08:38
created

UpdateOrganizationCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 8
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
A execute() 0 18 2
A sendOutput() 0 12 3
A interact() 0 4 1
A updateOrganization() 0 7 2
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SWP\Bundle\CoreBundle\Co...eateOrganizationCommand.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $secretToken of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
107
            $organization->setSecretToken($secretToken);
108
        }
109
    }
110
}
111