Completed
Push — master ( 68f942...d69058 )
by Rafał
18:18
created

CreateOrganizationCommand::execute()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 17
nc 4
nop 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
namespace SWP\Bundle\MultiTenancyBundle\Command;
15
16
use Doctrine\Common\Persistence\ObjectManager;
17
use SWP\Component\MultiTenancy\Model\OrganizationInterface;
18
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface;
19
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
/**
26
 * Class CreateOrganizationCommand.
27
 */
28
class CreateOrganizationCommand extends ContainerAwareCommand
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('swp:organization:create')
37
            ->setDescription('Creates a new organization.')
38
            ->setDefinition([
39
                new InputArgument('name', InputArgument::OPTIONAL, 'Organization name'),
40
                new InputOption('disabled', null, InputOption::VALUE_NONE, 'Set the organization as a disabled'),
41
                new InputOption('default', null, InputOption::VALUE_NONE, 'Creates the default organization'),
42
            ])
43
            ->setHelp(
44
                <<<'EOT'
45
                The <info>%command.name%</info> command creates a new organization.
46
EOT
47
            );
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $name = $input->getArgument('name');
56
        $disabled = $input->getOption('disabled');
57
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, $disabled);
70
71
        $this->getObjectManager()->persist($organization);
72
        $this->getObjectManager()->flush();
73
74
        $output->writeln(
75
            sprintf(
76
                'Organization <info>%s</info> has been created and <info>%s</info>!',
77
                $name,
78
                $organization->isEnabled() ? 'enabled' : 'disabled'
79
            )
80
        );
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function interact(InputInterface $input, OutputInterface $output)
87
    {
88
        $this->askAndValidateInteract($input, $output, 'name');
89
    }
90
91
    /**
92
     * @param InputInterface  $input
93
     * @param OutputInterface $output
94
     * @param string          $name
95
     */
96 View Code Duplication
    protected function askAndValidateInteract(InputInterface $input, OutputInterface $output, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $default = $input->getOption('default');
99
100
        if (!$input->getArgument($name) && !$default) {
101
            $argument = $this->getHelper('dialog')->askAndValidate(
0 ignored issues
show
Bug introduced by
The method askAndValidate() does not seem to exist on object<Symfony\Component...Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
                $output,
103
                '<question>Please enter '.$name.':</question>',
104
                function ($argument) use ($name) {
105
                    if (empty($argument)) {
106
                        throw new \RuntimeException('The '.$name.' can not be empty');
107
                    }
108
109
                    return $argument;
110
                }
111
            );
112
113
            $input->setArgument($name, $argument);
114
        }
115
    }
116
117
    /**
118
     * @param string $name
119
     * @param bool   $disabled
120
     *
121
     * @return OrganizationInterface
122
     */
123
    protected function createOrganization($name, $disabled)
124
    {
125
        $organizationFactory = $this->getContainer()->get('swp.factory.organization');
126
        /** @var OrganizationInterface $organization */
127
        $organization = $organizationFactory->createWithCode();
128
        $organization->setName($name);
129
        $organization->setEnabled(!$disabled);
130
131
        return $organization;
132
    }
133
134
    /**
135
     * @return ObjectManager
136
     */
137
    protected function getObjectManager()
138
    {
139
        return $this->getContainer()->get('swp.object_manager.organization');
140
    }
141
142
    /**
143
     * @return OrganizationRepositoryInterface
144
     */
145
    protected function getOrganizationRepository()
146
    {
147
        return $this->getContainer()->get('swp.repository.organization');
148
    }
149
}
150