Completed
Push — 6.11 ( bc10f7...606f7b )
by André
17:52 queued 02:42
created

ObjectStateHandler::loadObjectStates()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 20
rs 9.2
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 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
     * {@inheritdoc}
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
     * {@inheritdoc}
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
     * {@inheritdoc}
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
     * {@inheritdoc}
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
     * {@inheritdoc}
111
     */
112
    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', $groupId);
118
119
        return $return;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 View Code Duplication
    public function deleteGroup($groupId)
126
    {
127
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
128
        $return = $this->persistenceHandler->objectStateHandler()->deleteGroup($groupId);
129
130
        $this->cache->clear('objectstategroup', 'all');
131
        $this->cache->clear('objectstategroup', $groupId);
132
        $this->cache->clear('objectstate', 'byGroup', $groupId);
133
134
        return $return;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function create($groupId, InputStruct $input)
141
    {
142
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
143
        $return = $this->persistenceHandler->objectStateHandler()->create($groupId, $input);
144
145
        $this->cache->clear('objectstate', 'byGroup', $groupId);
146
147
        return $return;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 View Code Duplication
    public function load($stateId)
154
    {
155
        $cache = $this->cache->getItem('objectstate', $stateId);
156
        $objectState = $cache->get();
157
        if ($cache->isMiss()) {
158
            $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
159
            $cache->set($objectState = $this->persistenceHandler->objectStateHandler()->load($stateId))->save();
160
        }
161
162
        return $objectState;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function loadByIdentifier($identifier, $groupId)
169
    {
170
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier, 'groupId' => $groupId));
171
172
        return $this->persistenceHandler->objectStateHandler()->loadByIdentifier($identifier, $groupId);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function update($stateId, InputStruct $input)
179
    {
180
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'struct' => $input));
181
        $return = $this->persistenceHandler->objectStateHandler()->update($stateId, $input);
182
183
        $this->cache->clear('objectstate', $stateId);
184
185
        return $return;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function setPriority($stateId, $priority)
192
    {
193
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'priority' => $priority));
194
        $return = $this->persistenceHandler->objectStateHandler()->setPriority($stateId, $priority);
195
196
        $this->cache->clear('objectstate', $stateId);
197
198
        return $return;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function delete($stateId)
205
    {
206
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
207
        $return = $this->persistenceHandler->objectStateHandler()->delete($stateId);
208
209
        $this->cache->clear('objectstate', $stateId);
210
        $this->cache->clear('objectstate', 'byGroup'); // TIMBER!
211
212
        return $return;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 View Code Duplication
    public function setContentState($contentId, $groupId, $stateId)
219
    {
220
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'groupId' => $groupId, 'stateId' => $stateId));
221
        $return = $this->persistenceHandler->objectStateHandler()->setContentState($contentId, $groupId, $stateId);
222
223
        $this->cache->clear('objectstate', 'byContent', $contentId, $groupId);
224
225
        return $return;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getContentState($contentId, $stateGroupId)
232
    {
233
        $cache = $this->cache->getItem('objectstate', 'byContent', $contentId, $stateGroupId);
234
        $stateId = $cache->get();
235 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...
236
            $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'stateGroupId' => $stateGroupId));
237
238
            $contentState = $this->persistenceHandler->objectStateHandler()->getContentState($contentId, $stateGroupId);
239
            $cache->set($contentState->id)->save();
240
241
            return $contentState;
242
        }
243
244
        return $this->load($stateId);
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     *
250
     * @todo cache results
251
     */
252
    public function getContentCount($stateId)
253
    {
254
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
255
256
        return $this->persistenceHandler->objectStateHandler()->getContentCount($stateId);
257
    }
258
}
259