ListTenantsCommand::execute()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
1
<?php
2
3
namespace Vivait\TenantBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Vivait\TenantBundle\Kernel\TenantKernel;
12
use Vivait\TenantBundle\Model\Tenant;
13
use Vivait\TenantBundle\Registry\TenantRegistry;
14
15
class ListTenantsCommand extends ContainerAwareCommand
16
{
17
    protected function configure()
18
    {
19
        $this
20
            ->setName('vivait:tenants:list')
21
            ->setDescription('Provides a list of tenants')
22
            ->addOption(
23
                'null',
24
                '0',
25
                InputOption::VALUE_NONE,
26
                'Use a null character as a separator (instead of the newline character).'
27
            )
28
            ->addOption(
29
                'force',
30
                'f',
31
                InputOption::VALUE_NONE,
32
                'Force a list of tenants, even if tenanting is disabled for the current environment'
33
            )
34
        ;
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $null = $input->getOption('null');
40
        $force = $input->getOption('force');
41
42
        /** @var TenantKernel $kernel */
43
        $kernel = $this->getContainer()->get('kernel');
44
45
        if ($force || $kernel->enableTenanting) {
46
            /** @var TenantRegistry $registry */
47
            $registry = $this->getContainer()->get('vivait_tenant.registry');
48
49
            $separator = $null ? "\0" : "\n";
50
51
            foreach ($registry->getAll() as $tenant) {
52
                $output->write($tenant->getKey() . $separator);
53
            }
54
        }
55
    }
56
}
57