Failed Conditions
Pull Request — master (#342)
by Maximo
02:25
created

Notification::message()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Notifications;
6
7
use Canvas\Contracts\Notifications\NotificationInterfase;
8
use Canvas\Models\AbstractModel;
9
use Canvas\Models\NotificationType;
10
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...
11
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...
12
use Canvas\Models\Notifications;
13
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...
14
use Phalcon\Di;
15
use Canvas\Queue\Queue;
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
18
class Notification implements NotificationInterfase
19
{
20
    use EventManagerAwareTrait;
21
22
    /**
23
     *
24
     * @var Users
25
     */
26
    protected $toUser = null;
27
28
    /**
29
     *
30
     * @var Users
31
     */
32
    protected $fromUser = null;
33
34
    /**
35
     * Send this notification to the queue?
36
     *
37
     * @var boolean
38
     */
39
    protected $useQueue = false;
40
41
    /**
42
     *
43
     * @var NotificationType
44
     */
45
    protected $type = null;
46
47
    /**
48
     *
49
     * @var AbstractModel
50
     */
51
    protected $entity = null;
52
53
    /**
54
     *
55
     * @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...
56
     */
57
    protected $mail;
58
59
    const USERS = 'Canvas\Notifications\Users' ;
60
    const SYSTEM = 'Canvas\Notifications\System';
61
    const APPS = 'Canvas\Notifications\Apps';
62
63
    /**
64
     * Constructor.
65
     *
66
     * @param AbstractModel $entity
67
     */
68
    public function __construct(Model $entity)
69
    {
70
        $this->entity = $entity;
71
    }
72
73
    /**
74
     * Set the notification type.
75
     *
76
     * @param NotificationType $type
77
     * @return void
78
     */
79
    public function setType(NotificationType $type): void
80
    {
81
        $this->type = $type;
82
    }
83
84
    /**
85
     * Return the message from the current notification type.
86
     *
87
     * @return string
88
     */
89
    public function message(): string
90
    {
91
        return $this->type->template ?: '';
92
    }
93
94
    /**
95
     * Define a Baka Mail to send a email.
96
     *
97
     * @todo add Interfase to bakaMail
98
     * @return Message
99
     */
100
    protected function toMail(): ?Message
101
    {
102
        return null;
103
    }
104
105
    /**
106
     * To send push notification.
107
     *
108
     * @return void
109
     */
110
    protected function toPushNotification(): ?PushNotification
111
    {
112
        return null;
113
    }
114
115
    /**
116
     * Send to websocket / realtime.
117
     *
118
     * @return void
119
     */
120
    protected function toRealtime(): ?PusherNotification
121
    {
122
        return null;
123
    }
124
125
    /**
126
     * Set the usre we are sending the notification to.
127
     *
128
     * @param Users $user
129
     * @return void
130
     */
131
    public function setTo(Users $user): void
132
    {
133
        $this->toUser = $user;
134
    }
135
136
    /**
137
     * Set the user from who the notification if comming from.
138
     *
139
     * @param User $user
140
     * @return void
141
     */
142
    public function setFrom(Users $user): void
143
    {
144
        $this->fromUser = $user;
145
    }
146
147
    /**
148
     * Disable this notification queue in runtime.
149
     *
150
     * @return void
151
     */
152
    public function disableQueue(): void
153
    {
154
        $this->useQueue = false;
155
    }
156
157
    /**
158
     * Process the notification
159
     *  - handle the db
160
     *  - trigger the notification
161
     *  - knows if we have to send it to queu.
162
     *
163
     * @return boolean
164
     */
165
    public function process(): bool
166
    {
167
        //if the user didnt provide the type get it based on the class name
168
        if (is_null($this->type)) {
169
            $this->setType(NotificationType::getByKey(static::class));
170
        } elseif (is_string($this->type)) {
0 ignored issues
show
introduced by
The condition is_string($this->type) is always false.
Loading history...
171
            //not great but for now lets use it
172
            $this->setType(NotificationType::getByKey($this->type));
173
        }
174
175
        if (Di::getDefault()->has('mail')) {
176
            $this->mail = Di::getDefault()->getMail();
177
        }
178
179
        if ($this->useQueue) {
180
            $this->sendToQueue();
181
            return true; //send it to the queue
182
        }
183
184
        $this->trigger();
185
186
        return true;
187
    }
188
189
    /**
190
     * Send to our internal Notification queue.
191
     *
192
     * @return boolean
193
     */
194
    protected function sendToQueue(): bool
195
    {
196
        $notificationData = [
197
            'from' => $this->fromUser,
198
            'to' => $this->toUser,
199
            'entity' => $this->entity,
200
            'type' => $this->type,
201
            'notification' => get_class($this)
202
        ];
203
204
        return Queue::send(Queue::NOTIFICATIONS, serialize($notificationData));
205
    }
206
207
    /**
208
     * Send the noficiatino to the places the user defined.
209
     *
210
     * @return boolean
211
     */
212
    public function trigger(): bool
213
    {
214
        $content = $this->message();
215
        $app = Di::getDefault()->getApp();
216
217
        //save to DB
218
        $notification = new Notifications();
219
        $notification->from_users_id = $this->fromUser->getId();
220
        $notification->users_id = $this->toUser->getId();
221
        $notification->companies_id = $this->fromUser->currentCompanyId();
222
        $notification->apps_id = $app->getId();
223
        $notification->system_modules_id = $this->type->system_modules_id;
224
        $notification->notification_type_id = $this->type->getId();
225
        $notification->entity_id = $this->entity->getId();
226
        $notification->content = $content;
227
        $notification->read = 0;
228
        $notification->saveOrFail();
229
230
        $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...
231
        if ($toMail instanceof Message) {
232
            $this->fire('notification:sendMail', $toMail);
233
        }
234
235
        $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...
236
        if ($toPushNotification instanceof PushNotification) {
237
            $this->fire('notification:sendPushNotification', $toPushNotification);
238
        }
239
240
        $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...
241
        if ($toRealtime instanceof PusherNotification) {
242
            $this->fire('notification:sendRealtimeNotification', $toRealtime);
243
        }
244
245
        /**
246
         * @todo send to push ontification
247
         */
248
249
        if ($this->type->with_realtime) {
250
            $this->fire('notification:sendRealtime', $this);
251
        }
252
253
        return true;
254
    }
255
}
256