Completed
Push — master ( d75670...195c2a )
by Pascal
01:46
created

Media::getFirstStream()   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\Filters\Audio\SimpleFilter;
8
use FFMpeg\Filters\FilterInterface;
9
use FFMpeg\Media\MediaTypeInterface;
10
11
/**
12
 * @method mixed save(\FFMpeg\Format\FormatInterface $format, $outputPathfile)
13
 */
14
class Media
15
{
16
    protected $file;
17
18
    protected $media;
19
20
    public function __construct(File $file, MediaTypeInterface $media)
21
    {
22
        $this->file  = $file;
23
        $this->media = $media;
24
    }
25
26
    public function isFrame(): bool
27
    {
28
        return $this instanceof Frame;
29
    }
30
31
    public function getFile(): File
32
    {
33
        return $this->file;
34
    }
35
36
    public function getDurationInSeconds(): int
37
    {
38
        return $this->getDurationInMiliseconds() / 1000;
39
    }
40
41
    public function getFirstStream()
42
    {
43
        return $this->media->getStreams()->first();
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...
44
    }
45
46
    public function getDurationInMiliseconds(): float
47
    {
48
        $stream = $this->getFirstStream();
49
50
        if ($stream->has('duration')) {
51
            return $stream->get('duration') * 1000;
52
        }
53
54
        $format = $this->media->getFormat();
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 getFormat() 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...
55
56
        if ($format->has('duration')) {
57
            return $format->get('duration') * 1000;
58
        }
59
    }
60
61
    public function export(): MediaExporter
62
    {
63
        return new MediaExporter($this);
64
    }
65
66
    public function exportForHLS(): HLSPlaylistExporter
67
    {
68
        return new HLSPlaylistExporter($this);
69
    }
70
71
    public function getFrameFromString(string $timecode): Frame
72
    {
73
        return $this->getFrameFromTimecode(
74
            TimeCode::fromString($timecode)
75
        );
76
    }
77
78
    public function getFrameFromSeconds(float $quantity): Frame
79
    {
80
        return $this->getFrameFromTimecode(
81
            TimeCode::fromSeconds($quantity)
82
        );
83
    }
84
85
    public function getFrameFromTimecode(TimeCode $timecode): Frame
86
    {
87
        $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...
88
89
        return new Frame($this->getFile(), $frame);
90
    }
91
92
    public function addFilter(): Media
93
    {
94
        $arguments = func_get_args();
95
96
        if (isset($arguments[0]) && $arguments[0] instanceof Closure) {
97
            call_user_func_array($arguments[0], [$this->media->filters()]);
98
        } else if (isset($arguments[0]) && $arguments[0] instanceof FilterInterface) {
99
            call_user_func_array([$this->media, 'addFilter'], $arguments);
100
        } else if (isset($arguments[0]) && is_array($arguments[0])) {
101
            $this->media->addFilter(new SimpleFilter($arguments[0]));
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 addFilter() does only exist in the following implementations of said interface: FFMpeg\Media\Audio, FFMpeg\Media\Concat, FFMpeg\Media\Frame, FFMpeg\Media\Gif, FFMpeg\Media\Video, FFMpeg\Media\Waveform.

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...
102
        } else {
103
            $this->media->addFilter(new SimpleFilter($arguments));
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 addFilter() does only exist in the following implementations of said interface: FFMpeg\Media\Audio, FFMpeg\Media\Concat, FFMpeg\Media\Frame, FFMpeg\Media\Gif, FFMpeg\Media\Video, FFMpeg\Media\Waveform.

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...
104
        }
105
106
        return $this;
107
    }
108
109
    protected function selfOrArgument($argument)
110
    {
111
        return ($argument === $this->media) ? $this : $argument;
112
    }
113
114
    public function __invoke(): MediaTypeInterface
115
    {
116
        return $this->media;
117
    }
118
119
    public function __clone()
120
    {
121
        if ($this->media) {
122
            $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...
123
124
            $this->media = clone $this->media;
125
126
            $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...
127
        }
128
    }
129
130
    public function __call($method, $parameters)
131
    {
132
        return $this->selfOrArgument(
133
            call_user_func_array([$this->media, $method], $parameters)
134
        );
135
    }
136
}
137