Completed
Push — master ( 4cced5...7f1d8b )
by Brian
05:26
created

PlaybackList::append()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2597

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 18
ccs 8
cts 14
cp 0.5714
rs 9.2
cc 4
eloc 10
nc 2
nop 1
crap 5.2597
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
}