TenantAwarePathBuilder::absolutizePath()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 2
dl 0
loc 12
rs 9.2222
c 0
b 0
f 0
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
15
namespace SWP\Component\MultiTenancy\PathBuilder;
16
17
use PHPCR\Util\PathHelper;
18
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
19
20
/**
21
 * TenantAwarePathBuilder class.
22
 */
23
class TenantAwarePathBuilder implements TenantAwarePathBuilderInterface
24
{
25
    /**
26
     * @var TenantContextInterface
27
     */
28
    protected $tenantContext;
29
30
    /**
31
     * @var string
32
     */
33
    protected $rootPath;
34
35
    /**
36
     * @var string
37
     */
38
    protected $latestRootPath;
39
40
    /**
41
     * @var string
42
     */
43
    protected $defaultRootPath;
44
45
    /**
46
     * Construct.
47
     *
48
     * @param TenantContextInterface $tenantContext Tenant context
49
     * @param string                 $rootPath      PHPCR root path (e.g. /swp)
50
     */
51
    public function __construct(TenantContextInterface $tenantContext, $rootPath)
52
    {
53
        $this->tenantContext = $tenantContext;
54
        $this->rootPath = $rootPath;
55
        $this->defaultRootPath = $rootPath;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function build($data, $context = null)
62
    {
63
        return $this->absolutize($data, $context);
64
    }
65
66
    /**
67
     * Absolutize path or paths based on current context when provided.
68
     *
69
     * @param string|array $data    Path or array of paths
70
     * @param string       $context Path absolute context
71
     *
72
     * @return string|array Tenant aware paths
73
     */
74
    protected function absolutize($data, $context = null)
75
    {
76
        $this->makePathTenantAware();
77
        if (is_array($data)) {
78
            $tenantAwarePaths = [];
79
            foreach ($data as $path) {
80
                $tenantAwarePaths[] = $this->absolutizePath($path, $context);
81
            }
82
83
            return $tenantAwarePaths;
84
        }
85
86
        return $this->absolutizePath($data, $context);
87
    }
88
89
    /**
90
     * Makes PHPCR tree root path to be tenant aware.
91
     *
92
     * When tenant is not available in tenant context it will resolve
93
     * the tenant from current request.
94
     */
95
    protected function makePathTenantAware()
96
    {
97
        if ($this->latestRootPath === $this->rootPath) {
98
            return;
99
        }
100
101
        $tenant = $this->tenantContext->getTenant();
102
        $path = $tenant->getCode();
103
104
        if (null !== $tenant->getOrganization()) {
105
            $path = $tenant->getOrganization()->getCode().'/'.$path;
106
        }
107
108
        $this->rootPath = $this->absolutizePath($path);
109
        $this->latestRootPath = $this->rootPath;
110
    }
111
112
    private function absolutizePath($path, $context = null)
113
    {
114
        if (null !== $context) {
115
            $context = $this->defaultRootPath.DIRECTORY_SEPARATOR.$context;
116
        }
117
118
        if (isset($path[0]) && '/' === $path[0]) {
119
            $path = $context ?: $this->rootPath;
120
        }
121
122
        return (string) PathHelper::absolutizePath($path, $context ?: $this->rootPath);
123
    }
124
}
125