Completed
Pull Request — 1.5 (#761)
by Paweł
17:05
created

TenantContext::theFollowingTenants()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.3306
c 0
b 0
f 0
cc 7
nc 10
nop 1
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
            if (array_key_exists('code', $columns)) {
51
                $existingTenant = $this->tenantRepository->findOneByCode($columns['code']);
52
                if (null !== $existingTenant) {
53
                    $currentTenant = $this->setCurrentTenant($columns, $existingTenant);
54
55
                    continue;
56
                }
57
            }
58
59
            if (array_key_exists('code', $columns)) {
60
                $tenant = $this->tenantFactory->createWithoutCode();
61
            } else {
62
                $tenant = $this->tenantFactory->create();
63
            }
64
            $this->entityManager->persist($tenant);
65
66
            /** @var OrganizationInterface $organization */
67
            $organization = $this->organizationFactory->create();
68
            $organization->setName($columns['organization']);
69
            $organization->setCode('123456');
70
            $this->entityManager->persist($organization);
71
            $columns['organization'] = $organization;
72
73
            if ('null' === $columns['subdomain'] || 0 === strlen(trim($columns['subdomain']))) {
74
                $columns['subdomain'] = null;
75
            }
76
            $columns['enabled'] = (bool) $columns['enabled'];
77
78
            $currentTenant = $this->setCurrentTenant($columns, $tenant);
79
80
            $this->fillObject($tenant, $columns);
81
        }
82
83
        $this->entityManager->flush();
84
        $this->tenantContext->setTenant($currentTenant);
85
    }
86
87
    /**
88
     * @Given default tenant with code :code
89
     */
90
    public function defaultTenantWithCode($code)
91
    {
92
        $tenant = $this->tenantRepository->findOneByCode($code);
93
        if (null === $tenant) {
94
            throw new \Exception('Tenant was not found');
95
        }
96
97
        $this->tenantContext->setTenant($tenant);
98
    }
99
100
    private function setCurrentTenant(&$columns, $tenant)
101
    {
102
        $currentTenant = null;
103
        if (true === (bool) $columns['default']) {
104
            $currentTenant = $tenant;
105
        }
106
        unset($columns['default']);
107
108
        return $currentTenant;
109
    }
110
}
111