Completed
Push — master ( 2ee038...4cced5 )
by Brian
04:00
created

PlaybackList   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 75%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 10
c 6
b 0
f 1
lcom 1
cbo 5
dl 0
loc 69
ccs 24
cts 32
cp 0.75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A offsetSet() 0 14 2
A append() 0 18 4
A stop() 0 11 3
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;
0 ignored issues
show
Coding Style Compatibility introduced by
The method offsetSet() contains an exit expression.

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.

Loading history...
52
            $this->offsetUnset($offset);
0 ignored issues
show
Unused Code introduced by
$this->offsetUnset($offset); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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
                if ($playbackFinished->getPlayback()->getId() === $value->getId()) {
70
                    $key = array_search($value, $this->getArrayCopy());
71
                    if ($key !== false) {
72
                        $this->offsetUnset($key);
73
                    }
74
                }
75 1
            });
76 1
    }
77
78
    /**
79
     * Stop all the playbacks
80
     */
81 1
    public function stop()
82
    {
83 1
        foreach (array_reverse($this->getArrayCopy()) as $playback) {
84
            /* @var $playback Playback */
85
            try {
86 1
                $this->phparia->playbacks()->stopPlayback($playback->getId());
87 1
            } catch (\Exception $ignore) {
88
                // Don't throw exception if the playback does not exist
89
            }
90 1
        }
91
    }
92
}