Completed
Push — master ( b322c4...679efe )
by Pascal
05:28
created

Media::getDurationInSeconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg;
4
5
use Closure;
6
use FFMpeg\Coordinate\TimeCode;
7
use FFMpeg\Media\MediaTypeInterface;
8
9
/**
10
 * @method mixed save(\FFMpeg\Format\FormatInterface $format, $outputPathfile)
11
 */
12
class Media
13
{
14
    protected $file;
15
16
    protected $media;
17
18
    public function __construct(File $file, MediaTypeInterface $media)
19
    {
20
        $this->file  = $file;
21
        $this->media = $media;
22
    }
23
24
    public function isFrame(): bool
25
    {
26
        return $this instanceof Frame;
27
    }
28
29
    public function getFile(): File
30
    {
31
        return $this->file;
32
    }
33
34
    public function getDurationInSeconds(): int
35
    {
36
        return $this->media->getStreams()->first()->get('duration');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FFMpeg\Media\MediaTypeInterface as the method getStreams() does only exist in the following implementations of said interface: FFMpeg\Media\AbstractStreamableMedia, FFMpeg\Media\Audio, FFMpeg\Media\Video.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
37
    }
38
39
    public function export(): MediaExporter
40
    {
41
        return new MediaExporter($this);
42
    }
43
44
    public function exportForHLS(): HLSPlaylistExporter
45
    {
46
        return new HLSPlaylistExporter($this);
47
    }
48
49
    public function getFrameFromString(string $timecode): Frame
50
    {
51
        return $this->getFrameFromTimecode(
52
            TimeCode::fromString($timecode)
53
        );
54
    }
55
56
    public function getFrameFromSeconds(float $quantity): Frame
57
    {
58
        return $this->getFrameFromTimecode(
59
            TimeCode::fromSeconds($quantity)
60
        );
61
    }
62
63
    public function getFrameFromTimecode(TimeCode $timecode): Frame
64
    {
65
        $frame = $this->media->frame($timecode);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FFMpeg\Media\MediaTypeInterface as the method frame() does only exist in the following implementations of said interface: FFMpeg\Media\Video.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
66
67
        return new Frame($this->getFile(), $frame);
68
    }
69
70
    public function addFilter(): Media
71
    {
72
        $arguments = func_get_args();
73
74
        if (isset($arguments[0]) && $arguments[0] instanceof Closure) {
75
            call_user_func_array($arguments[0], [$this->media->filters()]);
76
        } else {
77
            call_user_func_array([$this->media, 'addFilter'], $arguments);
78
        }
79
80
        return $this;
81
    }
82
83
    protected function selfOrArgument($argument)
84
    {
85
        return ($argument === $this->media) ? $this : $argument;
86
    }
87
88
    public function __invoke(): MediaTypeInterface
89
    {
90
        return $this->media;
91
    }
92
93
    public function __clone()
94
    {
95
        if ($this->media) {
96
            $clonedFilters = clone $this->media->getFiltersCollection();
0 ignored issues
show
Bug introduced by
The method getFiltersCollection() does not exist on FFMpeg\Media\MediaTypeInterface. Did you maybe mean filters()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
97
98
            $this->media = clone $this->media;
99
100
            $this->media->setFiltersCollection($clonedFilters);
0 ignored issues
show
Bug introduced by
The method setFiltersCollection() does not exist on FFMpeg\Media\MediaTypeInterface. Did you maybe mean filters()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
101
        }
102
    }
103
104
    public function __call($method, $parameters)
105
    {
106
        return $this->selfOrArgument(
107
            call_user_func_array([$this->media, $method], $parameters)
108
        );
109
    }
110
}
111