NotificationController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 10
dl 0
loc 81
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B send() 0 49 6
1
<?php
2
/**
3
 * Push notification server example (http://github.com/juliangut/tify_example)
4
 *
5
 * @link https://github.com/juliangut/tify_example for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify_example/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Pusher\Controller;
11
12
use Jgut\Pusher\Entity\DeviceEntity;
13
use Jgut\Pusher\Repository\DeviceRepository;
14
use Jgut\Tify\Message;
15
use Jgut\Tify\Notification;
16
use Jgut\Tify\Receiver\ApnsReceiver;
17
use Jgut\Tify\Receiver\GcmReceiver;
18
use Jgut\Tify\Service;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
22
/**
23
 * Class NotificationController
24
 */
25
class NotificationController extends AbstractController
26
{
27
    /**
28
     * @var DeviceRepository
29
     */
30
    protected $repository;
31
32
    /**
33
     * @var Service
34
     */
35
    protected $tify;
36
37
    /**
38
     * @param DeviceRepository $repository
39
     * @param Service          $tify
40
     */
41
    public function __construct(DeviceRepository $repository, Service $tify)
42
    {
43
        $this->repository = $repository;
44
        $this->tify = $tify;
45
    }
46
47
    /**
48
     * @param ServerRequestInterface $request
49
     * @param ResponseInterface      $response
50
     *
51
     * @throws \InvalidArgumentException
52
     * @throws \RuntimeException
53
     *
54
     * @return ResponseInterface
55
     */
56
    public function send(ServerRequestInterface $request, ResponseInterface $response)
57
    {
58
        $parameters = $this->resolveRequestParameters(
59
            $request,
60
            ['title', 'body', 'platform', 'data'],
61
            [],
62
            ['title', 'body']
63
        );
64
65
        if (array_key_exists('platform', $parameters)) {
66
            $devices = $this->repository->findBy(['platform' => $parameters['platform']]);
67
        } else {
68
            $devices = $this->repository->findAll();
69
        }
70
71
        /* @var DeviceEntity[] $devices */
72
        if (count($devices)) {
73
            /* @var \Jgut\Tify\Receiver\AbstractReceiver[] $receivers */
74
            $receivers = array_map(
75
                function (DeviceEntity $device) {
76
                    switch ($device->getPlatform()) {
77
                        case DeviceEntity::PLATFORM_ANDROID:
78
                            return new GcmReceiver($device->getToken());
79
80
                        case DeviceEntity::PLATFORM_IOS:
81
                            return new ApnsReceiver($device->getToken());
82
                    }
83
84
                    return;
85
                },
86
                $devices
87
            );
88
89
            $message = new Message([
90
                'title' => $parameters['title'],
91
                'body' => $parameters['body'],
92
            ]);
93
94
            if (array_key_exists('data', $parameters)) {
95
                $message->setPayloadData($parameters['data']);
96
            }
97
98
            $this->tify->addNotification(new Notification($message, array_filter($receivers)));
99
        }
100
101
        $response->getBody()->write(json_encode($this->tify->push()));
102
103
        return $response;
104
    }
105
}
106