Completed
Pull Request — master (#1)
by Rafał
12:20 queued 05:49
created

TenantAwarePathBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Component.
5
 *
6
 * Copyright 2016 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 2016 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\Component\MultiTenancy\PathBuilder;
15
16
use PHPCR\Util\PathHelper;
17
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
18
use SWP\Component\MultiTenancy\Resolver\TenantResolverInterface;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
21
/**
22
 * TenantAwarePathBuilder class.
23
 */
24
class TenantAwarePathBuilder implements TenantAwarePathBuilderInterface
25
{
26
    /**
27
     * @var TenantResolverInterface
28
     */
29
    protected $tenantResolver;
30
31
    /**
32
     * @var TenantContextInterface
33
     */
34
    protected $tenantContext;
35
36
    /**
37
     * @var string
38
     */
39
    protected $rootPath;
40
41
    /**
42
     * @var RequestStack
43
     */
44
    protected $requestStack;
45
46
    /**
47
     * @var string
48
     */
49
    protected $latestRootPath;
50
51
    /**
52
     * @var string
53
     */
54
    protected $defaultRootPath;
55
56
    /**
57
     * Construct.
58
     *
59
     * @param TenantResolverInterface $tenantResolver Tenant resolver
60
     * @param TenantContextInterface  $tenantContext  Tenant context
61
     * @param string                  $rootPath       PHPCR root path (e.g. /swp)
62
     */
63
    public function __construct(TenantResolverInterface $tenantResolver, TenantContextInterface $tenantContext, $rootPath)
64
    {
65
        $this->tenantResolver = $tenantResolver;
66
        $this->tenantContext = $tenantContext;
67
        $this->rootPath = $rootPath;
68
        $this->defaultRootPath = $rootPath;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function build($data, $context = null)
75
    {
76
        return $this->absolutize($data, $context);
77
    }
78
79
    /**
80
     * Absolutize path or paths based on current context when provided.
81
     *
82
     * @param string|array $data    Path or array of paths
83
     * @param string       $context Path absolute context
84
     *
85
     * @return string|array Tenant aware paths
86
     */
87
    protected function absolutize($data, $context = null)
88
    {
89
        $this->makePathTenantAware();
90
        if (is_array($data)) {
91
            $tenantAwarePaths = [];
92
            foreach ($data as $path) {
93
                $tenantAwarePaths[] = $this->absolutizePath($path, $context);
94
            }
95
96
            return $tenantAwarePaths;
97
        }
98
99
        return $this->absolutizePath($data, $context);
100
    }
101
102
    /**
103
     * Makes PHPCR tree root path to be tenant aware.
104
     *
105
     * When tenant is not available in tenant context it will resolve
106
     * the tenant from current request.
107
     */
108
    protected function makePathTenantAware()
109
    {
110
        if ($this->latestRootPath === $this->rootPath) {
111
            return;
112
        }
113
114
        $tenant = $this->tenantContext->getTenant();
115
        if (null === $tenant->getSubdomain() && null !== $this->getRequestStack()) {
116
            $currentRequest = $this->requestStack->getCurrentRequest();
117
            $tenant = $this->tenantResolver->resolve(
118
                $currentRequest ? $currentRequest->getHost() : null
119
            );
120
121
            $this->tenantContext->setTenant($tenant);
122
        }
123
124
        $this->rootPath = $this->absolutizePath($tenant->getSubdomain());
125
        $this->latestRootPath = $this->rootPath;
126
    }
127
128
    private function absolutizePath($path, $context = null)
129
    {
130
        if (null !== $context) {
131
            $context = $this->defaultRootPath.DIRECTORY_SEPARATOR.$context;
132
        }
133
134
        if (isset($path[0]) && '/' === $path[0]) {
135
            $path = $context ?: $this->rootPath;
136
        }
137
138
        return (string) PathHelper::absolutizePath($path, $context ?: $this->rootPath);
139
    }
140
141
    /**
142
     * Sets the RequestStack.
143
     *
144
     * @param RequestStack $requestStack
145
     */
146
    public function setRequestStack(RequestStack $requestStack)
147
    {
148
        $this->requestStack = $requestStack;
149
    }
150
151
    /**
152
     * Gets the RequestStack object.
153
     *
154
     * @return RequestStack
155
     */
156
    public function getRequestStack()
157
    {
158
        return $this->requestStack;
159
    }
160
}
161