Completed
Push — master ( cf2713...aee5ae )
by Sherif
02:07
created

PushNotificationDeviceService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A registerDevice() 0 17 2
A generateMessageData() 0 16 1
1
<?php namespace App\Modules\PushNotificationDevices\Services;
2
3
use App\Modules\Core\BaseClasses\BaseService;
4
use LaravelFCM\Message\OptionsBuilder;
5
use LaravelFCM\Message\PayloadDataBuilder;
6
use LaravelFCM\Message\PayloadNotificationBuilder;
7
use App\Modules\PushNotificationDevices\Repositories\PushNotificationDeviceRepository;
8
9
class PushNotificationDeviceService extends BaseService
10
{
11
    /**
12
     * Init new object.
13
     *
14
     * @param   PushNotificationDeviceRepository $repo
15
     * @return  void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
16
     */
17
    public function __construct(PushNotificationDeviceRepository $repo)
18
    {
19
        parent::__construct($repo);
20
    }
21
22
    /**
23
     * Register the given device to the logged in user.
24
     *
25
     * @param  array $data
26
     * @return void
27
     */
28
    public function registerDevice($data)
29
    {
30
        $data['access_token'] = \Auth::user()->token();
31
        $data['user_id']      = \Auth::id();
32
        $device               = $this->repo->first([
33
            'and' => [
34
                'device_token' => $data['device_token'], 
35
                'user_id' => $data['user_id']
36
                ]
37
            ]);
38
39
        if ($device) {
40
            $data['id'] = $device->id;
41
        }
42
43
        return $this->repo->save($data);
44
    }
45
46
    /**
47
     * Generate the given message data.
48
     *
49
     * @param  string $title
50
     * @param  string $message
51
     * @param  array  $data
52
     * @return void
53
     */
54
    public function generateMessageData($title, $message, $data = [])
55
    {
56
        $optionBuilder       = new OptionsBuilder();
57
        $notificationBuilder = new PayloadNotificationBuilder($title);
58
        $dataBuilder         = new PayloadDataBuilder();
59
60
        $optionBuilder->setTimeToLive(60 * 20);
61
        $notificationBuilder->setBody($message);
62
        $dataBuilder->addData($data);
63
64
        $options             = $optionBuilder->build();
65
        $notification        = $notificationBuilder->build();
66
        $data                = $dataBuilder->build();
67
68
        return compact('options', 'notification', 'data');
69
    }
70
}
71