PHPCRBasePathsInitializer::generateBasePaths()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.7333
cc 4
nc 6
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
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 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\MultiTenancyBundle\Initializer;
16
17
use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface;
18
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
19
use Doctrine\Common\Collections\Collection;
20
use Doctrine\ODM\PHPCR\Query\Query;
21
use PHPCR\SessionInterface;
22
use PHPCR\Util\NodeHelper;
23
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface;
24
use SWP\Component\MultiTenancy\Provider\TenantProviderInterface;
25
26
/**
27
 * PHPCR Base Paths Repository Initializer.
28
 *
29
 * It creates based paths in content repository based on provided
30
 * tenants and config. Disabled by default, can be enabled in config.
31
 * Requires DoctrinePHPCRBundle to be configured in the system.
32
 */
33
class PHPCRBasePathsInitializer implements InitializerInterface
34
{
35
    /**
36
     * @var array
37
     */
38
    private $paths;
39
40
    /**
41
     * @var TenantProviderInterface
42
     */
43
    private $tenantProvider;
44
45
    /**
46
     * @var TenantAwarePathBuilderInterface
47
     */
48
    private $pathBuilder;
49
50
    /**
51
     * Construct.
52
     *
53
     * @param array                           $paths          Content paths
54
     * @param TenantProviderInterface         $tenantProvider Tenants provider
55
     * @param TenantAwarePathBuilderInterface $pathBuilder    Path builder
56
     */
57
    public function __construct(
58
        array $paths,
59
        TenantProviderInterface $tenantProvider,
60
        TenantAwarePathBuilderInterface $pathBuilder
61
    ) {
62
        $this->paths = $paths;
63
        $this->tenantProvider = $tenantProvider;
64
        $this->pathBuilder = $pathBuilder;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function init(ManagerRegistry $registry)
71
    {
72
        /** @var SessionInterface $session */
73
        $session = $registry->getConnection();
74
75
        /** @var Query $tenantsQuery */
76
        $tenantsQuery = $this->tenantProvider->getAvailableTenants();
77
        /** @var Collection $tenants */
78
        $tenants = $tenantsQuery->getResult();
79
        $this->generateBasePaths($session, $tenants->toArray());
80
    }
81
82
    private function generateBasePaths(SessionInterface $session, array $tenants = [])
83
    {
84
        $basePaths = [];
85
        foreach ($tenants as $tenant) {
86
            foreach ($this->paths as $path) {
87
                $basePaths[] = $this->pathBuilder->build(
88
                    $path,
89
                    $tenant->getOrganization()->getCode().'/'.$tenant->getCode()
90
                );
91
            }
92
        }
93
94
        if (count($basePaths) > 0) {
95
            $this->createBasePaths($session, $basePaths);
96
        }
97
    }
98
99
    private function createBasePaths(SessionInterface $session, array $basePaths)
100
    {
101
        foreach ($basePaths as $path) {
102
            NodeHelper::createPath($session, $path);
103
        }
104
105
        $session->save();
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getName()
112
    {
113
        return 'Multi-tenancy base paths';
114
    }
115
}
116