DeviceController::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 12
nc 2
nop 2
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 Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
/**
18
 * Class DeviceController
19
 */
20
class DeviceController extends AbstractController
21
{
22
    /**
23
     * @var DeviceRepository
24
     */
25
    protected $repository;
26
27
    /**
28
     * @param DeviceRepository $repository
29
     */
30
    public function __construct(DeviceRepository $repository)
31
    {
32
        $this->repository = $repository;
33
    }
34
35
    /**
36
     * @param ServerRequestInterface $request
37
     * @param ResponseInterface      $response
38
     *
39
     * @throws \InvalidArgumentException
40
     * @throws \RuntimeException
41
     *
42
     * @return ResponseInterface
43
     */
44
    public function save(ServerRequestInterface $request, ResponseInterface $response)
45
    {
46
        $parameters = $this->resolveRequestParameters(
47
            $request,
48
            ['token', 'platform'],
49
            [],
50
            ['token', 'platform']
51
        );
52
53
        /* @var DeviceEntity $device */
54
        $device = $this->repository->findOneBy(['token' => $parameters['token']]);
55
        if (!$device) {
56
            $device = new DeviceEntity();
57
            $device->setToken($parameters['token']);
58
            $device->setPlatform($parameters['platform']);
59
60
            $this->repository->saveDevice($device);
61
        }
62
63
        $response->getBody()->write(json_encode($device));
64
65
        return $response;
66
    }
67
68
    /**
69
     * @param ServerRequestInterface $request
70
     * @param ResponseInterface      $response
71
     *
72
     * @throws \InvalidArgumentException
73
     * @throws \RuntimeException
74
     *
75
     * @return ResponseInterface
76
     */
77
    public function update(ServerRequestInterface $request, ResponseInterface $response)
78
    {
79
        $parameters = $this->resolveRequestParameters(
80
            $request,
81
            ['token', 'previous_token'],
82
            [],
83
            ['token', 'previous_token']
84
        );
85
86
        /* @var DeviceEntity $device */
87
        $device = $this->repository->findOneBy(['token' => $parameters['previous_token']]);
88
        if (!$device) {
89
            throw new \RuntimeException('Impossible to find device with previous token');
90
        }
91
92
        $device->setToken($parameters['token']);
93
94
        $this->repository->saveDevice($device);
95
96
        $response->getBody()->write(json_encode($device));
97
98
        return $response;
99
    }
100
}
101