Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

ObjectStateHandler::loadAllGroups()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 2
dl 0
loc 23
rs 8.9297
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
        // Method caches all state groups in cache only uses offset / limit to slice the cached result
64
        $cache = $this->cache->getItem('objectstategroup', 'all');
65
        $stateGroups = $cache->get();
66
        if ($cache->isMiss()) {
67
            $this->logger->logCall(__METHOD__, array('offset' => $offset, 'limit' => $limit));
68
            $stateGroups = $this->persistenceHandler->objectStateHandler()->loadAllGroups(0, -1);
69
            $cache->set($stateGroups)->save();
70
            $stateGroups = array_slice($stateGroups, $offset, $limit > -1 ?: null);
71
        } else {
72
            $stateGroups = array_slice($stateGroups, $offset, $limit > -1 ?: null);
73
            // BC for updates to 6.7LTS installs where cache contains ID's and not objects
74
            // @todo Remove in later branches
75
            foreach ($stateGroups as $key => $stateGroup) {
76
                if (is_numeric($stateGroup)) {
77
                    $stateGroups[$key] = $this->loadGroup($stateGroup);
78
                }
79
            }
80
        }
81
82
        return $stateGroups;
83
    }
84
85
    /**
86
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates
87
     */
88
    public function loadObjectStates($groupId)
89
    {
90
        $cache = $this->cache->getItem('objectstate', 'byGroup', $groupId);
91
        $objectStates = $cache->get();
92
        if ($cache->isMiss()) {
93
            $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
94
            $objectStates = $this->persistenceHandler->objectStateHandler()->loadObjectStates($groupId);
95
            $cache->set($objectStates)->save();
96
        } else {
97
            // BC for updates to 6.7LTS installs where cache contains ID's and not objects
98
            // @todo Remove in later branches
99
            foreach ($objectStates as $key => $state) {
100
                if (is_numeric($state)) {
101
                    $objectStates[$key] = $this->load($state);
102
                }
103
            }
104
        }
105
106
        return $objectStates;
107
    }
108
109
    /**
110
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::updateGroup
111
     */
112 View Code Duplication
    public function updateGroup($groupId, InputStruct $input)
113
    {
114
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
115
        $return = $this->persistenceHandler->objectStateHandler()->updateGroup($groupId, $input);
116
117
        $this->cache->clear('objectstategroup', 'all');
118
        $this->cache->clear('objectstategroup', $groupId);
119
120
        return $return;
121
    }
122
123
    /**
124
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::deleteGroup
125
     */
126 View Code Duplication
    public function deleteGroup($groupId)
127
    {
128
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
129
        $return = $this->persistenceHandler->objectStateHandler()->deleteGroup($groupId);
130
131
        $this->cache->clear('objectstategroup', 'all');
132
        $this->cache->clear('objectstategroup', $groupId);
133
        $this->cache->clear('objectstate', 'byGroup', $groupId);
134
135
        return $return;
136
    }
137
138
    /**
139
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::create
140
     */
141
    public function create($groupId, InputStruct $input)
142
    {
143
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
144
        $return = $this->persistenceHandler->objectStateHandler()->create($groupId, $input);
145
146
        $this->cache->clear('objectstate', 'byGroup', $groupId);
147
148
        return $return;
149
    }
150
151
    /**
152
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load
153
     */
154 View Code Duplication
    public function load($stateId)
155
    {
156
        $cache = $this->cache->getItem('objectstate', $stateId);
157
        $objectState = $cache->get();
158
        if ($cache->isMiss()) {
159
            $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
160
            $cache->set($objectState = $this->persistenceHandler->objectStateHandler()->load($stateId))->save();
161
        }
162
163
        return $objectState;
164
    }
165
166
    /**
167
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadByIdentifier
168
     */
169
    public function loadByIdentifier($identifier, $groupId)
170
    {
171
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier, 'groupId' => $groupId));
172
173
        return $this->persistenceHandler->objectStateHandler()->loadByIdentifier($identifier, $groupId);
174
    }
175
176
    /**
177
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::update
178
     */
179 View Code Duplication
    public function update($stateId, InputStruct $input)
180
    {
181
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'struct' => $input));
182
        $return = $this->persistenceHandler->objectStateHandler()->update($stateId, $input);
183
184
        $this->cache->clear('objectstate', $stateId);
185
        $this->cache->clear('objectstate', 'byGroup');
186
187
        return $return;
188
    }
189
190
    /**
191
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setPriority
192
     */
193
    public function setPriority($stateId, $priority)
194
    {
195
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'priority' => $priority));
196
        $return = $this->persistenceHandler->objectStateHandler()->setPriority($stateId, $priority);
197
198
        $this->cache->clear('objectstate', $stateId);
199
200
        return $return;
201
    }
202
203
    /**
204
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::delete
205
     */
206
    public function delete($stateId)
207
    {
208
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
209
        $return = $this->persistenceHandler->objectStateHandler()->delete($stateId);
210
211
        $this->cache->clear('objectstate', $stateId);
212
        $this->cache->clear('objectstate', 'byGroup'); // TIMBER!
213
214
        return $return;
215
    }
216
217
    /**
218
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setContentState
219
     */
220 View Code Duplication
    public function setContentState($contentId, $groupId, $stateId)
221
    {
222
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'groupId' => $groupId, 'stateId' => $stateId));
223
        $return = $this->persistenceHandler->objectStateHandler()->setContentState($contentId, $groupId, $stateId);
224
225
        $this->cache->clear('objectstate', 'byContent', $contentId, $groupId);
226
227
        return $return;
228
    }
229
230
    /**
231
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState
232
     */
233 View Code Duplication
    public function getContentState($contentId, $stateGroupId)
234
    {
235
        $cache = $this->cache->getItem('objectstate', 'byContent', $contentId, $stateGroupId);
236
        $stateId = $cache->get();
237
        if ($cache->isMiss()) {
238
            $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'stateGroupId' => $stateGroupId));
239
240
            $contentState = $this->persistenceHandler->objectStateHandler()->getContentState($contentId, $stateGroupId);
241
            $cache->set($contentState->id)->save();
242
243
            return $contentState;
244
        }
245
246
        return $this->load($stateId);
247
    }
248
249
    /**
250
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentCount
251
     *
252
     * @todo cache results
253
     */
254
    public function getContentCount($stateId)
255
    {
256
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
257
258
        return $this->persistenceHandler->objectStateHandler()->getContentCount($stateId);
259
    }
260
}
261