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
|
10 |
|
public static function createEventFromRequest(Request $request) |
34
|
|
|
{ |
35
|
10 |
|
$eventName = 'xabbuh_panda.'.strtr($request->request->get('event'), '-', '_'); |
36
|
|
|
|
37
|
|
|
switch ($eventName) { |
38
|
10 |
|
case VideoCreatedEvent::NAME: |
39
|
2 |
|
return self::createVideoCreatedEventFromRequest($request); |
40
|
8 |
|
case VideoEncodedEvent::NAME: |
41
|
2 |
|
return self::createVideoEncodedEventFromRequest($request); |
42
|
6 |
|
case EncodingProgressEvent::NAME: |
43
|
2 |
|
return self::createEncodingProgressEventFromRequest($request); |
44
|
4 |
|
case EncodingCompleteEvent::NAME: |
45
|
2 |
|
return self::createEncodingCompleteEventFromRequest($request); |
46
|
|
|
default: |
47
|
2 |
|
throw new \InvalidArgumentException( |
48
|
2 |
|
'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
|
3 |
|
public static function createEncodingCompleteEventFromRequest(Request $request) |
63
|
|
|
{ |
64
|
3 |
|
$videoId = $request->request->get('video_id'); |
65
|
3 |
|
$encodingId = $request->request->get('encoding_id'); |
66
|
|
|
|
67
|
3 |
|
if (null === $videoId || !is_string($videoId)) { |
68
|
|
|
throw new \InvalidArgumentException('no valid video id given'); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
if (null === $encodingId || !is_string($encodingId)) { |
72
|
|
|
throw new \InvalidArgumentException('no valid encoding id given'); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
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
|
3 |
|
public static function createEncodingProgressEventFromRequest(Request $request) |
88
|
|
|
{ |
89
|
3 |
|
$videoId = $request->request->get('video_id'); |
90
|
3 |
|
$encodingId = $request->request->get('encoding_id'); |
91
|
3 |
|
$progress = $request->request->get('progress'); |
92
|
|
|
|
93
|
3 |
|
if (null === $videoId || !is_string($videoId)) { |
94
|
|
|
throw new \InvalidArgumentException('no valid video id given'); |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
if (null === $encodingId || !is_string($encodingId)) { |
98
|
|
|
throw new \InvalidArgumentException('no valid encoding id given'); |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
if (null === $progress || !is_int($progress)) { |
102
|
|
|
throw new \InvalidArgumentException('no valid progress given'); |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
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
|
3 |
|
public static function createVideoCreatedEventFromRequest(Request $request) |
118
|
|
|
{ |
119
|
3 |
|
$videoId = $request->request->get('video_id'); |
120
|
3 |
|
$encodingIds = $request->request->get('encoding_ids'); |
121
|
|
|
|
122
|
3 |
|
if (null === $videoId || !is_string($videoId)) { |
123
|
|
|
throw new \InvalidArgumentException('no valid video id given'); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
if (null === $encodingIds || !is_array($encodingIds) || 0 == count($encodingIds)) { |
127
|
|
|
throw new \InvalidArgumentException('no valid encoding ids given'); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
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
|
3 |
|
public static function createVideoEncodedEventFromRequest(Request $request) |
143
|
|
|
{ |
144
|
3 |
|
$videoId = $request->request->get('video_id'); |
145
|
3 |
|
$encodingIds = $request->request->get('encoding_ids'); |
146
|
|
|
|
147
|
3 |
|
if (null === $videoId || !is_string($videoId)) { |
148
|
|
|
throw new \InvalidArgumentException('no valid video id given'); |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
if (null === $encodingIds || !is_array($encodingIds) || 0 == count($encodingIds)) { |
152
|
|
|
throw new \InvalidArgumentException('no valid encoding ids given'); |
153
|
|
|
} |
154
|
|
|
|
155
|
3 |
|
return new VideoEncodedEvent($videoId, $encodingIds); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|