Completed
Push — master ( e9cf55...72c4df )
by Brian
03:18
created

PlaybackList   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 47.3 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.44%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 12
c 8
b 1
f 1
lcom 1
cbo 5
dl 35
loc 74
ccs 34
cts 36
cp 0.9444
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A offsetSet() 17 17 4
A append() 18 18 4
A stop() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 5
    public function __construct(Phparia $phparia)
34
    {
35 5
        $this->phparia = $phparia;
36 5
        parent::__construct($this->playbacks, \ArrayObject::ARRAY_AS_PROPS);
37 5
    }
38
39
    /**
40
     * @param mixed $offset
41
     * @param Playback $value
42
     */
43 4 View Code Duplication
    public function offsetSet($offset, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45 4
        if (!$value instanceof Playback) {
46 1
            throw new \InvalidArgumentException("Value must be of type Playback");
47
        }
48 3
        parent::offsetSet($offset, $value);
49
50
        // Remove playbacks when they are done playing
51
        $value->oncePlaybackFinished(function (PlaybackFinished $playbackFinished) use ($value) {
52 2
            if ($playbackFinished->getPlayback()->getId() === $value->getId()) {
53 2
                $key = array_search($value, $this->getArrayCopy());
54 2
                if ($key !== false) {
55 2
                    $this->offsetUnset($key);
56 2
                }
57 2
            }
58 3
        });
59 3
    }
60
61
    /**
62
     * @param Playback $value
63
     */
64 3 View Code Duplication
    public function append($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66 3
        if (!$value instanceof Playback) {
67 1
            throw new \InvalidArgumentException("Value must be of type Playback");
68
        }
69 2
        parent::append($value);
70
71
        // Remove playbacks when they are done playing
72 2
        $this->phparia->getWsClient()->once('PlaybackFinished',
73 2
            function (PlaybackFinished $playbackFinished) use ($value) {
74 1
                if ($playbackFinished->getPlayback()->getId() === $value->getId()) {
75 1
                    $key = array_search($value, $this->getArrayCopy());
76 1
                    if ($key !== false) {
77
                        $this->offsetUnset($key);
78
                    }
79 1
                }
80 2
            });
81 2
    }
82
83
    /**
84
     * Stop all the playbacks
85
     */
86 1
    public function stop()
87
    {
88 1
        foreach (array_reverse($this->getArrayCopy()) as $playback) {
89
            /* @var $playback Playback */
90
            try {
91 1
                $this->phparia->playbacks()->stopPlayback($playback->getId());
92 1
            } catch (\Exception $ignore) {
93
                // Don't throw exception if the playback does not exist
94
            }
95 1
        }
96
    }
97
}