Completed
Push — master ( 03bc6f...00f77b )
by Arjay
11:37
created

CacheRepository::getByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Repositories\FileAsset;
4
5
use Illuminate\Contracts\Cache\Repository as Cache;
6
7
class CacheRepository implements Repository
8
{
9
    /**
10
     * @var \Yajra\CMS\Repositories\FileAsset\Repository
11
     */
12
    private $repository;
13
14
    /**
15
     * @var \Illuminate\Contracts\Cache\Repository
16
     */
17
    private $cache;
18
19
    /**
20
     * FileAssetCacheRepository constructor.
21
     *
22
     * @param \Yajra\CMS\Repositories\FileAsset\Repository $repository
23
     * @param \Illuminate\Contracts\Cache\Repository $cache
24
     */
25
    public function __construct(Repository $repository, Cache $cache)
26
    {
27
        $this->repository = $repository;
28
        $this->cache      = $cache;
29
    }
30
31
    /**
32
     * Get all file assets.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Collection|static[]
35
     */
36
    public function all()
37
    {
38
        return $this->cache->rememberForever('fileAssets.all', function () {
39
            return $this->repository->all();
40
        });
41
    }
42
43
    /**
44
     * Get file asset by name.
45
     *
46
     * @param string $name
47
     * @param null $category
48
     * @return \Yajra\CMS\Repositories\FileAsset\FileAsset
49
     */
50
    public function getByName($name, $category = null)
51
    {
52
        return $this->cache->rememberForever('fileAssets.' . $name . '.' . $category,
53
            function () use ($name, $category) {
54
                return $this->repository->getByName($name, $category);
55
            });
56
    }
57
58
    /**
59
     * Add site asset.
60
     *
61
     * @param string $type
62
     * @return \Roumen\Asset\Asset;
0 ignored issues
show
Documentation introduced by
The doc-type \Roumen\Asset\Asset; could not be parsed: Expected "|" or "end of type", but got ";" at position 19. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
63
     */
64
    public function addAsset($type)
65
    {
66
        return $this->cache->rememberForever('fileAssets.addAsset.' . $type, function () use ($type) {
67
            return $this->repository->addAsset($type);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Yajra\CMS\Repositories\FileAsset\Repository as the method addAsset() does only exist in the following implementations of said interface: Yajra\CMS\Repositories\FileAsset\CacheRepository.

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...
68
        });
69
    }
70
}