Completed
Push — ezp_30981_content_info_proxy ( 6fc040...a78a98 )
by
unknown
15:33
created

ProxyDomainMapper::createContentProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\ProxyFactory;
10
11
use eZ\Publish\API\Repository\Repository;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14
use eZ\Publish\API\Repository\Values\Content\Language;
15
use eZ\Publish\API\Repository\Values\Content\Location;
16
use eZ\Publish\API\Repository\Values\Content\Section;
17
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
18
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup;
19
use eZ\Publish\API\Repository\Values\User\User;
20
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eZ\Publish\Core\Reposito...adingValueHolderFactory.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
use ProxyManager\Proxy\LazyLoadingInterface;
22
23
/**
24
 * @internal
25
 */
26
final class ProxyDomainMapper implements ProxyDomainMapperInterface
27
{
28
    /** @var \eZ\Publish\API\Repository\Repository */
29
    private $repository;
30
31
    /** @var \ProxyManager\Factory\LazyLoadingValueHolderFactory */
32
    private $factory;
33
34
    public function __construct(Repository $repository, LazyLoadingValueHolderFactory $lazyLoadingValueHolderFactory)
35
    {
36
        $this->repository = $repository;
37
        $this->factory = $lazyLoadingValueHolderFactory;
38
    }
39
40
    public function createContentProxy(
41
        int $contentId,
42
        array $prioritizedLanguages = Language::ALL,
43
        bool $useAlwaysAvailable = true
44
    ): Content {
45
        $initializer = function (
46
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
47
        ) use ($contentId, $prioritizedLanguages, $useAlwaysAvailable): bool {
48
            $initializer = null;
49
            $wrappedObject = $this->repository->getContentService()->loadContent(
50
                $contentId,
51
                $prioritizedLanguages,
52
                null,
53
                $useAlwaysAvailable
54
            );
55
56
            return true;
57
        };
58
59
        return $this->factory->createProxy(Content::class, $initializer);
60
    }
61
62
    public function createContentInfoProxy(int $contentId): ContentInfo
63
    {
64
        $initializer = function (
65
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
66
        ) use ($contentId): bool {
67
            $initializer = null;
68
            $wrappedObject = $this->repository->getContentService()->loadContentInfo(
69
                $contentId
70
            );
71
72
            return true;
73
        };
74
75
        return $this->factory->createProxy(ContentInfo::class, $initializer);
76
    }
77
78 View Code Duplication
    public function createContentTypeProxy(
79
        int $contentTypeId,
80
        array $prioritizedLanguages = Language::ALL
81
    ): ContentType {
82
        $initializer = function (
83
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
84
        ) use ($contentTypeId, $prioritizedLanguages): bool {
85
            $initializer = null;
86
            $wrappedObject = $this->repository->getContentTypeService()->loadContentType(
87
                $contentTypeId,
88
                $prioritizedLanguages
89
            );
90
91
            return true;
92
        };
93
94
        return $this->factory->createProxy(ContentType::class, $initializer);
95
    }
96
97 View Code Duplication
    public function createContentTypeGroupProxy(
98
        int $contentTypeGroupId,
99
        array $prioritizedLanguages = Language::ALL
100
    ): ContentTypeGroup {
101
        $initializer = function (
102
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
103
        ) use ($contentTypeGroupId, $prioritizedLanguages): bool {
104
            $initializer = null;
105
            $wrappedObject = $this->repository->getContentTypeService()->loadContentTypeGroup(
106
                $contentTypeGroupId,
107
                $prioritizedLanguages
108
            );
109
110
            return true;
111
        };
112
113
        return $this->factory->createProxy(ContentTypeGroup::class, $initializer);
114
    }
115
116
    public function createContentTypeGroupProxyList(
117
        array $contentTypeGroupIds,
118
        array $prioritizedLanguages = Language::ALL
119
    ): array {
120
        $groups = [];
121
        foreach ($contentTypeGroupIds as $contentTypeGroupId) {
122
            $groups[] = $this->createContentTypeGroupProxy($contentTypeGroupId, $prioritizedLanguages);
123
        }
124
125
        return $groups;
126
    }
127
128
    public function createLanguageProxy(string $languageCode): Language
129
    {
130
        $initializer = function (
131
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
132
        ) use ($languageCode): bool {
133
            $initializer = null;
134
            $wrappedObject = $this->repository->getContentLanguageService()->loadLanguage($languageCode);
135
136
            return true;
137
        };
138
139
        return $this->factory->createProxy(Language::class, $initializer);
140
    }
141
142 View Code Duplication
    public function createLocationProxy(
143
        int $locationId,
144
        array $prioritizedLanguages = Language::ALL
145
    ): Location {
146
        $initializer = function (
147
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
148
        ) use ($locationId, $prioritizedLanguages): bool {
149
            $initializer = null;
150
            $wrappedObject = $this->repository->getLocationService()->loadLocation(
151
                $locationId,
152
                $prioritizedLanguages
153
            );
154
155
            return true;
156
        };
157
158
        return $this->factory->createProxy(Location::class, $initializer);
159
    }
160
161
    public function createSectionProxy(int $sectionId): Section
162
    {
163
        $initializer = function (
164
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
165
        ) use ($sectionId): bool {
166
            $initializer = null;
167
            $wrappedObject = $this->repository->getSectionService()->loadSection($sectionId);
168
169
            return true;
170
        };
171
172
        return $this->factory->createProxy(Section::class, $initializer);
173
    }
174
175 View Code Duplication
    public function createUserProxy(int $userId, array $prioritizedLanguages = Language::ALL): User
176
    {
177
        $initializer = function (
178
            &$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer
179
        ) use ($userId, $prioritizedLanguages): bool {
180
            $initializer = null;
181
            $wrappedObject = $this->repository->getUserService()->loadUser($userId, $prioritizedLanguages);
182
183
            return true;
184
        };
185
186
        return $this->factory->createProxy(User::class, $initializer);
187
    }
188
}
189