Completed
Push — master ( db7fdf...6d7a34 )
by Rafał
08:10
created

TenantFactory::createForOrganization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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
namespace SWP\Component\MultiTenancy\Factory;
15
16
use SWP\Component\Common\Generator\GeneratorInterface;
17
use SWP\Component\MultiTenancy\Model\TenantInterface;
18
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface;
19
use SWP\Component\Storage\Factory\FactoryInterface;
20
21
/**
22
 * Class TenantFactory.
23
 */
24
class TenantFactory implements TenantFactoryInterface
25
{
26
    /**
27
     * @var FactoryInterface
28
     */
29
    protected $decoratedFactory;
30
31
    /**
32
     * @var GeneratorInterface
33
     */
34
    protected $generator;
35
36
    /**
37
     * @var OrganizationRepositoryInterface
38
     */
39
    protected $organizationRepository;
40
41
    /**
42
     * TenantFactory constructor.
43
     *
44
     * @param FactoryInterface                $decoratedFactory
45
     * @param GeneratorInterface              $generator
46
     * @param OrganizationRepositoryInterface $organizationRepository
47
     */
48
    public function __construct(
49
        FactoryInterface $decoratedFactory,
50
        GeneratorInterface $generator,
51
        OrganizationRepositoryInterface $organizationRepository
52
    ) {
53
        $this->decoratedFactory = $decoratedFactory;
54
        $this->generator = $generator;
55
        $this->organizationRepository = $organizationRepository;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function create()
62
    {
63
        /** @var TenantInterface $tenant */
64
        $tenant = $this->decoratedFactory->create();
65
        $tenant->setCode($this->generator->generate(6));
66
67
        return $tenant;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function createForOrganization($code)
74
    {
75
        if (null === $organization = $this->organizationRepository->findOneByCode($code)) {
76
            throw new \InvalidArgumentException(sprintf('Organization does not exist with code "%s".', $code));
77
        }
78
79
        /** @var TenantInterface $tenant */
80
        $tenant = $this->create();
81
        $tenant->setOrganization($organization);
82
83
        return $tenant;
84
    }
85
}
86