TenantFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 8 1
A createWithoutCode() 0 4 1
A createForOrganization() 0 12 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Component.
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 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\MultiTenancy\Factory;
16
17
use SWP\Component\Common\Generator\GeneratorInterface;
18
use SWP\Component\MultiTenancy\Model\TenantInterface;
19
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface;
20
use SWP\Component\Storage\Factory\FactoryInterface;
21
22
/**
23
 * Class TenantFactory.
24
 */
25
class TenantFactory implements TenantFactoryInterface
26
{
27
    /**
28
     * @var FactoryInterface
29
     */
30
    protected $decoratedFactory;
31
32
    /**
33
     * @var GeneratorInterface
34
     */
35
    protected $generator;
36
37
    /**
38
     * @var OrganizationRepositoryInterface
39
     */
40
    protected $organizationRepository;
41
42
    public function __construct(
43
        FactoryInterface $decoratedFactory,
44
        GeneratorInterface $generator,
45
        OrganizationRepositoryInterface $organizationRepository
46
    ) {
47
        $this->decoratedFactory = $decoratedFactory;
48
        $this->generator = $generator;
49
        $this->organizationRepository = $organizationRepository;
50
    }
51
52
    public function create(): TenantInterface
53
    {
54
        /** @var TenantInterface $tenant */
55
        $tenant = $this->decoratedFactory->create();
56
        $tenant->setCode($this->generator->generate(6));
57
58
        return $tenant;
59
    }
60
61
    public function createWithoutCode(): TenantInterface
62
    {
63
        return $this->decoratedFactory->create();
64
    }
65
66
    public function createForOrganization(string $code): TenantInterface
67
    {
68
        if (null === $organization = $this->organizationRepository->findOneByCode($code)) {
69
            throw new \InvalidArgumentException(sprintf('Organization does not exist with code "%s".', $code));
70
        }
71
72
        /** @var TenantInterface $tenant */
73
        $tenant = $this->create();
74
        $tenant->setOrganization($organization);
75
76
        return $tenant;
77
    }
78
}
79