Completed
Pull Request — master (#1)
by Rafał
10:37
created

PHPCRBasePathsInitializer::getBasePaths()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 25
rs 8.439
cc 5
eloc 15
nc 6
nop 0
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
        $basePaths = [];
93
        foreach ($tenants as $tenant) {
94
            $subdomain = $tenant['subdomain'];
95
96
            $site = $this->dm->find($this->siteClass, $this->pathBuilder->build('/', $subdomain));
97
            if (!$site) {
98
                $site = new $this->siteClass();
99
                if (!$site instanceof SiteDocumentInterface) {
100
                    throw new UnexpectedTypeException($site, 'SWP\Component\MultiTenancy\Model\SiteDocumentInterface');
101
                }
102
103
                $site->setId((string) $this->pathBuilder->build('/', $subdomain));
104
                $this->dm->persist($site);
105
            }
106
107
            foreach ($this->paths as $path) {
108
                $basePaths[] = $this->pathBuilder->build($path, $subdomain);
109
            }
110
        }
111
112
        return $basePaths;
113
    }
114
115
    private function createBasePaths(SessionInterface $session, array $basePaths)
116
    {
117
        foreach ($basePaths as $path) {
118
            NodeHelper::createPath($session, $path);
119
        }
120
121
        $session->save();
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getName()
128
    {
129
        return 'Multi-tenancy base paths';
130
    }
131
}
132