CacheNotificationDecorator::deleteAllForUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Modules\Notification\Repositories\Cache;
4
5
use Modules\Core\Repositories\Cache\BaseCacheDecorator;
6
use Modules\Notification\Repositories\NotificationRepository;
7
8
class CacheNotificationDecorator extends BaseCacheDecorator implements NotificationRepository
9
{
10
    public function __construct(NotificationRepository $notification)
11
    {
12
        parent::__construct();
13
        $this->entityName = 'notification.notifications';
14
        $this->repository = $notification;
15
    }
16
17
    /**
18
     * @param int $userId
19
     * @return \Illuminate\Database\Eloquent\Collection
20
     */
21 View Code Duplication
    public function latestForUser($userId)
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
        return $this->cache
24
            ->tags([$this->entityName, 'global'])
25
            ->remember(
26
                "{$this->locale}.{$this->entityName}.latestForUser.{$userId}",
27
                $this->cacheTime,
28
                function () use ($userId) {
29
                    return $this->repository->latestForUser($userId);
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 latestForUser() does only exist in the following implementations of said interface: Modules\Notification\Rep...heNotificationDecorator, Modules\Notification\Rep...tNotificationRepository.

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
    /**
35
     * Mark the given notification id as "read"
36
     * @param int $notificationId
37
     * @return bool
38
     */
39
    public function markNotificationAsRead($notificationId)
40
    {
41
        $this->cache->tags($this->entityName)->flush();
42
43
        return $this->repository->markNotificationAsRead($notificationId);
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 markNotificationAsRead() does only exist in the following implementations of said interface: Modules\Notification\Rep...heNotificationDecorator, Modules\Notification\Rep...tNotificationRepository.

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...
44
    }
45
46
    /**
47
     * Get all the notifications for the given user id
48
     * @param int $userId
49
     * @return \Illuminate\Database\Eloquent\Collection
50
     */
51 View Code Duplication
    public function allForUser($userId)
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...
52
    {
53
        return $this->cache
54
            ->tags([$this->entityName, 'global'])
55
            ->remember(
56
                "{$this->locale}.{$this->entityName}.allForUser.{$userId}",
57
                $this->cacheTime,
58
                function () use ($userId) {
59
                    return $this->repository->allForUser($userId);
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 allForUser() does only exist in the following implementations of said interface: Modules\Notification\Rep...heNotificationDecorator, Modules\Notification\Rep...tNotificationRepository.

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...
60
                }
61
            );
62
    }
63
64
    /**
65
     * Get all the read notifications for the given user id
66
     * @param int $userId
67
     * @return \Illuminate\Database\Eloquent\Collection
68
     */
69 View Code Duplication
    public function allReadForUser($userId)
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...
70
    {
71
        return $this->cache
72
            ->tags([$this->entityName, 'global'])
73
            ->remember(
74
                "{$this->locale}.{$this->entityName}.allReadForUser.{$userId}",
75
                $this->cacheTime,
76
                function () use ($userId) {
77
                    return $this->repository->allReadForUser($userId);
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 allReadForUser() does only exist in the following implementations of said interface: Modules\Notification\Rep...heNotificationDecorator, Modules\Notification\Rep...tNotificationRepository.

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...
78
                }
79
            );
80
    }
81
82
    /**
83
     * Get all the unread notifications for the given user id
84
     * @param int $userId
85
     * @return \Illuminate\Database\Eloquent\Collection
86
     */
87 View Code Duplication
    public function allUnreadForUser($userId)
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...
88
    {
89
        return $this->cache
90
            ->tags([$this->entityName, 'global'])
91
            ->remember(
92
                "{$this->locale}.{$this->entityName}.allUnreadForUser.{$userId}",
93
                $this->cacheTime,
94
                function () use ($userId) {
95
                    return $this->repository->allUnreadForUser($userId);
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 allUnreadForUser() does only exist in the following implementations of said interface: Modules\Notification\Rep...heNotificationDecorator, Modules\Notification\Rep...tNotificationRepository.

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...
96
                }
97
            );
98
    }
99
100
    /**
101
     * Delete all the notifications for the given user
102
     * @param int $userId
103
     * @return bool
104
     */
105
    public function deleteAllForUser($userId)
106
    {
107
        $this->cache->tags($this->entityName)->flush();
108
109
        return $this->repository->deleteAllForUser($userId);
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 deleteAllForUser() does only exist in the following implementations of said interface: Modules\Notification\Rep...heNotificationDecorator, Modules\Notification\Rep...tNotificationRepository.

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...
110
    }
111
112
    /**
113
     * Mark all the notifications for the given user as read
114
     * @param int $userId
115
     * @return bool
116
     */
117
    public function markAllAsReadForUser($userId)
118
    {
119
        $this->cache->tags($this->entityName)->flush();
120
121
        return $this->repository->markAllAsReadForUser($userId);
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 markAllAsReadForUser() does only exist in the following implementations of said interface: Modules\Notification\Rep...heNotificationDecorator, Modules\Notification\Rep...tNotificationRepository.

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