Repository::loadEntityFromLocationRemoteId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Kaliop\eZObjectWrapperBundle\Core;
4
5
use eZ\Publish\API\Repository\Repository as eZRepository;
6
use eZ\Publish\API\Repository\Values\Content\Location;
7
use eZ\Publish\API\Repository\Values\Content\Content;
8
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
9
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * Typehint methods we expose from Logger and Repository using magic calls. NB: to be adjusted manually if those change...
14
 *
15
 * @method null emergency($message, array $context = array())
16
 * @method null alert($message, array $context = array())
17
 * @method null critical($message, array $context = array())
18
 * @method null error($message, array $context = array())
19
 * @method null warning($message, array $context = array())
20
 * @method null notice($message, array $context = array())
21
 * @method null info($message, array $context = array())
22
 * @method null debug($message, array $context = array())
23
 * @method null log($message, array $context = array())
24
 *
25
 * @method \eZ\Publish\API\Repository\ContentService getContentService()
26
 * @method \eZ\Publish\API\Repository\LanguageService getContentLanguageService()
27
 * @method \eZ\Publish\API\Repository\ContentTypeService getContentTypeService()
28
 * @method \eZ\Publish\API\Repository\LocationService getLocationService()
29
 * @method \eZ\Publish\API\Repository\TrashService getTrashService()
30
 * @method \eZ\Publish\API\Repository\SectionService getSectionService()
31
 * @method \eZ\Publish\API\Repository\SearchService getSearchService()
32
 * @method \eZ\Publish\API\Repository\UserService getUserService()
33
 * @method \eZ\Publish\API\Repository\URLAliasService getURLAliasService()
34
 * @method \eZ\Publish\API\Repository\URLWildcardService getURLWildcardService()
35
 * @method \eZ\Publish\API\Repository\ObjectStateService getObjectStateService()
36
 * @method \eZ\Publish\API\Repository\RoleService getRoleService()
37
 * @method \eZ\Publish\API\Repository\FieldTypeService getFieldTypeService()
38
 *
39
 * @method \eZ\Publish\API\Repository\Values\User\User getCurrentUser()
40
 * @method null setCurrentUser(\eZ\Publish\API\Repository\Values\User\User $user)
41
 * @method array|null hasAccess($module, $function, \eZ\Publish\API\Repository\Values\User\User $user = null)
42
 * @method bool canUser($module, $function, \eZ\Publish\API\Repository\Values\ValueObject $object, $targets = null)
43
 *
44
 * @method null beginTransaction()
45
 * @method null commit()
46
 * @method null rollback()
47
 *
48
 * @todo we could add simple methods like findAll() and fetch($offset, $limit) which simply filter based on contentType
49
 */
