Completed
Push — master ( df9f00...ccd20f )
by dan
01:37
created

NotificationManager.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace IrishDan\NotificationBundle;
4
5
use IrishDan\NotificationBundle\Notification\NotifiableInterface;
6
use IrishDan\NotificationBundle\Notification\NotificationInterface;
7
8
/**
9
 * Class NotificationManager
10
 * This the primary public service.
11
 * From here all notifications can be dispatched.
12
 * Essentially is just a wrapper around the ContainerManager and DatabaseNotificationManager
13
 *
14
 * @package NotificationBundle
15
 */
16
class NotificationManager
17
{
18
    /**
19
     * @var ChannelManager
20
     */
21
    private $channelManager;
22
    /**
23
     * @var DatabaseNotificationManager
24
     */
25
    private $databaseNotificationManager;
26
27
    public function __construct(ChannelManager $channelManager)
28
    {
29
        $this->channelManager = $channelManager;
30
    }
31
32
    public function send(NotificationInterface $notification, $recipients)
33
    {
34
        if (!is_array($recipients)) {
35
            $recipients = [$recipients];
36
        }
37
38
        $this->channelManager->send($recipients, $notification);
39
    }
40
41
    public function markAsRead(NotificationInterface $notification)
42
    {
43
        $now = new \DateTime();
44
        try {
45
            $this->databaseNotificationManager->setReadAtDate($notification, $now);
0 ignored issues
show
$notification is of type object<IrishDan\Notifica...\NotificationInterface>, but the function expects a object<IrishDan\Notifica...eNotificationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47
            return true;
48
        } catch (\Exception $exception) {
49
            // @TODO:
50
        }
51
    }
52
53
    public function markAllAsRead(NotifiableInterface $user)
54
    {
55
        $now = new \DateTime();
56
        try {
57
            $this->databaseNotificationManager->setUsersNotificationsAsRead($user, $now);
58
59
            return true;
60
        } catch (\Exception $exception) {
61
            // @TODO:
62
        }
63
    }
64
65
    public function allNotificationCount(NotifiableInterface $user)
66
    {
67
        return $this->notificationCount($user);
68
    }
69
70
    public function unreadNotificationCount(NotifiableInterface $user)
71
    {
72
        return $this->notificationCount($user, 'unread');
73
    }
74
75
    public function readNotificationCount(NotifiableInterface $user)
76
    {
77
        return $this->notificationCount($user, 'read');
78
    }
79
80
    public function notificationCount(NotifiableInterface $user, $status = '')
81
    {
82
        // try {
83
84
        return $this->databaseNotificationManager->getUsersNotificationCount($user, $status);
85
        // } catch (\Exception $exception) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
        //     // @TODO:
87
        // }
88
    }
89
90
    public function setDatabaseNotificationManager(DatabaseNotificationManager $databaseNotificationManager)
91
    {
92
        $this->databaseNotificationManager = $databaseNotificationManager;
93
    }
94
}
95