Completed
Push — 6.12 ( 1cff2f )
by Łukasz
64:51
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
    public function loadObjectStates($groupId)
79
    {
80
        $cache = $this->cache->getItem('objectstate', 'byGroup', $groupId);
81
        $objectStates = $cache->get();
82 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...
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
    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', $groupId);
100
101
        return $return;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 View Code Duplication
    public function deleteGroup($groupId)
108
    {
109
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
110
        $return = $this->persistenceHandler->objectStateHandler()->deleteGroup($groupId);
111
112
        $this->cache->clear('objectstategroup', 'all');
113
        $this->cache->clear('objectstategroup', $groupId);
114
        $this->cache->clear('objectstate', 'byGroup', $groupId);
115
116
        return $return;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function create($groupId, InputStruct $input)
123
    {
124
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
125
        $return = $this->persistenceHandler->objectStateHandler()->create($groupId, $input);
126
127
        $this->cache->clear('objectstate', 'byGroup', $groupId);
128
129
        return $return;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 View Code Duplication
    public function load($stateId)
136
    {
137
        $cache = $this->cache->getItem('objectstate', $stateId);
138
        $objectState = $cache->get();
139
        if ($cache->isMiss()) {
140
            $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
141
            $cache->set($objectState = $this->persistenceHandler->objectStateHandler()->load($stateId))->save();
142
        }
143
144
        return $objectState;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function loadByIdentifier($identifier, $groupId)
151
    {
152
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier, 'groupId' => $groupId));
153
154
        return $this->persistenceHandler->objectStateHandler()->loadByIdentifier($identifier, $groupId);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function update($stateId, InputStruct $input)
161
    {
162
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'struct' => $input));
163
        $return = $this->persistenceHandler->objectStateHandler()->update($stateId, $input);
164
165
        $this->cache->clear('objectstate', $stateId);
166
167
        return $return;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function setPriority($stateId, $priority)
174
    {
175
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'priority' => $priority));
176
        $return = $this->persistenceHandler->objectStateHandler()->setPriority($stateId, $priority);
177
178
        $this->cache->clear('objectstate', $stateId);
179
180
        return $return;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function delete($stateId)
187
    {
188
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
189
        $return = $this->persistenceHandler->objectStateHandler()->delete($stateId);
190
191
        $this->cache->clear('objectstate', $stateId);
192
        $this->cache->clear('objectstate', 'byGroup'); // TIMBER!
193
194
        return $return;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 View Code Duplication
    public function setContentState($contentId, $groupId, $stateId)
201
    {
202
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'groupId' => $groupId, 'stateId' => $stateId));
203
        $return = $this->persistenceHandler->objectStateHandler()->setContentState($contentId, $groupId, $stateId);
204
205
        $this->cache->clear('objectstate', 'byContent', $contentId, $groupId);
206
207
        return $return;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function getContentState($contentId, $stateGroupId)
214
    {
215
        $cache = $this->cache->getItem('objectstate', 'byContent', $contentId, $stateGroupId);
216
        $stateId = $cache->get();
217 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...
218
            $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'stateGroupId' => $stateGroupId));
219
220
            $contentState = $this->persistenceHandler->objectStateHandler()->getContentState($contentId, $stateGroupId);
221
            $cache->set($contentState->id)->save();
222
223
            return $contentState;
224
        }
225
226
        return $this->load($stateId);
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     *
232
     * @todo cache results
233
     */
234
    public function getContentCount($stateId)
235
    {
236
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
237
238
        return $this->persistenceHandler->objectStateHandler()->getContentCount($stateId);
239
    }
240
}
241