Completed
Push — 1.4 ( f2fa86...1c2218 )
by Paweł
08:49
created

ScopeContext::getScopesOwners()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2017 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 2017 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Context;
16
17
use SWP\Bundle\CoreBundle\Theme\Model\ThemeInterface;
18
use SWP\Bundle\SettingsBundle\Context\ScopeContext as BaseScopeContext;
19
use SWP\Bundle\SettingsBundle\Model\SettingsOwnerInterface;
20
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
21
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
22
23
class ScopeContext extends BaseScopeContext implements ScopeContextInterface
24
{
25
    const NEW_SCOPES = [
26
        ScopeContextInterface::SCOPE_ORGANIZATION,
27
        ScopeContextInterface::SCOPE_TENANT,
28
        ScopeContextInterface::SCOPE_THEME,
29
    ];
30
31
    private $tenantContent;
32
33
    private $themeContext;
34
35
    public function __construct(TenantContextInterface $tenantContext, ThemeContextInterface $themeContext)
36
    {
37
        $this->tenantContent = $tenantContext;
38
        $this->themeContext = $themeContext;
39
    }
40
41
    public function getScopes(): array
42
    {
43
        return array_merge(parent::getScopes(), self::NEW_SCOPES);
44
    }
45
46
    public function getScopesOwners(): array
47
    {
48
        foreach ($this->getScopes() as $scope) {
49
            $this->lazyLoad($scope);
50
        }
51
52
        return parent::getScopesOwners();
53
    }
54
55
    public function getScopeOwner(string $scope)
56
    {
57
        $this->lazyLoad($scope);
58
59
        return parent::getScopeOwner($scope);
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::getScopeOwner($scope); of type SWP\Bundle\SettingsBundl...sOwnerInterface|boolean adds the type boolean to the return on line 59 which is incompatible with the return type declared by the interface SWP\Bundle\SettingsBundl...nterface::getScopeOwner of type null|SWP\Bundle\Settings...\SettingsOwnerInterface.
Loading history...
60
    }
61
62
    private function lazyLoad(string $scope)
63
    {
64
        if (in_array($scope, self::NEW_SCOPES)) {
65
            $tenant = $this->tenantContent->getTenant();
66
            if ($tenant instanceof SettingsOwnerInterface) {
67
                if (ScopeContextInterface::SCOPE_TENANT === $scope) {
68
                    $this->setScopeOwner(ScopeContextInterface::SCOPE_TENANT, $tenant);
69
                } elseif (ScopeContextInterface::SCOPE_ORGANIZATION === $scope) {
70
                    $this->setScopeOwner(ScopeContextInterface::SCOPE_ORGANIZATION, $tenant->getOrganization());
71
                } elseif (ScopeContextInterface::SCOPE_THEME === $scope) {
72
                    if ($this->themeContext->getTheme() instanceof ThemeInterface) {
73
                        $this->setScopeOwner(ScopeContextInterface::SCOPE_THEME, $tenant);
74
                    }
75
                }
76
            }
77
        }
78
    }
79
}
80