Completed
Push — migrate-files-no-interaction ( 025687...608925 )
by
unknown
46:43 queued 18:48
created

ObjectStateHandler::loadAllGroups()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 5
Ratio 38.46 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 5
loc 13
rs 9.4285
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 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...
67
            $this->logger->logCall(__METHOD__, array('offset' => $offset, 'limit' => $limit));
68
            $stateGroups = $this->persistenceHandler->objectStateHandler()->loadAllGroups(0, -1);
69
            $cache->set($stateGroups)->save();
70
        }
71
72
        return array_slice($stateGroups, $offset, $limit > -1 ?: null);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 View Code Duplication
    public function loadObjectStates($groupId)
79
    {
80
        $cache = $this->cache->getItem('objectstate', 'byGroup', $groupId);
81
        $objectStates = $cache->get();
82
        if ($cache->isMiss()) {
83
            $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
84
            $objectStates = $this->persistenceHandler->objectStateHandler()->loadObjectStates($groupId);
85
            $cache->set($objectStates)->save();
86
        }
87
88
        return $objectStates;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 View Code Duplication
    public function updateGroup($groupId, InputStruct $input)
95
    {
96
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
97
        $return = $this->persistenceHandler->objectStateHandler()->updateGroup($groupId, $input);
98
99
        $this->cache->clear('objectstategroup', 'all');
100
        $this->cache->clear('objectstategroup', $groupId);
101
102
        return $return;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 View Code Duplication
    public function deleteGroup($groupId)
109
    {
110
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
111
        $return = $this->persistenceHandler->objectStateHandler()->deleteGroup($groupId);
112
113
        $this->cache->clear('objectstategroup', 'all');
114
        $this->cache->clear('objectstategroup', $groupId);
115
        $this->cache->clear('objectstate', 'byGroup', $groupId);
116
117
        return $return;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function create($groupId, InputStruct $input)
124
    {
125
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
126
        $return = $this->persistenceHandler->objectStateHandler()->create($groupId, $input);
127
128
        $this->cache->clear('objectstate', 'byGroup', $groupId);
129
130
        return $return;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 View Code Duplication
    public function load($stateId)
137
    {
138
        $cache = $this->cache->getItem('objectstate', $stateId);
139
        $objectState = $cache->get();
140
        if ($cache->isMiss()) {
141
            $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
142
            $cache->set($objectState = $this->persistenceHandler->objectStateHandler()->load($stateId))->save();
143
        }
144
145
        return $objectState;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function loadByIdentifier($identifier, $groupId)
152
    {
153
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier, 'groupId' => $groupId));
154
155
        return $this->persistenceHandler->objectStateHandler()->loadByIdentifier($identifier, $groupId);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 View Code Duplication
    public function update($stateId, InputStruct $input)
162
    {
163
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'struct' => $input));
164
        $return = $this->persistenceHandler->objectStateHandler()->update($stateId, $input);
165
166
        $this->cache->clear('objectstate', $stateId);
167
        $this->cache->clear('objectstate', 'byGroup');
168
169
        return $return;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function setPriority($stateId, $priority)
176
    {
177
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'priority' => $priority));
178
        $return = $this->persistenceHandler->objectStateHandler()->setPriority($stateId, $priority);
179
180
        $this->cache->clear('objectstate', $stateId);
181
182
        return $return;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function delete($stateId)
189
    {
190
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
191
        $return = $this->persistenceHandler->objectStateHandler()->delete($stateId);
192
193
        $this->cache->clear('objectstate', $stateId);
194
        $this->cache->clear('objectstate', 'byGroup'); // TIMBER!
195
196
        return $return;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 View Code Duplication
    public function setContentState($contentId, $groupId, $stateId)
203
    {
204
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'groupId' => $groupId, 'stateId' => $stateId));
205
        $return = $this->persistenceHandler->objectStateHandler()->setContentState($contentId, $groupId, $stateId);
206
207
        $this->cache->clear('objectstate', 'byContent', $contentId, $groupId);
208
209
        return $return;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function getContentState($contentId, $stateGroupId)
216
    {
217
        $cache = $this->cache->getItem('objectstate', 'byContent', $contentId, $stateGroupId);
218
        $stateId = $cache->get();
219
        if ($cache->isMiss()) {
220
            $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'stateGroupId' => $stateGroupId));
221
222
            $contentState = $this->persistenceHandler->objectStateHandler()->getContentState($contentId, $stateGroupId);
223
            $cache->set($contentState->id)->save();
224
225
            return $contentState;
226
        }
227
228
        return $this->load($stateId);
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     *
234
     * @todo cache results
235
     */
236
    public function getContentCount($stateId)
237
    {
238
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
239
240
        return $this->persistenceHandler->objectStateHandler()->getContentCount($stateId);
241
    }
242
}
243