Completed
Pull Request — master (#1)
by Rafał
02:55
created

CreateTenantCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 132
Duplicated Lines 22.73 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 15
c 2
b 0
f 2
lcom 1
cbo 10
dl 30
loc 132
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 18 1
B execute() 0 30 4
C interact() 30 35 7
A createTenant() 0 9 1
A getEntityManager() 0 4 1
A getTenantRepository() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()->findBySubdomain($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(
79
            sprintf(
80
                'Tenant <info>%s</info> has been created and <info>%s</info>!',
81
                $name,
82
                $tenant->isEnabled() ? 'enabled' : 'disabled'
83
            )
84
        );
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function interact(InputInterface $input, OutputInterface $output)
91
    {
92
        $default = $input->getOption('default');
93 View Code Duplication
        if (!$input->getArgument('subdomain') && !$default) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
            $subdomain = $this->getHelper('dialog')->askAndValidate(
95
                $output,
96
                '<question>Please enter subdomain:</question>',
97
                function ($subdomain) {
98
                    if (empty($subdomain)) {
99
                        throw new \RuntimeException('Subdomain can not be empty');
100
                    }
101
102
                    return $subdomain;
103
                }
104
            );
105
106
            $input->setArgument('subdomain', $subdomain);
107
        }
108
109 View Code Duplication
        if (!$input->getArgument('name') && !$default) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
            $name = $this->getHelper('dialog')->askAndValidate(
111
                $output,
112
                '<question>Please enter name:</question>',
113
                function ($name) {
114
                    if (empty($name)) {
115
                        throw new \RuntimeException('Name can not be empty');
116
                    }
117
118
                    return $name;
119
                }
120
            );
121
122
            $input->setArgument('name', $name);
123
        }
124
    }
125
126
    /**
127
     * Creates a new tenant.
128
     *
129
     * @param $subdomain
130
     * @param $name
131
     * @param $disabled
132
     *
133
     * @return TenantInterface
134
     */
135
    protected function createTenant($subdomain, $name, $disabled)
136
    {
137
        $tenant = new Tenant();
138
        $tenant->setSubdomain($subdomain);
139
        $tenant->setName($name);
140
        $tenant->setEnabled(!$disabled);
141
142
        return $tenant;
143
    }
144
145
    /**
146
     * @return EntityManagerInterface
147
     */
148
    protected function getEntityManager()
149
    {
150
        return $this->getContainer()->get('doctrine.orm.entity_manager');
151
    }
152
153
    /**
154
     * @return TenantRepositoryInterface
155
     */
156
    protected function getTenantRepository()
157
    {
158
        return $this->getContainer()->get('swp_multi_tenancy.tenant_repository');
159
    }
160
}
161