Completed
Push — sf_multi_get ( c68a81 )
by André
25:43 queued 12:06
created

ObjectStateHandler::loadObjectStates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ObjectStateHandler implementation.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use eZ\Publish\SPI\Persistence\Content\ObjectState\Handler as ObjectStateHandlerInterface;
12
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct;
13
14
/**
15
 * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler
16
 */
17
class ObjectStateHandler extends AbstractHandler implements ObjectStateHandlerInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function createGroup(InputStruct $input)
23
    {
24
        $this->logger->logCall(__METHOD__, array('struct' => $input));
25
        $group = $this->persistenceHandler->objectStateHandler()->createGroup($input);
26
27
        $this->cache->deleteItem('ez-state-group-all');
28
29
        return $group;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function loadGroup($groupId)
36
    {
37
        $cacheItem = $this->cache->getItem('ez-state-group-'.$groupId);
38
        if ($cacheItem->isHit()) {
39
            return $cacheItem->get();
40
        }
41
42
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
43
        $group = $this->persistenceHandler->objectStateHandler()->loadGroup($groupId);
44
45
        $cacheItem->set($group);
46
        $cacheItem->tag(['state-group-'.$group->id]);
47
        $this->cache->save($cacheItem);
48
49
        return $group;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function loadGroupByIdentifier($identifier)
56
    {
57
        $cacheItem = $this->cache->getItem('ez-state-group-'.$identifier.'-by-identifier');
58
        if ($cacheItem->isHit()) {
59
            return $cacheItem->get();
60
        }
61
62
        $this->logger->logCall(__METHOD__, array('groupId' => $identifier));
63
        $group = $this->persistenceHandler->objectStateHandler()->loadGroupByIdentifier($identifier);
64
65
        $cacheItem->set($group);
66
        $cacheItem->tag(['state-group-'.$group->id]);
67
        $this->cache->save($cacheItem);
68
69
        return $group;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function loadAllGroups($offset = 0, $limit = -1)
76
    {
77
        $cacheItem = $this->cache->getItem('ez-state-group-all');
78
        if ($cacheItem->isHit()) {
79
            return array_slice($cacheItem->get(), $offset, $limit > -1 ? $limit: null);
80
        }
81
82
        $this->logger->logCall(__METHOD__, array('offset' => $offset, 'limit' => $limit));
83
        $stateGroups = $this->persistenceHandler->objectStateHandler()->loadAllGroups(0, -1);
84
85
        $cacheItem->set($stateGroups);
86
        $cacheTags = [];
87
        foreach ($stateGroups as $group) {
88
            $cacheTags[] = 'state-group-'.$group->id;
89
        }
90
        $cacheItem->tag($cacheTags);
91
        $this->cache->save($cacheItem);
92
93
        return array_slice($stateGroups, $offset, $limit > -1 ? $limit: null);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function loadObjectStates($groupId)
100
    {
101
        $cacheItem = $this->cache->getItem('ez-state-list-'.$groupId.'-by-group');
102
        if ($cacheItem->isHit()) {
103
            return $cacheItem->get();
104
        }
105
106
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
107
        $objectStates = $this->persistenceHandler->objectStateHandler()->loadObjectStates($groupId);
108
109
        $cacheItem->set($objectStates);
110
        $cacheTags = ['state-group-'.$groupId];
111
        foreach ($objectStates as $state) {
112
            $cacheTags[] = 'state-'.$state->id;
113
        }
114
        $cacheItem->tag($cacheTags);
115
        $this->cache->save($cacheItem);
116
117
        return $objectStates;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function updateGroup($groupId, InputStruct $input)
124
    {
125
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
126
        $return = $this->persistenceHandler->objectStateHandler()->updateGroup($groupId, $input);
127
128
        $this->cache->invalidateTags(['state-group-'.$groupId]);
129
130
        return $return;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function deleteGroup($groupId)
137
    {
138
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
139
        $return = $this->persistenceHandler->objectStateHandler()->deleteGroup($groupId);
140
141
        $this->cache->invalidateTags(['state-group-'.$groupId]);
142
143
        return $return;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function create($groupId, InputStruct $input)
150
    {
151
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
152
        $return = $this->persistenceHandler->objectStateHandler()->create($groupId, $input);
153
154
        $this->cache->deleteItem('ez-state-list-by-group-'.$groupId);
155
156
        return $return;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 View Code Duplication
    public function load($stateId)
163
    {
164
        $cacheItem = $this->cache->getItem('ez-state-'.$stateId);
165
        if ($cacheItem->isHit()) {
166
            return $cacheItem->get();
167
        }
168
169
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
170
        $objectState = $this->persistenceHandler->objectStateHandler()->load($stateId);
171
172
        $cacheItem->set($objectState);
173
        $cacheItem->tag(['state-'.$objectState->id,'state-group-'.$objectState->groupId]);
174
        $this->cache->save($cacheItem);
175
176
        return $objectState;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 View Code Duplication
    public function loadByIdentifier($identifier, $groupId)
183
    {
184
185
        $cacheItem = $this->cache->getItem('ez-state-identifier-'.$identifier.'-by-group-'.$groupId);
186
        if ($cacheItem->isHit()) {
187
            return $cacheItem->get();
188
        }
189
190
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier, 'groupId' => $groupId));
191
        $objectState = $this->persistenceHandler->objectStateHandler()->loadByIdentifier($identifier, $groupId);
192
193
        $cacheItem->set($objectState);
194
        $cacheItem->tag(['state-'.$objectState->id,'state-group-'.$objectState->groupId]);
195
        $this->cache->save($cacheItem);
196
197
        return $objectState;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function update($stateId, InputStruct $input)
204
    {
205
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'struct' => $input));
206
        $return = $this->persistenceHandler->objectStateHandler()->update($stateId, $input);
207
208
        $this->cache->invalidateTags(['state-'.$stateId]);
209
210
        return $return;
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function setPriority($stateId, $priority)
217
    {
218
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'priority' => $priority));
219
        $return = $this->persistenceHandler->objectStateHandler()->setPriority($stateId, $priority);
220
221
        $this->cache->invalidateTags(['state-'.$stateId]);
222
223
        return $return;
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function delete($stateId)
230
    {
231
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
232
        $return = $this->persistenceHandler->objectStateHandler()->delete($stateId);
233
234
        $this->cache->invalidateTags(['state-'.$stateId]);
235
236
        return $return;
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function setContentState($contentId, $groupId, $stateId)
243
    {
244
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'groupId' => $groupId, 'stateId' => $stateId));
245
        $return = $this->persistenceHandler->objectStateHandler()->setContentState($contentId, $groupId, $stateId);
246
247
        $this->cache->deleteItem('ez-state-by-group-'.$groupId.'-on-content-'.$contentId);
248
249
        return $return;
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 View Code Duplication
    public function getContentState($contentId, $stateGroupId)
256
    {
257
        $cacheItem = $this->cache->getItem('ez-state-by-group-'.$stateGroupId.'-on-content-'.$contentId);
258
        if ($cacheItem->isHit()) {
259
            return $cacheItem->get();
260
        }
261
262
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'stateGroupId' => $stateGroupId));
263
        $contentState = $this->persistenceHandler->objectStateHandler()->getContentState($contentId, $stateGroupId);
264
265
        $cacheItem->set($contentState);
266
        $cacheItem->tag(['state-'.$contentState->id, 'content-'.$contentId]);
267
        $this->cache->save($cacheItem);
268
269
        return $contentState;
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function getContentCount($stateId)
276
    {
277
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
278
279
        return $this->persistenceHandler->objectStateHandler()->getContentCount($stateId);
280
    }
281
}
282