Completed
Push — master ( b78bb2...efb1a0 )
by Rafał
14:32
created

ConsoleCommandTenantListener::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
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
15
namespace SWP\Bundle\MultiTenancyBundle\EventListener;
16
17
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
18
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
19
use Symfony\Component\Console\Event\ConsoleCommandEvent;
20
21
/**
22
 * Class ConsoleCommandTenantListener.
23
 *
24
 * It set tenant from tenant code provided in console command options
25
 */
26
class ConsoleCommandTenantListener
27
{
28
    /**
29
     * @var TenantContextInterface
30
     */
31
    protected $tenantContext;
32
33
    /**
34
     * @var TenantRepositoryInterface
35
     */
36
    protected $tenantRepository;
37
38
    /**
39
     * ConsoleCommandTenantListener constructor.
40
     *
41
     * @param TenantContextInterface $tenantContext
42
     */
43
    public function __construct(TenantContextInterface $tenantContext, TenantRepositoryInterface $tenantRepository)
44
    {
45
        $this->tenantContext = $tenantContext;
46
        $this->tenantRepository = $tenantRepository;
47
    }
48
49
    /**
50
     * @param ConsoleCommandEvent $event
51
     */
52
    public function onConsoleCommand(ConsoleCommandEvent $event)
53
    {
54
        if (!$event->getInput()->hasOption('tenant')) {
55
            return;
56
        }
57
58
        $tenantCode = $event->getInput()->getOption('tenant');
59
        if (null !== $tenantCode) {
60
            $tenant = $this->tenantRepository->findOneByCode($tenantCode);
61
            if (null !== $tenant) {
62
                $this->tenantContext->setTenant($tenant);
63
            }
64
        }
65
    }
66
}
67