Completed
Push — 6.7 ( 0b0beb...16636e )
by Łukasz
22:30
created

AliasGeneratorDecorator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getVariation() 0 11 2
A setSiteAccess() 0 4 1
A getCacheKey() 0 14 3
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\Core\MVC\Symfony\SiteAccess;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
13
use eZ\Publish\SPI\Variation\VariationHandler;
14
use Psr\Cache\CacheItemPoolInterface;
15
use Symfony\Component\Routing\RequestContext;
16
17
/**
18
 * Persistence Cache layer for AliasGenerator.
19
 */
20
class AliasGeneratorDecorator implements VariationHandler, SiteAccessAware
21
{
22
    /**
23
     * @var \eZ\Publish\SPI\Variation\VariationHandler
24
     */
25
    private $aliasGenerator;
26
27
    /**
28
     * @var \Psr\Cache\CacheItemPoolInterface
29
     */
30
    private $cache;
31
32
    /**
33
     * @var \eZ\Publish\Core\MVC\Symfony\SiteAccess
34
     */
35
    private $siteAccess;
36
37
    /**
38
     * @var \Symfony\Component\Routing\RequestContext
39
     */
40
    private $requestContext;
41
42
    /**
43
     * @param \eZ\Publish\SPI\Variation\VariationHandler $aliasGenerator
44
     * @param \Psr\Cache\CacheItemPoolInterface $cache
45
     * @param \Symfony\Component\Routing\RequestContext $requestContext
46
     */
47
    public function __construct(VariationHandler $aliasGenerator, CacheItemPoolInterface $cache, RequestContext $requestContext)
48
    {
49
        $this->aliasGenerator = $aliasGenerator;
50
        $this->cache = $cache;
51
        $this->requestContext = $requestContext;
52
    }
53
54
    /**
55
     * @param \eZ\Publish\API\Repository\Values\Content\Field $field
56
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
57
     * @param string $variationName
58
     * @param array $parameters
59
     *
60
     * @return \eZ\Publish\SPI\Variation\Values\Variation
61
     *
62
     * @throws \Psr\Cache\InvalidArgumentException
63
     */
64
    public function getVariation(Field $field, VersionInfo $versionInfo, $variationName, array $parameters = [])
65
    {
66
        $item = $this->cache->getItem($this->getCacheKey($field, $versionInfo, $variationName));
67
        $image = $item->get();
68
        if (!$item->isHit()) {
69
            $image = $this->aliasGenerator->getVariation($field, $versionInfo, $variationName, $parameters);
70
            $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...
71
        }
72
73
        return $image;
74
    }
75
76
    /**
77
     * @param \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteAccess
78
     */
79
    public function setSiteAccess(SiteAccess $siteAccess = null)
80
    {
81
        $this->siteAccess = $siteAccess;
82
    }
83
84
    /**
85
     * @param \eZ\Publish\API\Repository\Values\Content\Field $field
86
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
87
     * @param string $variationName
88
     *
89
     * @return string
90
     */
91
    private function getCacheKey(Field $field, VersionInfo $versionInfo, $variationName)
92
    {
93
        return sprintf(
94
            'ez-image-variation-%s-%s-%s-%d-%d-%d-%s-%s',
95
            $this->siteAccess ? $this->siteAccess->name : 'default',
96
            $this->requestContext->getScheme(),
97
            $this->requestContext->getHost(),
98
            $this->requestContext->getScheme() === 'https' ? $this->requestContext->getHttpsPort() : $this->requestContext->getHttpPort(),
99
            $versionInfo->getContentInfo()->id,
100
            $versionInfo->id,
101
            $field->id,
102
            $variationName
103
        );
104
    }
105
}
106