Completed
Push — master ( b1e889...da3959 )
by Jonathan
7s
created

PoolDecorator::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Jsq\CacheEncryption\Iron;
3
4
use Iron;
5
use Iron\PasswordInterface;
6
use Jsq\CacheEncryption\InvalidArgumentException;
7
use Jsq\CacheEncryption\PoolDecorator as BasePoolDecorator;
8
use Psr\Cache\CacheItemInterface;
9
use Psr\Cache\CacheItemPoolInterface;
10
11
class PoolDecorator extends BasePoolDecorator
12
{
13
    /** @var CacheItemPoolInterface */
14
    private $decorated;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
15
    /** @var PasswordInterface */
16
    private $password;
17
    /** @var string */
18
    private $cipher;
19
20 426 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
21
        CacheItemPoolInterface $decorated,
22
        $password,
23
        $cipher = Iron\Iron::DEFAULT_ENCRYPTION_METHOD
24
    ) {
25 426
        if (!class_exists(Iron\Iron::class)) {
26
            // @codeCoverageIgnoreStart
27
            throw new \RuntimeException('You must install'
28
                . ' jsq/iron-php to use the Iron decorator.');
29
            // @codeCoverageIgnoreEnd
30
        }
31
        
32 426
        parent::__construct($decorated);
33 426
        $this->decorated = $decorated;
34 426
        $this->password = Iron\normalize_password($password);
35 426
        $this->cipher = $cipher;
36 426
    }
37
38 126
    protected function decorate(CacheItemInterface $inner)
39
    {
40 126
        return new ItemDecorator(
41
            $inner,
42 126
            $this->password,
43 126
            $this->cipher
44
        );
45
    }
46
47 75
    public function save(CacheItemInterface $item)
48
    {
49 75
        $this->finalizeItem($item);
50 60
        return $this->decorated->save($item->getDecorated());
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 getDecorated() does only exist in the following implementations of said interface: Jsq\CacheEncryption\Envelope\ItemDecorator, Jsq\CacheEncryption\Iron\ItemDecorator, Jsq\CacheEncryption\ItemDecorator, Jsq\CacheEncryption\Password\ItemDecorator.

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...
51
    }
52
53 42
    public function saveDeferred(CacheItemInterface $item)
54
    {
55 42
        $this->finalizeItem($item);
56 27
        return $this->decorated->saveDeferred($item->getDecorated());
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 getDecorated() does only exist in the following implementations of said interface: Jsq\CacheEncryption\Envelope\ItemDecorator, Jsq\CacheEncryption\Iron\ItemDecorator, Jsq\CacheEncryption\ItemDecorator, Jsq\CacheEncryption\Password\ItemDecorator.

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...
57
    }
58
59 117
    private function finalizeItem(CacheItemInterface $item)
60
    {
61 117
        if ($item instanceof ItemDecorator) {
62 87
            return $item->finalize();
63
        }
64
        
65 30
        throw new InvalidArgumentException('The provided cache item cannot'
66 30
            . ' be saved, as it did not originate from this cache.');
67
    }
68
}
69