Completed
Push — master ( 163cb0...ddb453 )
by Pascal
05:53
created

Media::addFilter()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 4
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 View Code Duplication
    public function getDurationInSeconds(): int
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...
37
    {
38
        $stream = $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...
39
40
        if ($stream->has('duration')) {
41
            return $stream->get('duration');
42
        }
43
44
        $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...
45
46
        if ($format->has('duration')) {
47
            return $format->get('duration');
48
        }
49
    }
50
51 View Code Duplication
    public function getDurationInMiliseconds(): float
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...
52
    {
53
        $stream = $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...
54
55
        if ($stream->has('duration')) {
56
            return $stream->get('duration') * 1000;
57
        }
58
59
        $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...
60
61
        if ($format->has('duration')) {
62
            return $format->get('duration') * 1000;
63
        }
64
    }
65
66
    public function export(): MediaExporter
67
    {
68
        return new MediaExporter($this);
69
    }
70
71
    public function exportForHLS(): HLSPlaylistExporter
72
    {
73
        return new HLSPlaylistExporter($this);
74
    }
75
76
    public function getFrameFromString(string $timecode): Frame
77
    {
78
        return $this->getFrameFromTimecode(
79
            TimeCode::fromString($timecode)
80
        );
81
    }
82
83
    public function getFrameFromSeconds(float $quantity): Frame
84
    {
85
        return $this->getFrameFromTimecode(
86
            TimeCode::fromSeconds($quantity)
87
        );
88
    }
89
90
    public function getFrameFromTimecode(TimeCode $timecode): Frame
91
    {
92
        $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...
93
94
        return new Frame($this->getFile(), $frame);
95
    }
96
97
    public function addFilter(): Media
98
    {
99
        $arguments = func_get_args();
100
101
        if (isset($arguments[0]) && $arguments[0] instanceof Closure) {
102
            call_user_func_array($arguments[0], [$this->media->filters()]);
103
        } else if (isset($arguments[0]) && $arguments[0] instanceof FilterInterface) {
104
            call_user_func_array([$this->media, 'addFilter'], $arguments);
105
        } else if (isset($arguments[0]) && is_array($arguments[0])) {
106
            $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...
107
        } else {
108
            $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...
109
        }
110
111
        return $this;
112
    }
113
114
    protected function selfOrArgument($argument)
115
    {
116
        return ($argument === $this->media) ? $this : $argument;
117
    }
118
119
    public function __invoke(): MediaTypeInterface
120
    {
121
        return $this->media;
122
    }
123
124
    public function __clone()
125
    {
126
        if ($this->media) {
127
            $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...
128
129
            $this->media = clone $this->media;
130
131
            $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...
132
        }
133
    }
134
135
    public function __call($method, $parameters)
136
    {
137
        return $this->selfOrArgument(
138
            call_user_func_array([$this->media, $method], $parameters)
139
        );
140
    }
141
}
142