Failed Conditions
Push — master ( 35fb93...846edc )
by Maximo
20s queued 13s
created

Notification::getTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Canvas\Notifications;
6
7
use Baka\Mail\Message;
0 ignored issues
show
Bug introduced by
The type Baka\Mail\Message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Canvas\Contracts\Auth\UserInterface;
9
use Canvas\Contracts\Notifications\NotificationInterface;
10
use Canvas\Models\AbstractModel;
11
use Canvas\Models\Notifications;
12
use Canvas\Models\NotificationType;
13
use Canvas\Models\Users;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Canvas\Notifications\Users. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Canvas\Queue\Queue;
15
use Phalcon\Di;
16
use Phalcon\Mvc\Model;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Phalcon\Traits\EventManagerAwareTrait;
0 ignored issues
show
Bug introduced by
The type Phalcon\Traits\EventManagerAwareTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class Notification implements NotificationInterface
20
{
21
    use EventManagerAwareTrait;
22
23
    /**
24
     *
25
     * @var Users
26
     */
27
    protected $toUser = null;
28
29
    /**
30
     *
31
     * @var Users
32
     */
33
    protected $fromUser = null;
34
35
    /**
36
     * Send this notification to the queue?
37
     *
38
     * @var boolean
39
     */
40
    protected $useQueue = false;
41
42
    /**
43
     *
44
     * @var NotificationType
45
     */
46
    protected $type = null;
47
48
    /**
49
     *
50
     * @var AbstractModel
51
     */
52
    protected $entity = null;
53
54
    /**
55
     *
56
     * @var Baka\Mail\Manager
0 ignored issues
show
Bug introduced by
The type Canvas\Notifications\Baka\Mail\Manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
     */
58
    protected $mail;
59
60
    const USERS = 'Canvas\Notifications\Users';
61
    const SYSTEM = 'Canvas\Notifications\System';
62
    const APPS = 'Canvas\Notifications\Apps';
63
64
    /**
65
     * Constructor.
66
     *
67
     * @param AbstractModel $entity
68
     */
69
    public function __construct(Model $entity)
70
    {
71
        $this->entity = $entity;
72
    }
73
74
    /**
75
     * Set the notification type.
76
     *
77
     * @param NotificationType $type
78
     *
79
     * @return void
80
     */
81
    public function setType(NotificationType $type): void
82
    {
83
        $this->type = $type;
84
    }
85
86
    /**
87
     * Return the message from the current notification type.
88
     *
89
     * @return string
90
     */
91
    public function message(): string
92
    {
93
        return $this->type->template ?: '';
94
    }
95
96
    /**
97
     * Define a Baka Mail to send a email.
98
     *
99
     * @todo add Interfase to bakaMail
100
     *
101
     * @return Message
102
     */
103
    protected function toMail(): ?Message
104
    {
105
        return null;
106
    }
107
108
    /**
109
     * To send push notification.
110
     *
111
     * @return void
112
     */
113
    protected function toPushNotification(): ?PushNotification
114
    {
115
        return null;
116
    }
117
118
    /**
119
     * Send to websocket / realtime.
120
     *
121
     * @return void
122
     */
123
    protected function toRealtime(): ?PusherNotification
124
    {
125
        return null;
126
    }
127
128
    /**
129
     * Set the usre we are sending the notification to.
130
     *
131
     * @param Users $user
132
     *
133
     * @return void
134
     */
135
    public function setTo(UserInterface $user): void
136
    {
137
        $this->toUser = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type Canvas\Contracts\Auth\UserInterface is incompatible with the declared type Canvas\Models\Users of property $toUser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
138
    }
139
140
    /**
141
     * Set the user from who the notification if comming from.
142
     *
143
     * @param User $user
144
     *
145
     * @return void
146
     */
147
    public function setFrom(UserInterface $user): void
148
    {
149
        $this->fromUser = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type Canvas\Contracts\Auth\UserInterface is incompatible with the declared type Canvas\Models\Users of property $fromUser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
150
    }
151
152
    /**
153
     * Set the usre we are sending the notification to.
154
     *
155
     * @return Users
156
     */
157
    public function getTo(): ?UserInterface
158
    {
159
        return $this->toUser;
160
    }
161
162
    /**
163
     * get the user from who the notification if comming from.
164
     *
165
     * @return User
166
     */
167
    public function getFrom(): ?UserInterface
168
    {
169
        return $this->fromUser;
170
    }
171
172
    /**
173
     * Disable this notification queue in runtime.
174
     *
175
     * @return void
176
     */
177
    public function disableQueue(): void
178
    {
179
        $this->useQueue = false;
180
    }
181
182
    /**
183
     * Process the notification
184
     *  - handle the db
185
     *  - trigger the notification
186
     *  - knows if we have to send it to queu.
187
     *
188
     * @return boolean
189
     */
190
    public function process(): bool
191
    {
192
        //if the user didnt provide the type get it based on the class name
193
        if (is_null($this->type)) {
194
            $this->setType(NotificationType::getByKey(static::class));
195
        } elseif (is_string($this->type)) {
0 ignored issues
show
introduced by
The condition is_string($this->type) is always false.
Loading history...
196
            //not great but for now lets use it
197
            $this->setType(NotificationType::getByKey($this->type));
198
        }
199
200
        if (Di::getDefault()->has('mail')) {
201
            $this->mail = Di::getDefault()->getMail();
202
        }
203
204
        if ($this->useQueue) {
205
            $this->sendToQueue();
206
            return true; //send it to the queue
207
        }
208
209
        $this->trigger();
210
211
        return true;
212
    }
213
214
    /**
215
     * Send to our internal Notification queue.
216
     *
217
     * @return boolean
218
     */
219
    protected function sendToQueue(): bool
220
    {
221
        $notificationData = [
222
            'from' => $this->fromUser,
223
            'to' => $this->toUser,
224
            'entity' => $this->entity,
225
            'type' => $this->type,
226
            'notification' => get_class($this),
227
        ];
228
229
        return Queue::send(Queue::NOTIFICATIONS, serialize($notificationData));
230
    }
231
232
    /**
233
     * Send the noficiatino to the places the user defined.
234
     *
235
     * @return boolean
236
     */
237
    public function trigger(): bool
238
    {
239
        $content = $this->message();
240
        $app = Di::getDefault()->getApp();
241
242
        //save to DB
243
        $notification = new Notifications();
244
        $notification->from_users_id = $this->fromUser->getId();
245
        $notification->users_id = $this->toUser->getId();
246
        $notification->companies_id = $this->fromUser->currentCompanyId();
247
        $notification->apps_id = $app->getId();
248
        $notification->system_modules_id = $this->type->system_modules_id;
249
        $notification->notification_type_id = $this->type->getId();
250
        $notification->entity_id = $this->entity->getId();
251
        $notification->content = $content;
252
        $notification->read = 0;
253
        $notification->saveOrFail();
254
255
        $toMail = $this->toMail();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $toMail is correct as $this->toMail() targeting Canvas\Notifications\Notification::toMail() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
256
        if ($toMail instanceof Message) {
257
            $this->fire('notification:sendMail', $toMail);
258
        }
259
260
        $toPushNotification = $this->toPushNotification();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $toPushNotification is correct as $this->toPushNotification() targeting Canvas\Notifications\Not...n::toPushNotification() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
261
        if ($toPushNotification instanceof PushNotification) {
262
            $this->fire('notification:sendPushNotification', $toPushNotification);
263
        }
264
265
        $toRealtime = $this->toRealtime();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $toRealtime is correct as $this->toRealtime() targeting Canvas\Notifications\Notification::toRealtime() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
266
        if ($toRealtime instanceof PusherNotification) {
267
            $this->fire('notification:sendRealtimeNotification', $toRealtime);
268
        }
269
270
        /**
271
         * @todo send to push ontification
272
         */
273
274
        if ($this->type->with_realtime) {
275
            $this->fire('notification:sendRealtime', $this);
276
        }
277
278
        return true;
279
    }
280
}
281