Completed
Push — master ( 1494d2...398a9b )
by dan
02:07
created

NotificationManager.php (4 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
use Symfony\Component\PropertyAccess\PropertyAccess;
8
9
/**
10
 * Class NotificationManager
11
 * This the primary public service.
12
 * From here all notifications can be dispatched.
13
 * Essentially is just a wrapper around the ContainerManager and DatabaseNotificationManager
14
 *
15
 * @package NotificationBundle
16
 */
17
class NotificationManager
18
{
19
    /**
20
     * @var ChannelManager
21
     */
22
    private $channelManager;
23
    /**
24
     * @var DatabaseNotificationManager
25
     */
26
    private $databaseNotificationManager;
27
    protected $propertyAccessor;
28
29
    public function __construct(ChannelManager $channelManager, DatabaseNotificationManager $databaseNotificationManager = null, $broadcaster = null)
30
    {
31
        $this->channelManager = $channelManager;
32
        $this->databaseNotificationManager = $databaseNotificationManager;
33
        $this->broadcaster = $broadcaster;
0 ignored issues
show
The property broadcaster does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
    }
35
36
    public function setDatabaseNotificationManager(DatabaseNotificationManager $databaseNotificationManager)
37
    {
38
        $this->databaseNotificationManager = $databaseNotificationManager;
39
    }
40
41
    public function broadcast(NotificationInterface $notification, array $broadcasters = null)
0 ignored issues
show
The parameter $broadcasters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        try {
44
            $this->broadcaster->broadcast($notification);
45
        } catch (\Exception $exception) {
46
            // @TODO:
47
        }
48
    }
49
50
    public function send(NotificationInterface $notification, $recipients, array $data = [])
51
    {
52
        if (!is_array($recipients)) {
53
            $recipients = [$recipients];
54
        }
55
56
        if (!empty($data)) {
57
            if (empty($this->propertyAccessor)) {
58
                $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
59
            }
60
61
            $dataArray = $notification->getDataArray();
62
            foreach ($data as $key => $value) {
63
                $this->propertyAccessor->setValue($dataArray, '[' . $key . ']', $value);
64
            }
65
66
            $notification->setDataArray($dataArray);
67
        }
68
69
        $this->channelManager->send($recipients, $notification);
70
    }
71
72
    public function markAsRead(NotificationInterface $notification)
73
    {
74
        $now = new \DateTime();
75
        try {
76
            $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...
77
78
            return true;
79
        } catch (\Exception $exception) {
80
            // @TODO:
81
        }
82
    }
83
84
    public function markAllAsRead(NotifiableInterface $user)
85
    {
86
        $now = new \DateTime();
87
        try {
88
            $this->databaseNotificationManager->setUsersNotificationsAsRead($user, $now);
89
90
            return true;
91
        } catch (\Exception $exception) {
92
            // @TODO:
93
        }
94
    }
95
96
    public function allNotificationCount(NotifiableInterface $user)
97
    {
98
        return $this->notificationCount($user);
99
    }
100
101
    public function unreadNotificationCount(NotifiableInterface $user)
102
    {
103
        return $this->notificationCount($user, 'unread');
104
    }
105
106
    public function readNotificationCount(NotifiableInterface $user)
107
    {
108
        return $this->notificationCount($user, 'read');
109
    }
110
111
    public function notificationCount(NotifiableInterface $user, $status = '')
112
    {
113
        // try {
114
115
        return $this->databaseNotificationManager->getUsersNotificationCount($user, $status);
116
        // } 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...
117
        //     // @TODO:
118
        // }
119
    }
120
}
121