Completed
Push — master ( a00d21...132934 )
by Nicolas
04:39
created

CacheTranslationDecorator::cleanKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Modules\Translation\Repositories\Cache;
2
3
use Modules\Core\Repositories\Cache\BaseCacheDecorator;
4
use Modules\Translation\Entities\TranslationTranslation;
5
use Modules\Translation\Repositories\TranslationRepository;
6
7
class CacheTranslationDecorator extends BaseCacheDecorator implements TranslationRepository
8
{
9
    public function __construct(TranslationRepository $recipe)
10
    {
11
        parent::__construct();
12
        $this->entityName = 'translation.translations';
13
        $this->repository = $recipe;
14
    }
15
16
    /**
17
     * @param string $key
18
     * @param string $locale
19
     * @return string
20
     */
21 View Code Duplication
    public function findByKeyAndLocale($key, $locale = null)
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...
22
    {
23
        $key = $this->cleanKey($key);
24
25
        return $this->cache
26
            ->tags($this->entityName, 'global')
27
            ->rememberForever("{$this->locale}.{$this->entityName}.findByKeyAndLocale.{$key}.{$locale}",
28
                function () use ($key, $locale) {
29
                    return $this->repository->findByKeyAndLocale($key, $locale);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Modules\Core\Repositories\BaseRepository as the method findByKeyAndLocale() does only exist in the following implementations of said interface: Modules\Translation\Repo...cheTranslationDecorator, Modules\Translation\Repo...ntTranslationRepository.

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
                }
31
            );
32
    }
33
34
    public function allFormatted()
35
    {
36
        return $this->cache
37
            ->tags($this->entityName, 'global')
38
            ->rememberForever("{$this->locale}.{$this->entityName}.allFormatted",
39
                function () {
40
                    return $this->repository->allFormatted();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Modules\Core\Repositories\BaseRepository as the method allFormatted() does only exist in the following implementations of said interface: Modules\Translation\Repo...cheTranslationDecorator, Modules\Translation\Repo...ntTranslationRepository.

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...
41
                }
42
            );
43
    }
44
45
    public function saveTranslationForLocaleAndKey($locale, $key, $value)
46
    {
47
        $this->cache->tags($this->entityName)->flush();
48
49
        return $this->repository->saveTranslationForLocaleAndKey($locale, $key, $value);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Modules\Core\Repositories\BaseRepository as the method saveTranslationForLocaleAndKey() does only exist in the following implementations of said interface: Modules\Translation\Repo...cheTranslationDecorator, Modules\Translation\Repo...ntTranslationRepository.

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...
50
    }
51
52 View Code Duplication
    public function findTranslationByKey($key)
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...
53
    {
54
        $key = $this->cleanKey($key);
55
56
        return $this->cache
57
            ->tags($this->entityName, 'global')
58
            ->rememberForever("{$this->locale}.{$this->entityName}.findTranslationByKey.{$key}",
59
                function () use ($key) {
60
                    return $this->repository->findTranslationByKey($key);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Modules\Core\Repositories\BaseRepository as the method findTranslationByKey() does only exist in the following implementations of said interface: Modules\Translation\Repo...cheTranslationDecorator, Modules\Translation\Repo...ntTranslationRepository.

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...
61
                }
62
            );
63
    }
64
65
    /**
66
     * Update the given translation key with the given data
67
     * @param string $key
68
     * @param array $data
69
     * @return mixed
70
     */
71
    public function updateFromImport($key, array $data)
72
    {
73
        $this->cache->tags($this->entityName)->flush();
74
75
        return $this->repository->updateFromImport($key, $data);
0 ignored issues
show
Bug introduced by
The method updateFromImport() does not exist on Modules\Core\Repositories\BaseRepository. Did you maybe mean update()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76
    }
77
78
    /**
79
     * Set the given value on the given TranslationTranslation
80
     * @param TranslationTranslation $translationTranslation
81
     * @param string $value
82
     * @return void
83
     */
84
    public function updateTranslationToValue(TranslationTranslation $translationTranslation, $value)
85
    {
86
        $this->cache->tags($this->entityName)->flush();
87
88
        return $this->repository->updateTranslationToValue($translationTranslation, $value);
0 ignored issues
show
Bug introduced by
The method updateTranslationToValue() does not exist on Modules\Core\Repositories\BaseRepository. Did you maybe mean update()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
89
    }
90
91
    /**
92
     * Clean a Cache Key so it is safe for use
93
     * @param string $key   Potentially unsafe key
94
     * @return string
95
     */
96
    protected function cleanKey($key)
97
    {
98
        return str_replace(" ", "--", $key);
99
    }
100
}
101