|
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
|
|
|
protected $playbacks = []; |
|
32
|
|
|
|
|
33
|
2 |
|
public function __construct(Phparia $phparia) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->phparia = $phparia; |
|
36
|
2 |
|
parent::__construct($this->playbacks, \ArrayObject::ARRAY_AS_PROPS); |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param mixed $offset |
|
41
|
|
|
* @param Playback $value |
|
42
|
|
|
*/ |
|
43
|
1 |
View Code Duplication |
public function offsetSet($offset, $value) |
|
44
|
|
|
{ |
|
45
|
1 |
|
if (!$value instanceof Playback) { |
|
46
|
1 |
|
throw new \InvalidArgumentException("Value must be of type Playback"); |
|
47
|
|
|
} |
|
48
|
|
|
parent::offsetSet($offset, $value); |
|
49
|
|
|
|
|
50
|
|
|
// Remove playbacks when they are done playing |
|
51
|
|
|
$value->oncePlaybackFinished(function (PlaybackFinished $playbackFinished) use ($value) { |
|
52
|
|
|
if ($playbackFinished->getPlayback()->getId() === $value->getId()) { |
|
53
|
|
|
$key = array_search($value, $this->getArrayCopy()); |
|
54
|
|
|
if ($key !== false) { |
|
55
|
|
|
$this->offsetUnset($key); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Playback $value |
|
63
|
|
|
*/ |
|
64
|
1 |
View Code Duplication |
public function append($value) |
|
65
|
|
|
{ |
|
66
|
1 |
|
if (!$value instanceof Playback) { |
|
67
|
1 |
|
throw new \InvalidArgumentException("Value must be of type Playback"); |
|
68
|
|
|
} |
|
69
|
|
|
parent::append($value); |
|
70
|
|
|
|
|
71
|
|
|
// Remove playbacks when they are done playing |
|
72
|
|
|
$this->phparia->getWsClient()->once('PlaybackFinished', |
|
73
|
|
|
function (PlaybackFinished $playbackFinished) use ($value) { |
|
74
|
|
|
if ($playbackFinished->getPlayback()->getId() === $value->getId()) { |
|
75
|
|
|
$key = array_search($value, $this->getArrayCopy()); |
|
76
|
|
|
if ($key !== false) { |
|
77
|
|
|
$this->offsetUnset($key); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
}); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Stop all the playbacks |
|
85
|
|
|
*/ |
|
86
|
|
|
public function stop() |
|
87
|
|
|
{ |
|
88
|
|
|
foreach (array_reverse($this->getArrayCopy()) as $playback) { |
|
89
|
|
|
/* @var $playback Playback */ |
|
90
|
|
|
try { |
|
91
|
|
|
$this->phparia->playbacks()->stopPlayback($playback->getId()); |
|
92
|
|
|
} catch (\Exception $ignore) { |
|
93
|
|
|
// Don't throw exception if the playback does not exist |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |