Completed
Push — sf_cache ( 94d0e5...42a66b )
by André
12:53
created

ObjectStateHandler::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 9.6666
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->deleteItem('ez-state-group-all');
28
        $this->cache->save(
29
            $this->cache->getItem('ez-state-group-'.$group->id)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Cache\CacheItemInterface as the method tag() does only exist in the following implementations of said interface: Symfony\Component\Cache\CacheItem.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
30
                ->set($group)
31
                ->tag(['state-group-'.$group->id])
32
        );
33
34
        return $group;
35
    }
36
37
    /**
38
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadGroup
39
     */
40 View Code Duplication
    public function loadGroup($groupId)
41
    {
42
        $cacheItem = $this->cache->getItem('ez-state-group-'.$groupId);
43
        if ($cacheItem->isHit()) {
44
            return $cacheItem->get();
45
        }
46
47
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
48
        $group = $this->persistenceHandler->objectStateHandler()->loadGroup($groupId);
49
50
        $cacheItem->set($group);
51
        $cacheItem->tag(['state-group-'.$group->id]);
52
        $this->cache->save($cacheItem);
53
54
        return $group;
55
    }
56
57
    /**
58
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadGroupByIdentifier
59
     */
60 View Code Duplication
    public function loadGroupByIdentifier($identifier)
61
    {
62
        $cacheItem = $this->cache->getItem('ez-state-group-identifier-'.$identifier);
63
        if ($cacheItem->isHit()) {
64
            return $cacheItem->get();
65
        }
66
67
        $this->logger->logCall(__METHOD__, array('groupId' => $identifier));
68
        $group = $this->persistenceHandler->objectStateHandler()->loadGroupByIdentifier($identifier);
69
70
        $cacheItem->set($group);
71
        $cacheItem->tag(['state-group-'.$group->id]);
72
        $this->cache->save($cacheItem);
73
74
        return $group;
75
    }
76
77
    /**
78
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
79
     */
80
    public function loadAllGroups($offset = 0, $limit = -1)
81
    {
82
        $cacheItem = $this->cache->getItem('ez-state-group-all');
83
        if ($cacheItem->isHit()) {
84
            return array_slice($cacheItem->get(), $offset, $limit > -1 ? $limit: null);
85
        }
86
87
        $this->logger->logCall(__METHOD__, array('offset' => $offset, 'limit' => $limit));
88
        $stateGroups = $this->persistenceHandler->objectStateHandler()->loadAllGroups(0, -1);
89
90
        $cacheItem->set($stateGroups);
91
        $cacheTags = [];
92
        foreach ($stateGroups as $group) {
93
            $cacheTags[] = 'state-group-'.$group->id;
94
        }
95
        $cacheItem->tag($cacheTags);
96
        $this->cache->save($cacheItem);
97
98
        return array_slice($stateGroups, $offset, $limit > -1 ? $limit: null);
99
    }
100
101
    /**
102
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates
103
     */
104
    public function loadObjectStates($groupId)
105
    {
106
        $cacheItem = $this->cache->getItem('ez-state-list-by-group-'.$groupId);
107
        if ($cacheItem->isHit()) {
108
            return $cacheItem->get();
109
        }
110
111
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
112
        $objectStates = $this->persistenceHandler->objectStateHandler()->loadObjectStates($groupId);
113
114
        $cacheItem->set($objectStates);
115
        $cacheTags = ['state-group-'.$groupId];
116
        foreach ($objectStates as $state) {
117
            $cacheTags[] = 'state-'.$state->id;
118
        }
119
        $cacheItem->tag($cacheTags);
120
        $this->cache->save($cacheItem);
121
122
        return $objectStates;
123
    }
124
125
    /**
126
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::updateGroup
127
     */
128 View Code Duplication
    public function updateGroup($groupId, InputStruct $input)
129
    {
130
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
131
        $return = $this->persistenceHandler->objectStateHandler()->updateGroup($groupId, $input);
132
133
        $this->cache->invalidateTags(['state-group-'.$groupId]);
134
135
        return $return;
136
    }
137
138
    /**
139
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::deleteGroup
140
     */
141
    public function deleteGroup($groupId)
142
    {
143
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId));
144
        $return = $this->persistenceHandler->objectStateHandler()->deleteGroup($groupId);
145
146
        $this->cache->invalidateTags(['state-group-'.$groupId]);
147
148
        return $return;
149
    }
150
151
    /**
152
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::create
153
     */
