Completed
Push — master ( e32e27...6718d9 )
by Rafał
02:26
created

ListTenantsCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
B execute() 0 34 5
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\MultiTenancy\Model\TenantInterface;
18
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
19
use Symfony\Component\Console\Helper\Table;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
class ListTenantsCommand extends ContainerAwareCommand
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('swp:tenant:list')
33
            ->setDescription('List all available tenants.')
34
            ->setDefinition([
35
                new InputOption('organization', 'o', InputOption::VALUE_REQUIRED, 'Organization code (ex: 123456)', null),
36
            ]);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        /* @var TenantInterface[] $tenants */
45
        if (null !== $input->getOption('organization')) {
46
            $organization = $this->getContainer()->get('swp.repository.organization')->findOneByCode($input->getOption('organization'));
47
            $tenants = $this->getContainer()->get('swp.repository.tenant')->findBy([
48
                'organization' => $organization,
49
            ]);
50
        } else {
51
            $tenants = $this->getContainer()->get('swp.repository.tenant')->findAll();
52
        }
53
54
        if (0 === count($tenants)) {
55
            $output->writeln('<error>There are no tenants defined.</error>');
56
57
            return;
58
        }
59
60
        $output->writeln('<info>List of all available tenants:</info>');
61
        $table = new Table($output);
62
        $table->setHeaders(['Id', 'Code', 'Name', 'Is active?', 'Created at', 'Organization']);
63
        foreach ($tenants as $tenant) {
64
            $table->addRow([
65
                $tenant->getId(),
66
                $tenant->getCode(),
67
                $tenant->getName(),
68
                $tenant->isEnabled() ? 'yes' : 'no',
69
                $tenant->getCreatedAt()->format('Y-m-d H:i:s'),
70
                $tenant->getOrganization()->getName().' (code: '.$tenant->getOrganization()->getCode().')',
71
            ]);
72
        }
73
74
        $table->render();
75
    }
76
}
77