Completed
Pull Request — master (#1)
by Rafał
02:38
created

PHPCRBasePathsInitializer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 12
c 3
b 1
f 1
lcom 1
cbo 4
dl 0
loc 106
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A init() 0 11 2
A createBasePaths() 0 8 2
A getName() 0 4 1
A getBasePaths() 0 6 1
B genereteBasePaths() 0 24 5
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2015 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 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\MultiTenancyBundle\Initializer;
15
16
use PHPCR\Util\NodeHelper;
17
use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface;
18
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
19
use PHPCR\SessionInterface;
20
use SWP\Component\MultiTenancy\Provider\TenantProviderInterface;
21
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface;
22
use SWP\Component\MultiTenancy\Model\SiteDocumentInterface;
23
use Symfony\Component\HttpFoundation\File\Exception\UnexpectedTypeException;
24
25
/**
26
 * PHPCR Base Paths Repository Initializer.
27
 *
28
 * It creates based paths in content repository based on provided
29
 * tenants and config. Disabled by default, can be enabled in config.
30
 * Requires DoctrinePHPCRBundle to be configured in the system.
31
 */
32
class PHPCRBasePathsInitializer implements InitializerInterface
33
{
34
    /**
35
     * @var array
36
     */
37
    private $paths;
38
39
    /**
40
     * @var TenantProviderInterface
41
     */
42
    private $tenantProvider;
43
44
    /**
45
     * @var TenantAwarePathBuilderInterface
46
     */
47
    private $pathBuilder;
48
49
    /**
50
     * @var string
51
     */
52
    private $siteClass;
53
54
    /**
55
     * Construct.
56
     *
57
     * @param array                           $paths          Content paths
58
     * @param TenantProviderInterface         $tenantProvider Tenants provider
59
     * @param TenantAwarePathBuilderInterface $pathBuilder    Path builder
60
     * @param string                          $siteClass      Site document class
61
     */
62
    public function __construct(
63
        array $paths,
64
        TenantProviderInterface $tenantProvider,
65
        TenantAwarePathBuilderInterface $pathBuilder,
66
        $siteClass
67
    ) {
68
        $this->paths = $paths;
69
        $this->tenantProvider = $tenantProvider;
70
        $this->pathBuilder = $pathBuilder;
71
        $this->siteClass = $siteClass;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function init(ManagerRegistry $registry)
78
    {
79
        $session = $registry->getConnection();
80
        $this->dm = $registry->getManager();
81
82
        $basePaths = $this->getBasePaths();
83
        $this->dm->flush();
84
        if (count($basePaths)) {
85
            $this->createBasePaths($session, $basePaths);
86
        }
87
    }
88
89
    private function getBasePaths()
90
    {
91
        $tenants = $this->tenantProvider->getAvailableTenants();
92
93
        return $this->genereteBasePaths($tenants);
94
    }
95
96
    private function genereteBasePaths(array $tenants = array())
97
    {
98
        $basePaths = array();
99
        foreach ($tenants as $tenant) {
100
            $subdomain = $tenant['subdomain'];
101
102
            $site = $this->dm->find($this->siteClass, $this->pathBuilder->build('/', $subdomain));
103
            if (!$site) {
104
                $site = new $this->siteClass();
105
                if (!$site instanceof SiteDocumentInterface) {
106
                    throw new UnexpectedTypeException($site, 'SWP\Component\MultiTenancy\Model\SiteDocumentInterface');
107
                }
108
109
                $site->setId((string) $this->pathBuilder->build('/', $subdomain));
110
                $this->dm->persist($site);
111
            }
112
113
            foreach ($this->paths as $path) {
114
                $basePaths[] = $this->pathBuilder->build($path, $subdomain);
115
            }
116
        }
117
118
        return $basePaths;
119
    }
120
121
    private function createBasePaths(SessionInterface $session, array $basePaths)
122
    {
123
        foreach ($basePaths as $path) {
124
            NodeHelper::createPath($session, $path);
125
        }
126
127
        $session->save();
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getName()
134
    {
135
        return 'Multi-tenancy base paths';
136
    }
137
}
138