Completed
Pull Request — 1.x (#56)
by Christian
04:46 queued 03:43
created

EventFactory::createVideoEncodedEventFromRequest()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 8
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 3
nop 1
crap 42
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaBundle 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\PandaBundle\Event;
13
14
use Symfony\Component\EventDispatcher\Event;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Create Panda events based on requests.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class EventFactory
23
{
24
    /**
25
     * Creates a Panda event from the given request.
26
     *
27
     * @param Request $request The request to analyse
28
     *
29
     * @return Event The created event
30
     *
31
     * @throws \InvalidArgumentException if the request could not be converted into a valid event
32
     */
33
    public static function createEventFromRequest(Request $request)
34
    {
35
        $eventName = 'xabbuh_panda.'.strtr($request->request->get('event'), '-', '_');
36
37
        switch ($eventName) {
38
            case VideoCreatedEvent::NAME:
39
                return self::createVideoCreatedEventFromRequest($request);
40
            case VideoEncodedEvent::NAME:
41
                return self::createVideoEncodedEventFromRequest($request);
42
            case EncodingProgressEvent::NAME:
43
                return self::createEncodingProgressEventFromRequest($request);
44
            case EncodingCompleteEvent::NAME:
45
                return self::createEncodingCompleteEventFromRequest($request);
46
            default:
47
                throw new \InvalidArgumentException(
48
                    'The request does not contain a valid event name'
49
                );
50
        }
51
    }
52
53
    /**
54
     * Creates an EncodingCompleteEvent from the given request.
55
     *
56
     * @param Request $request The request to analyse
57
     *
58
     * @return EncodingCompleteEvent The created event
59
     *
60
     * @throws \InvalidArgumentException if some necessary data is not provided
61
     */
62
    public static function createEncodingCompleteEventFromRequest(Request $request)
63
    {
64
        $videoId = $request->request->get('video_id');
65
        $encodingId = $request->request->get('encoding_id');
66
67
        if (null === $videoId || !is_string($videoId)) {
68
            throw new \InvalidArgumentException('no valid video id given');
69
        }
70
71
        if (null === $encodingId || !is_string($encodingId)) {
72
            throw new \InvalidArgumentException('no valid encoding id given');
73
        }
74
75
        return new EncodingCompleteEvent($videoId, $encodingId);
76
    }
77
78
    /**
79
     * Creates an EncodingProgressEvent from the given request.
80
     *
81
     * @param Request $request The request to analyze
82
     *
83
     * @return EncodingProgressEvent The created event
84
     *
85
     * @throws \InvalidArgumentException if some necessary data is not provided
86
     */
87
    public static function createEncodingProgressEventFromRequest(Request $request)
88
    {
89
        $videoId = $request->request->get('video_id');
90
        $encodingId = $request->request->get('encoding_id');
91
        $progress = $request->request->get('progress');
92
93
        if (null === $videoId || !is_string($videoId)) {
94
            throw new \InvalidArgumentException('no valid video id given');
95
        }
96
97
        if (null === $encodingId || !is_string($encodingId)) {
98
            throw new \InvalidArgumentException('no valid encoding id given');
99
        }
100
101
        if (null === $progress || !is_int($progress)) {
102
            throw new \InvalidArgumentException('no valid progress given');
103
        }
104
105
        return new EncodingProgressEvent($videoId, $encodingId, $progress);
106
    }
107
108
    /**
109
     * Creates a VideoCreatedEvent from the given request.
110
     *
111
     * @param Request $request The request to analyze
112
     *
113
     * @return VideoCreatedEvent The created event
114
     *
115
     * @throws \InvalidArgumentException if some necessary data is not provided
116
     */
117
    public static function createVideoCreatedEventFromRequest(Request $request)
118
    {
119
        $videoId = $request->request->get('video_id');
120
        $encodingIds = $request->request->get('encoding_ids');
121
122
        if (null === $videoId || !is_string($videoId)) {
123
            throw new \InvalidArgumentException('no valid video id given');
124
        }
125
126
        if (null === $encodingIds || !is_array($encodingIds) || 0 == count($encodingIds)) {
127
            throw new \InvalidArgumentException('no valid encoding ids given');
128
        }
129
130
        return new VideoCreatedEvent($videoId, $encodingIds);
131
    }
132
133
    /**
134
     * Creates a VideoEncodedEvent from the given request.
135
     *
136
     * @param Request $request The request to analyze
137
     *
138
     * @return VideoEncodedEvent The created event
139
     *
140
     * @throws \InvalidArgumentException if some necessary data is not provided
141
     */
142
    public static function createVideoEncodedEventFromRequest(Request $request)
143
    {
144
        $videoId = $request->request->get('video_id');
145
        $encodingIds = $request->request->get('encoding_ids');
146
147
        if (null === $videoId || !is_string($videoId)) {
148
            throw new \InvalidArgumentException('no valid video id given');
149
        }
150
151
        if (null === $encodingIds || !is_array($encodingIds) || 0 == count($encodingIds)) {
152
            throw new \InvalidArgumentException('no valid encoding ids given');
153
        }
154
155
        return new VideoEncodedEvent($videoId, $encodingIds);
156
    }
157
}
158