CacheSettingDecorator::createOrUpdate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4286
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Modules\Setting\Repositories\Cache;
2
3
use Modules\Core\Repositories\Cache\BaseCacheDecorator;
4
use Modules\Setting\Repositories\SettingRepository;
5
6
class CacheSettingDecorator extends BaseCacheDecorator implements SettingRepository
7
{
8
    public function __construct(SettingRepository $setting)
9
    {
10
        parent::__construct();
11
        $this->entityName = 'setting.settings';
12
        $this->repository = $setting;
13
    }
14
15
    /**
16
     * Create or update the settings
17
     * @param $settings
18
     * @return mixed
19
     */
20
    public function createOrUpdate($settings)
21
    {
22
        $this->cache->tags($this->entityName)->flush();
23
24
        return $this->repository->createOrUpdate($settings);
0 ignored issues
show
Bug introduced by
The method createOrUpdate() does not exist on Modules\Core\Repositories\BaseRepository. Did you maybe mean create()?

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...
25
    }
26
27
    /**
28
     * Find a setting by its name
29
     * @param $settingName
30
     * @return mixed
31
     */
32 View Code Duplication
    public function findByName($settingName)
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...
33
    {
34
        return $this->cache
35
            ->tags($this->entityName, 'global')
36
            ->remember("{$this->locale}.{$this->entityName}.findByName.{$settingName}", $this->cacheTime,
37
                function () use ($settingName) {
38
                    return $this->repository->findByName($settingName);
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 findByName() does only exist in the following implementations of said interface: Modules\Setting\Reposito...e\CacheSettingDecorator, Modules\Setting\Reposito...oquentSettingRepository.

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...
39
                }
40
            );
41
    }
42
43
    /**
44
     * Return all modules that have settings
45
     * with its settings
46
     * @param  array|string $modules
47
     * @return array
48
     */
49 View Code Duplication
    public function moduleSettings($modules)
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...
50
    {
51
        $moduleList = implode(',', $modules);
52
53
        return $this->cache
54
            ->tags($this->entityName, 'global')
55
            ->remember("{$this->locale}.{$this->entityName}.moduleSettings.{$moduleList}", $this->cacheTime,
56
                function () use ($modules) {
57
                    return $this->repository->moduleSettings($modules);
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 moduleSettings() does only exist in the following implementations of said interface: Modules\Setting\Reposito...e\CacheSettingDecorator, Modules\Setting\Reposito...oquentSettingRepository.

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...
58
                }
59
            );
60
    }
61
62
    /**
63
     * Return the saved module settings
64
     * @param $module
65
     * @return mixed
66
     */
67 View Code Duplication
    public function savedModuleSettings($module)
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...
68
    {
69
        return $this->cache
70
            ->tags($this->entityName, 'global')
71
            ->remember("{$this->locale}.{$this->entityName}.savedModuleSettings.{$module}", $this->cacheTime,
72
                function () use ($module) {
73
                    return $this->repository->savedModuleSettings($module);
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 savedModuleSettings() does only exist in the following implementations of said interface: Modules\Setting\Reposito...e\CacheSettingDecorator, Modules\Setting\Reposito...oquentSettingRepository.

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...
74
                }
75
            );
76
    }
77
78
    /**
79
     * Find settings by module name
80
     * @param  string $module
81
     * @return mixed
82
     */
83 View Code Duplication
    public function findByModule($module)
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...
84
    {
85
        return $this->cache
86
            ->tags($this->entityName, 'global')
87
            ->remember("{$this->locale}.{$this->entityName}.findByModule.{$module}", $this->cacheTime,
88
                function () use ($module) {
89
                    return $this->repository->findByModule($module);
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 findByModule() does only exist in the following implementations of said interface: Modules\Setting\Reposito...e\CacheSettingDecorator, Modules\Setting\Reposito...oquentSettingRepository.

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...
90
                }
91
            );
92
    }
93
94
    /**
95
     * Find the given setting name for the given module
96
     * @param  string $settingName
97
     * @return mixed
98
     */
99 View Code Duplication
    public function get($settingName)
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...
100
    {
101
        return $this->cache
102
            ->tags($this->entityName, 'global')
103
            ->remember("{$this->locale}.{$this->entityName}.get.{$settingName}", $this->cacheTime,
104
                function () use ($settingName) {
105
                    return $this->repository->get($settingName);
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 get() does only exist in the following implementations of said interface: Modules\Setting\Reposito...e\CacheSettingDecorator, Modules\Setting\Reposito...oquentSettingRepository.

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...
106
                }
107
            );
108
    }
109
110
    /**
111
     * Return the translatable module settings
112
     * @param $module
113
     * @return array
114
     */
115 View Code Duplication
    public function translatableModuleSettings($module)
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...
116
    {
117
        return $this->cache
118
            ->tags($this->entityName, 'global')
119
            ->remember("{$this->locale}.{$this->entityName}.translatableModuleSettings.{$module}", $this->cacheTime,
120
                function () use ($module) {
121
                    return $this->repository->translatableModuleSettings($module);
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 translatableModuleSettings() does only exist in the following implementations of said interface: Modules\Setting\Reposito...e\CacheSettingDecorator, Modules\Setting\Reposito...oquentSettingRepository.

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...
122
                }
123
            );
124
    }
125
126
    /**
127
     * Return the non translatable module settings
128
     * @param $module
129
     * @return array
130
     */
131 View Code Duplication
    public function plainModuleSettings($module)
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...
132
    {
133
        return $this->cache
134
            ->tags($this->entityName, 'global')
135
            ->remember("{$this->locale}.{$this->entityName}.plainModuleSettings.{$module}", $this->cacheTime,
136
                function () use ($module) {
137
                    return $this->repository->plainModuleSettings($module);
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 plainModuleSettings() does only exist in the following implementations of said interface: Modules\Setting\Reposito...e\CacheSettingDecorator, Modules\Setting\Reposito...oquentSettingRepository.

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...
138
                }
139
            );
140
    }
141
}
142