1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2015 Brian Smith <[email protected]>. |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace phparia\Api; |
19
|
|
|
|
20
|
|
|
use phparia\Client\Phparia; |
21
|
|
|
use phparia\Events\PlaybackFinished; |
22
|
|
|
use phparia\Resources\Playback; |
23
|
|
|
|
24
|
|
|
class PlaybackList extends \ArrayObject |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Phparia |
28
|
|
|
*/ |
29
|
|
|
protected $phparia; |
30
|
|
|
|
31
|
3 |
|
public function __construct(Phparia $phparia) |
32
|
|
|
{ |
33
|
3 |
|
$this->phparia = $phparia; |
34
|
3 |
|
parent::__construct(array(), \ArrayObject::ARRAY_AS_PROPS); |
35
|
3 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param mixed $offset |
39
|
|
|
* @param Playback $value |
40
|
|
|
*/ |
41
|
2 |
|
public function offsetSet($offset, $value) |
42
|
|
|
{ |
43
|
2 |
|
if (!$value instanceof Playback) { |
44
|
1 |
|
throw new \InvalidArgumentException("Value must be of type Playback"); |
45
|
|
|
} |
46
|
1 |
|
parent::offsetSet($offset, $value); |
47
|
|
|
|
48
|
|
|
// Remove playbacks when they are done playing |
49
|
|
|
$value->oncePlaybackFinished(function () use ($offset) { |
50
|
|
|
echo 'got here'; |
51
|
|
|
die; |
|
|
|
|
52
|
|
|
$this->offsetUnset($offset); |
|
|
|
|
53
|
1 |
|
}); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Playback $value |
58
|
|
|
*/ |
59
|
2 |
|
public function append($value) |
60
|
|
|
{ |
61
|
2 |
|
if (!$value instanceof Playback) { |
62
|
1 |
|
throw new \InvalidArgumentException("Value must be of type Playback"); |
63
|
|
|
} |
64
|
1 |
|
parent::append($value); |
65
|
|
|
|
66
|
|
|
// Remove playbacks when they are done playing |
67
|
1 |
|
$this->phparia->getWsClient()->once('PlaybackFinished', |
68
|
1 |
|
function (PlaybackFinished $playbackFinished) use ($value) { |
|
|
|
|
69
|
|
|
echo 'here'; |
70
|
|
|
die; |
|
|
|
|
71
|
|
|
if ($playbackFinished->getPlayback()->getId() === $value->getId()) { |
|
|
|
|
72
|
|
|
$key = array_search($value, $this->getArrayCopy()); |
73
|
|
|
if ($key !== false) { |
|
|
|
|
74
|
|
|
$this->offsetUnset($key); |
75
|
|
|
} |
76
|
|
|
} |
77
|
1 |
|
}); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Stop all the playbacks |
82
|
|
|
*/ |
83
|
1 |
|
public function stop() |
84
|
|
|
{ |
85
|
1 |
|
foreach (array_reverse($this->getArrayCopy()) as $playback) { |
86
|
|
|
/* @var $playback Playback */ |
87
|
|
|
try { |
88
|
1 |
|
$this->phparia->playbacks()->stopPlayback($playback->getId()); |
89
|
1 |
|
} catch (\Exception $ignore) { |
90
|
|
|
// Don't throw exception if the playback does not exist |
91
|
|
|
} |
92
|
1 |
|
} |
93
|
|
|
} |
94
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.