Completed
Push — state_lookup_optimizations ( 3185f4 )
by André
27:58 queued 15:00
created

ObjectStateHandler::loadObjectStates()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 21
rs 9.0534
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
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::createGroup
21
     */
22 View Code Duplication
    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->clear('objectstategroup', 'all');
28
        $this->cache->getItem('objectstategroup', $group->id)->set($group)->save();
29
30
        return $group;
31
    }
32
33
    /**
34
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadGroup
35
     */
36 View Code Duplication
    public function loadGroup($groupId)
37
    {
38
        $cache = $this->cache->getItem('objectstategroup', $groupId);
39
        $group = $cache->get();
40
        if ($cache->isMiss()) {
41
            $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
42
            $cache->set($group = $this->persistenceHandler->objectStateHandler()->loadGroup($groupId))->save();
43
        }
44
45
        return $group;
46
    }
47
48
    /**
49
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadGroupByIdentifier
50
     */
51
    public function loadGroupByIdentifier($identifier)
52
    {
53
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier));
54
55
        return $this->persistenceHandler->objectStateHandler()->loadGroupByIdentifier($identifier);
56
    }
57
58
    /**
59
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
60
     */
61
    public function loadAllGroups($offset = 0, $limit = -1)
62
    {
63
        $cache = $this->cache->getItem('objectstategroup', 'all');
64
        $groupIds = $cache->get();
65
        if ($cache->isMiss()) {
66
            $this->logger->logCall(__METHOD__, array('offset' => $offset, 'limit' => $limit));
67
            $stateGroups = $this->persistenceHandler->objectStateHandler()->loadAllGroups(0, -1);
68
69
            $groupIds = array();
70
            foreach ($stateGroups as $objectStateGroup) {
71
                $groupCache = $this->cache->getItem('objectstategroup', $objectStateGroup->id);
72
                $groupCache->set($objectStateGroup)->save();
73
                $groupIds[] = $objectStateGroup->id;
74
            }
75
76
            $cache->set($groupIds)->save();
77
            $stateGroups = array_slice($stateGroups, $offset, $limit > -1 ?: null);
78
        } else {
79
            $groupIds = array_slice($groupIds, $offset, $limit > -1 ?: null);
80
            $stateGroups = array();
81
            foreach ($groupIds as $groupId) {
82
                $stateGroups[] = $this->loadGroup($groupId);
83
            }
84
        }
85
86
        return $stateGroups;
87
    }
88
89
    /**
90
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates
91
     */
92
    public function loadObjectStates($groupId)
93
    {
94
        $cache = $this->cache->getItem('objectstate', 'byGroup', $groupId);
95
        $objectStates = $cache->get();
96
        if ($cache->isMiss()) {
97
            $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
98
99
            $objectStates = $this->persistenceHandler->objectStateHandler()->loadObjectStates($groupId);
100
            $cache->set($objectStates)->save();
101
        } else {
102
            // BC for updates to 6.7LTS installs where cache contains ID's and not objects
103
            // @todo Remove in later branches
104
            foreach ($objectStates as $key => $state) {
105
                if (is_numeric($state)) {
106
                    $objectStates[$key] = $this->load($state);
107
                }
108
            }
109
        }
110
111
        return $objectStates;
112
    }
113
114
    /**
115
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::updateGroup
116
     */
117
    public function updateGroup($groupId, InputStruct $input)
118
    {
119
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
120
        $return = $this->persistenceHandler->objectStateHandler()->updateGroup($groupId, $input);
121
122
        $this->cache->clear('objectstategroup', $groupId);
123
124
        return $return;
125
    }
126
127
    /**
128
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::deleteGroup
129
     */
130 View Code Duplication
    public function deleteGroup($groupId)
131
    {
132
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
133
        $return = $this->persistenceHandler->objectStateHandler()->deleteGroup($groupId);
134
135
        $this->cache->clear('objectstategroup', 'all');
136
        $this->cache->clear('objectstategroup', $groupId);
137
        $this->cache->clear('objectstate', 'byGroup', $groupId);
138
139
        return $return;
140
    }
141
142
    /**
143
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::create
144
     */
145
    public function create($groupId, InputStruct $input)
146
    {
147
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
148
        $return = $this->persistenceHandler->objectStateHandler()->create($groupId, $input);
149
150
        $this->cache->clear('objectstate', 'byGroup', $groupId);
151
152
        return $return;
153
    }
154
155
    /**
156
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load
157
     */
158 View Code Duplication
    public function load($stateId)
159
    {
160
        $cache = $this->cache->getItem('objectstate', $stateId);
161
        $objectState = $cache->get();
162
        if ($cache->isMiss()) {
163
            $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
164
            $cache->set($objectState = $this->persistenceHandler->objectStateHandler()->load($stateId))->save();
165
        }
166
167
        return $objectState;
168
    }
169
170
    /**
171
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadByIdentifier
172
     */
173
    public function loadByIdentifier($identifier, $groupId)
174
    {
175
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier, 'groupId' => $groupId));
176
177
        return $this->persistenceHandler->objectStateHandler()->loadByIdentifier($identifier, $groupId);
178
    }
179
180
    /**
181
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::update
182
     */
183
    public function update($stateId, InputStruct $input)
184
    {
185
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'struct' => $input));
186
        $return = $this->persistenceHandler->objectStateHandler()->update($stateId, $input);
187
188
        $this->cache->clear('objectstate', $stateId);
189
190
        return $return;
191
    }
192
193
    /**
194
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setPriority
195
     */
196
    public function setPriority($stateId, $priority)
197
    {
198
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'priority' => $priority));
199
        $return = $this->persistenceHandler->objectStateHandler()->setPriority($stateId, $priority);
200
201
        $this->cache->clear('objectstate', $stateId);
202
203
        return $return;
204
    }
205
206
    /**
207
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::delete
208
     */
209
    public function delete($stateId)
210
    {
211
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
212
        $return = $this->persistenceHandler->objectStateHandler()->delete($stateId);
213
214
        $this->cache->clear('objectstate', $stateId);
215
        $this->cache->clear('objectstate', 'byGroup'); // TIMBER!
216
217
        return $return;
218
    }
219
220
    /**
221
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setContentState
222
     */
223 View Code Duplication
    public function setContentState($contentId, $groupId, $stateId)
224
    {
225
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'groupId' => $groupId, 'stateId' => $stateId));
226
        $return = $this->persistenceHandler->objectStateHandler()->setContentState($contentId, $groupId, $stateId);
227
228
        $this->cache->clear('objectstate', 'byContent', $contentId, $groupId);
229
230
        return $return;
231
    }
232
233
    /**
234
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState
235
     */
236
    public function getContentState($contentId, $stateGroupId)
237
    {
238
        $cache = $this->cache->getItem('objectstate', 'byContent', $contentId, $stateGroupId);
239
        $stateId = $cache->get();
240 View Code Duplication
        if ($cache->isMiss()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
            $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'stateGroupId' => $stateGroupId));
242
243
            $contentState = $this->persistenceHandler->objectStateHandler()->getContentState($contentId, $stateGroupId);
244
            $cache->set($contentState->id)->save();
245
246
            return $contentState;
247
        }
248
249
        return $this->load($stateId);
250
    }
251
252
    /**
253
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentCount
254
     *
255
     * @todo cache results
256
     */
257
    public function getContentCount($stateId)
258
    {
259
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
260
261
        return $this->persistenceHandler->objectStateHandler()->getContentCount($stateId);
262
    }
263
}
264