Completed
Push — master ( ecb3b3...72d27f )
by Brian
09:30
created

PlaybackList::append()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
ccs 8
cts 10
cp 0.8
rs 9.2
cc 4
eloc 12
nc 2
nop 1
crap 4.128
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $playbackFinished is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
                echo 'here';
70
                die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method append() 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...
71
                if ($playbackFinished->getPlayback()->getId() === $value->getId()) {
0 ignored issues
show
Unused Code introduced by
if ($playbackFinished->g...setUnset($key); } } 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...
Bug introduced by
The variable $playbackFinished seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
72
                    $key = array_search($value, $this->getArrayCopy());
73
                    if ($key !== false) {
0 ignored issues
show
Bug introduced by
The variable $key seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
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
}