Completed
Pull Request — 1.5 (#758)
by Paweł
11:13
created

TenantContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A theFollowingTenants() 0 31 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Behat\Contexts;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Gherkin\Node\TableNode;
9
use Doctrine\ORM\EntityManagerInterface;
10
use SWP\Bundle\CoreBundle\Model\OrganizationInterface;
11
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
12
use SWP\Component\MultiTenancy\Factory\OrganizationFactoryInterface;
13
use SWP\Component\MultiTenancy\Factory\TenantFactoryInterface;
14
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
15
16
final class TenantContext extends AbstractContext implements Context
17
{
18
    private $tenantContext;
19
20
    private $organizationFactory;
21
22
    private $tenantFactory;
23
24
    private $tenantRepository;
25
26
    private $entityManager;
27
28
    public function __construct(
29
        TenantContextInterface $tenantContext,
30
        OrganizationFactoryInterface $organizationFactory,
31
        TenantFactoryInterface $tenantFactory,
32
        TenantRepositoryInterface $tenantRepository,
33
        EntityManagerInterface $entityManager
34
    ) {
35
        $this->tenantContext = $tenantContext;
36
        $this->organizationFactory = $organizationFactory;
37
        $this->tenantFactory = $tenantFactory;
38
        $this->tenantRepository = $tenantRepository;
39
        $this->entityManager = $entityManager;
40
    }
41
42
    /**
43
     * @Given the following Tenants:
44
     */
45
    public function theFollowingTenants(TableNode $table)
46
    {
47
        $currentTenant = null;
48
49
        foreach ($table as $row => $columns) {
50
            $tenant = $this->tenantFactory->create();
51
            $this->entityManager->persist($tenant);
52
53
            /** @var OrganizationInterface $organization */
54
            $organization = $this->organizationFactory->create();
55
            $organization->setName($columns['organization']);
56
            $organization->setCode('123456');
57
            $this->entityManager->persist($organization);
58
            $columns['organization'] = $organization;
59
60
            if ('null' === $columns['subdomain'] || 0 === strlen(trim($columns['subdomain']))) {
61
                $columns['subdomain'] = null;
62
            }
63
            $columns['enabled'] = (bool) $columns['enabled'];
64
65
            if (true === (bool) $columns['default']) {
66
                $currentTenant = $tenant;
67
            }
68
            unset($columns['default']);
69
70
            $this->fillObject($tenant, $columns);
71
        }
72
73
        $this->entityManager->flush();
74
        $this->tenantContext->setTenant($currentTenant);
75
    }
76
}
77