Completed
Push — 6.7 ( a85f61...1fb9e9 )
by Łukasz
46:47 queued 23:40
created

AliasGeneratorDecorator::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Bundle\EzPublishCoreBundle\Imagine\Cache;
8
9
use eZ\Publish\API\Repository\Values\Content\Field;
10
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
11
use eZ\Publish\SPI\Variation\VariationHandler;
12
use Psr\Cache\CacheItemPoolInterface;
13
14
/**
15
 * Persistence Cache layer for AliasGenerator.
16
 */
17
class AliasGeneratorDecorator implements VariationHandler
18
{
19
    /**
20
     * @var \eZ\Publish\SPI\Variation\VariationHandler
21
     */
22
    private $aliasGenerator;
23
24
    /**
25
     * @var \Psr\Cache\CacheItemPoolInterface
26
     */
27
    private $cache;
28
29
    /**
30
     * @param \eZ\Publish\SPI\Variation\VariationHandler $aliasGenerator
31
     * @param \Psr\Cache\CacheItemPoolInterface $cache
32
     */
33
    public function __construct(VariationHandler $aliasGenerator, CacheItemPoolInterface $cache)
34
    {
35
        $this->aliasGenerator = $aliasGenerator;
36
        $this->cache = $cache;
37
    }
38
39
    /**
40
     * @param \eZ\Publish\API\Repository\Values\Content\Field $field
41
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
42
     * @param string $variationName
43
     * @param array $parameters
44
     *
45
     * @return \eZ\Publish\SPI\Variation\Values\Variation
46
     *
47
     * @throws \Psr\Cache\InvalidArgumentException
48
     */
49
    public function getVariation(Field $field, VersionInfo $versionInfo, $variationName, array $parameters = [])
50
    {
51
        $item = $this->cache->getItem($this->getCacheKey($field, $versionInfo, $variationName));
52
        $image = $item->get();
53
        if (!$item->isHit()) {
54
            $image = $this->aliasGenerator->getVariation($field, $versionInfo, $variationName, $parameters);
55
            $item->set($image)->save();
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 save() does only exist in the following implementations of said interface: Stash\Item, Tedivm\StashBundle\Service\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...
56
        }
57
58
        return $image;
59
    }
60
61
    /**
62
     * @param \eZ\Publish\API\Repository\Values\Content\Field $field
63
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
64
     * @param string $variationName
65
     *
66
     * @return string
67
     */
68
    private function getCacheKey(Field $field, VersionInfo $versionInfo, $variationName)
69
    {
70
        return sprintf(
71
            'ez-image-variation-%d-%d-%s-%s',
72
            $versionInfo->getContentInfo()->id,
73
            $versionInfo->id,
74
            $field->id,
75
            $variationName
76
        );
77
    }
78
}
79