154
    public function create($groupId, InputStruct $input)
155
    {
156
        $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input));
157
        $return = $this->persistenceHandler->objectStateHandler()->create($groupId, $input);
158
159
        $this->cache->deleteItem('ez-state-list-by-group-'.$groupId);
160
161
        return $return;
162
    }
163
164
    /**
165
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load
166
     */
167 View Code Duplication
    public function load($stateId)
168
    {
169
        $cacheItem = $this->cache->getItem('ez-state-'.$stateId);
170
        if ($cacheItem->isHit()) {
171
            return $cacheItem->get();
172
        }
173
174
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
175
        $objectState = $this->persistenceHandler->objectStateHandler()->load($stateId);
176
177
        $cacheItem->set($objectState);
178
        $cacheItem->tag(['state-'.$objectState->id,'state-group-'.$objectState->groupId]);
179
        $this->cache->save($cacheItem);
180
181
        return $objectState;
182
    }
183
184
    /**
185
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadByIdentifier
186
     */
187 View Code Duplication
    public function loadByIdentifier($identifier, $groupId)
188
    {
189
190
        $cacheItem = $this->cache->getItem('ez-state-identifier-'.$identifier.'-by-group-'.$groupId);
191
        if ($cacheItem->isHit()) {
192
            return $cacheItem->get();
193
        }
194
195
        $this->logger->logCall(__METHOD__, array('identifier' => $identifier, 'groupId' => $groupId));
196
        $objectState = $this->persistenceHandler->objectStateHandler()->loadByIdentifier($identifier, $groupId);
197
198
        $cacheItem->set($objectState);
199
        $cacheItem->tag(['state-'.$objectState->id,'state-group-'.$objectState->groupId]);
200
        $this->cache->save($cacheItem);
201
202
        return $objectState;
203
    }
204
205
    /**
206
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::update
207
     */
208
    public function update($stateId, InputStruct $input)
209
    {
210
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'struct' => $input));
211
        $return = $this->persistenceHandler->objectStateHandler()->update($stateId, $input);
212
213
        $this->cache->invalidateTags(['state-'.$stateId]);
214
215
        return $return;
216
    }
217
218
    /**
219
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setPriority
220
     */
221
    public function setPriority($stateId, $priority)
222
    {
223
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId, 'priority' => $priority));
224
        $return = $this->persistenceHandler->objectStateHandler()->setPriority($stateId, $priority);
225
226
        $this->cache->invalidateTags(['state-'.$stateId]);
227
228
        return $return;
229
    }
230
231
    /**
232
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::delete
233
     */
234
    public function delete($stateId)
235
    {
236
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
237
        $return = $this->persistenceHandler->objectStateHandler()->delete($stateId);
238
239
        $this->cache->invalidateTags(['state-'.$stateId]);
240
241
        return $return;
242
    }
243
244
    /**
245
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setContentState
246
     */
247
    public function setContentState($contentId, $groupId, $stateId)
248
    {
249
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'groupId' => $groupId, 'stateId' => $stateId));
250
        $return = $this->persistenceHandler->objectStateHandler()->setContentState($contentId, $groupId, $stateId);
251
252
        $this->cache->deleteItem('ez-state-by-group-'.$groupId.'-on-content-'.$contentId);
253
254
        return $return;
255
    }
256
257
    /**
258
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState
259
     */
260 View Code Duplication
    public function getContentState($contentId, $stateGroupId)
261
    {
262
        $cacheItem = $this->cache->getItem('ez-state-by-group-'.$stateGroupId.'-on-content-'.$contentId);
263
        if ($cacheItem->isHit()) {
264
            return $cacheItem->get();
265
        }
266
267
        $this->logger->logCall(__METHOD__, array('contentId' => $contentId, 'stateGroupId' => $stateGroupId));
268
        $contentState = $this->persistenceHandler->objectStateHandler()->getContentState($contentId, $stateGroupId);
269
270
        $cacheItem->set($contentState);
271
        $cacheItem->tag(['state-'.$contentState->id, 'content-'.$contentId]);
272
        $this->cache->save($cacheItem);
273
274
        return $contentState;
275
    }
276
277
    /**
278
     * @see \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentCount
279
     */
280
    public function getContentCount($stateId)
281
    {
282
        $this->logger->logCall(__METHOD__, array('stateId' => $stateId));
283
284
        return $this->persistenceHandler->objectStateHandler()->getContentCount($stateId);
285
    }
286
}
287