50
class Repository implements RepositoryInterface
51
{
52
    // Name of the php class used to create entities. Subclasses have to set a value to this, to be able to create entities
53
    protected $entityClass;
54
55
    /** @var eZRepository $repository */
56
    protected $repository;
57
    protected $entityManager;
58
    /** @var string $contentTypeIdentifier */
59
    protected $contentTypeIdentifier;
60
    /** @var array $settings */
61 10
    protected $settings;
62
    /** @var LoggerInterface $logger */
63 10
    protected $logger;
64 10
65 10
    public function __construct(eZRepository $repository, $entityManager, array $settings=array(), $contentTypeIdentifier='')
66 10
    {
67 10
        $this->repository = $repository;
68
        $this->entityManager = $entityManager;
69 10
        $this->settings = $this->validateSettings($settings);
70
        $this->contentTypeIdentifier = $contentTypeIdentifier;
71 10
    }
72 10
73
    /**
74
     * @param string $contentTypeIdentifier
75
     * @return $this|RepositoryInterface
76
     * @todo we could disallow setting a $contentTypeIdentifier when it was already set...
77
     */
78
    public function setContentTypeIdentifier($contentTypeIdentifier)
79
    {
80
        $this->contentTypeIdentifier = $contentTypeIdentifier;
81 10
        return $this;
82
    }
83 10
84 10
    public function getContentTypeIdentifier()
85
    {
86
        return $this->contentTypeIdentifier;
87
    }
88
89
    public function setSettings(array $settings)
90
    {
91
        $this->settings = $this->validateSettings($settings);
92
        return $this;
93
    }
94
95 10
    public function setLogger(LoggerInterface $logger=null)
96
    {
97 10
        $this->logger = $logger;
98
        return $this;
99
    }
100
101
    /**
102
     * Called from the constructor, with the settings received from the caller.
103
     * Subclasses can implement checking here, or merge the received settings with other data, using f.e. the Symfony
104
     * OptionsResolver component (see http://symfony.com/doc/current/components/options_resolver.html).
105
     *
106
     * @param array $settings
107
     * @return array
108
     */
109
    protected function validateSettings(array $settings)
110 10
    {
111
        return $settings;
112
    }
113 10
114 10
    /**
115 10
     * Nice syntactic sugar ( manually typehinted :-) )
116 10
     * Allow all logger methods and eZ repo methods to be called on this extended repo
117 10
     *
118 10
     * @param string $method
119 10
     * @param array $args
120 10
     * @return mixed
121 10
     *
122
     * @todo !important move this method to protected access?
123
     */
124
    public function __call($method, $args)
125
    {
126
        switch($method) {
127 10
            case 'emergency':
128 10
            case 'alert':
129 10
            case 'critical':
130
            case 'error':
131
            case 'warning':
132
            case 'notice':
133
            case 'info':
134
            case 'debug':
135
            case 'log':
136
                if ($this->logger) {
137
                    return call_user_func_array(array($this->logger, $method), $args);
138
                }
139 10
                // if no logger is defined, swallow the method call
140
                return;
141 10
            default:
142
                return call_user_func_array(array($this->repository, $method), $args);
143
        }
144 10
    }
145 10
146 10
    /**
147 10
     * To be overridden in subclasses, this method allows injecting extra services/settings in the entities created.
148
     * This version 'knows' about EntityManagerAware and Logging entity traits.
149
     *
150
     * @param \Kaliop\eZObjectWrapperBundle\Core\EntityInterface $entity
151
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
152
     */
153
    protected function enrichEntityAtLoad($entity)
154
    {
155
        if (is_callable(array($entity, 'setLogger'))) {
156 10
            $entity->setLogger($this->logger);
157
        }
158 10
        if (is_callable(array($entity, 'setEntityManager'))) {
159 10
            $entity->setEntityManager($this->entityManager);
160 10
        }
161
        return $entity;
162
    }
163
164
    /**
165
     * @param Content $content
166
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
167 4
     *
168
     * @todo optionally (?) throw an error if bad content type is detected
169 4
     */
170 4
    public function loadEntityFromContent(Content $content)
171 4
    {
172
        $class = $this->entityClass;
173
        $entity = new $class($this->repository, $content, null);
174
        return $this->enrichEntityAtLoad($entity);
175
    }
176
177
    /**
178 2
     * @param Location $location
179
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
180 2
     */
181
    public function loadEntityFromLocation(Location $location)
182
    {
183
        $class = $this->entityClass;
184
        $entity = new $class($this->repository, null, $location);
185
        return $this->enrichEntityAtLoad($entity);
186
    }
187
188
    /**
189 10
     * This method is useful f.e. when you want to create an Entity that matches a given version and specific location.
190
     * This happens notably when doing content previews, where eZ will inject into your controllers both of them.
191 10
     *
192
     * @param Content $content
193
     * @param Location $location
194
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
195
     */
196
    public function loadEntityFromContentAndLocation(Content $content, Location $location)
197
    {
198
        $class = $this->entityClass;
199
        $entity = new $class($this->repository, $content, $location);
200
        return $this->enrichEntityAtLoad($entity);
201 10
    }
202
203 10
    /**
204
     * @param ContentInfo $contentInfo
205
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
206
     */
207
    public function loadEntityFromContentInfo(ContentInfo $contentInfo)
208
    {
209
        return $this->loadEntityFromContent($this->getContentService()->loadContentByContentInfo($contentInfo));
210
    }
211
212 1
    /**
213
     * @param int $id
214 1
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
215
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist
216
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions
217
     */
218
    public function loadEntityFromContentId($id)
219
    {
220
        return $this->loadEntityFromContent($this->getContentService()->loadContent($id));
221
    }
222
223 1
    /**
224
     * An alias for loadEntityFromContentId, to keep the API close to Doctrine
225 1
     * @param int $id
226
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
227
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist
228
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions
229
     */
230
    public function find($id)
231
    {
232
        return $this->loadEntityFromContentId($id);
233
    }
234 1
235
    /**
236 1
     * @param int $id
237
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
238
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location
239
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found
240
     */
241
    public function loadEntityFromLocationId($id)
242
    {
243
        return $this->loadEntityFromLocation($this->getLocationService()->loadLocation($id));
244
    }
245
246
    /**
247
     * @param string $remoteId
248
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
249
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content with the given id does not exist
250
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions
251
     */
252
    public function loadEntityFromContentRemoteId($remoteId)
253
    {
254
        return $this->loadEntityFromContent($this->getContentService()->loadContentByRemoteId($remoteId));
255
    }
256
257
    /**
258
     * @param string $remoteId
259
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface
260
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location
261
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found
262
     */
263
    public function loadEntityFromLocationRemoteId($remoteId)
264
    {
265
        return $this->loadEntityFromLocation($this->getLocationService()->loadLocationByRemoteId($remoteId));
266
    }
267
268
    /**
269
     * NB: assumes that all search results are homogeneous (same content type)
270
     * @param SearchResult $searchResult
271
     * @return \Kaliop\eZObjectWrapperBundle\Core\EntityInterface[]
272
     */
273
    protected function loadEntitiesFromSearchResults(SearchResult $searchResult)
274
    {
275
        $entities = array();
276
        foreach ($searchResult->searchHits as $searchHit) {
277
            // let's hope that in the future eZPublish does not add new types of results to SearchResult... :-P
278
            if ($searchHit->valueObject instanceof \eZ\Publish\API\Repository\Values\Content\Location) {
279
                $entities[] = $this->loadEntityFromLocation($searchHit->valueObject);
280
            } else {
281
                $entities[] = $this->loadEntityFromContent($searchHit->valueObject);
0 ignored issues
show
Compatibility introduced by
$searchHit->valueObject of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...Values\Content\Content>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\ValueObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
282
            }
283
        }
284
        return $entities;
285
    }
286
}
287