NotificationsTransformer::stringToNotifications()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 18
cts 18
cp 1
rs 8.8017
c 0
b 0
f 0
cc 6
nc 32
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaClient package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\PandaClient\Transformer;
13
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Xabbuh\PandaClient\Model\NotificationEvent;
16
use Xabbuh\PandaClient\Model\Notifications;
17
18
/**
19
 * Transformation from various data representation formats into Notifications
20
 * model objects and vice versa.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
class NotificationsTransformer extends BaseTransformer implements NotificationsTransformerInterface
25
{
26
    /**
27
     * {@inheritDoc}
28
     */
29 2
    public function stringToNotifications($jsonString)
30
    {
31 2
        $json = json_decode($jsonString);
32 2
        $notifications = new Notifications();
33
34 2
        if (isset($json->url)) {
35 1
            $notifications->setUrl($json->url);
36
        }
37
38 2
        if (isset($json->events->video_created)) {
39 2
            $notifications->addNotificationEvent(
40 2
                new NotificationEvent('video_created', $json->events->video_created));
41
        }
42
43 2
        if (isset($json->events->video_encoded)) {
44 1
            $notifications->addNotificationEvent(
45 1
                new NotificationEvent('video_encoded', $json->events->video_encoded));
46
        }
47
48 2
        if (isset($json->events->encoding_progress)) {
49 1
            $notifications->addNotificationEvent(
50 1
                new NotificationEvent('encoding_progress', $json->events->encoding_progress));
51
        }
52
53 2
        if (isset($json->events->encoding_completed)) {
54 2
            $notifications->addNotificationEvent(
55 2
                new NotificationEvent('encoding_completed', $json->events->encoding_completed));
56
        }
57
58 2
        return $notifications;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 2
    public function toRequestParams(Notifications $notifications)
65
    {
66 2
        $params = new ParameterBag();
67
68 2
        if (null !== $notifications->getUrl()) {
69 1
            $params->set('url', $notifications->getUrl());
70
        }
71
72 2
        if ($notifications->hasNotificationEvent('video-created')) {
73 2
            $this->addRequestParamForEvent($notifications, 'video_created', $params);
74
        }
75
76 2
        if ($notifications->hasNotificationEvent('video-encoded')) {
77 1
            $this->addRequestParamForEvent($notifications, 'video_encoded', $params);
78
        }
79
80 2
        if ($notifications->hasNotificationEvent('encoding-progress')) {
81 1
            $this->addRequestParamForEvent($notifications, 'encoding_progress', $params);
82
        }
83
84 2
        if ($notifications->hasNotificationEvent('encoding-completed')) {
85 2
            $this->addRequestParamForEvent($notifications, 'encoding_completed', $params);
86
        }
87
88 2
        return $params;
89
    }
90
91 2
    private function addRequestParamForEvent(Notifications $notifications, $eventName, ParameterBag $params)
92
    {
93 2
        $event = $notifications->getNotificationEvent($eventName);
94
95 2
        if ($event->isActive()) {
96 1
            $params->set('events['.$eventName.']', 'true');
97
        } else {
98 2
            $params->set('events['.$eventName.']', 'false');
99
        }
100 2
    }
101
}
102