ListTenantsCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 9 1
B execute() 0 39 7
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\MultiTenancyBundle\Command;
16
17
use SWP\Component\Common\Model\ThemeAwareTenantInterface;
18
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface;
19
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Helper\Table;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class ListTenantsCommand extends Command
27
{
28
    protected static $defaultName = 'swp:tenant:list';
29
30
    private $organizationRepository;
31
32
    private $tenantRepository;
33
34
    public function __construct(OrganizationRepositoryInterface $organizationRepository, TenantRepositoryInterface $tenantRepository)
35
    {
36
        parent::__construct();
37
38
        $this->organizationRepository = $organizationRepository;
39
        $this->tenantRepository = $tenantRepository;
40
    }
41
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('swp:tenant:list')
46
            ->setDescription('List all available tenants.')
47
            ->setDefinition([
48
                new InputOption('organization', 'o', InputOption::VALUE_REQUIRED, 'Organization code (ex: 123456)', null),
49
            ]);
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        /* @var ThemeAwareTenantInterface[] $tenants */
55
        if (null !== $input->getOption('organization')) {
56
            $organization = $this->organizationRepository->findOneByCode($input->getOption('organization'));
57
            $tenants = $this->tenantRepository->findBy([
58
                'organization' => $organization,
59
            ]);
60
        } else {
61
            $tenants = $this->tenantRepository->findAll();
62
        }
63
64
        if (null === $tenants || 0 === count($tenants)) {
65
            $output->writeln('<error>There are no tenants defined.</error>');
66
67
            return 0;
68
        }
69
70
        $output->writeln('<info>List of all available tenants:</info>');
71
        $table = new Table($output);
72
        $table->setHeaders(['Id', 'Code', 'Name', 'Domain', 'Subdomain', 'Is active?', 'Theme Name', 'Created at', 'Organization']);
73
        foreach ($tenants as $tenant) {
74
            $table->addRow([
75
                $tenant->getId(),
76
                $tenant->getCode(),
77
                $tenant->getName(),
78
                $tenant->getDomainName(),
79
                $tenant->getSubdomain(),
80
                $tenant->isEnabled() ? 'yes' : 'no',
81
                $tenant instanceof ThemeAwareTenantInterface ? $tenant->getThemeName() : null,
82
                $tenant->getCreatedAt()->format('Y-m-d H:i:s'),
83
                $tenant->getOrganization()->getName().' (code: '.$tenant->getOrganization()->getCode().')',
84
            ]);
85
        }
86
87
        $table->render();
88
89
        return 1;
90
    }
91
}
92