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
|
|
|
$this->offsetUnset($offset); |
51
|
1 |
|
}); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Playback $value |
56
|
|
|
*/ |
57
|
2 |
|
public function append($value) |
58
|
|
|
{ |
59
|
2 |
|
if (!$value instanceof Playback) { |
60
|
1 |
|
throw new \InvalidArgumentException("Value must be of type Playback"); |
61
|
|
|
} |
62
|
1 |
|
parent::append($value); |
63
|
|
|
|
64
|
|
|
// Remove playbacks when they are done playing |
65
|
1 |
|
$this->phparia->getWsClient()->once('PlaybackFinished', |
66
|
1 |
|
function (PlaybackFinished $playbackFinished) use ($value) { |
67
|
|
|
if ($playbackFinished->getPlayback()->getId() === $value->getId()) { |
68
|
|
|
$key = array_search($value, $this->getArrayCopy()); |
69
|
|
|
if ($key !== false) { |
70
|
|
|
$this->offsetUnset($key); |
71
|
|
|
} |
72
|
|
|
} |
73
|
1 |
|
}); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Stop all the playbacks |
78
|
|
|
*/ |
79
|
1 |
|
public function stop() |
80
|
|
|
{ |
81
|
1 |
|
foreach (array_reverse($this->getArrayCopy()) as $playback) { |
82
|
|
|
/* @var $playback Playback */ |
83
|
|
|
try { |
84
|
1 |
|
$this->phparia->playbacks()->stopPlayback($playback->getId()); |
85
|
1 |
|
} catch (\Exception $ignore) { |
86
|
|
|
// Don't throw exception if the playback does not exist |
87
|
|
|
} |
88
|
1 |
|
} |
89
|
|
|
} |
90
|
|
|
} |