Completed
Push — master ( a76137...163cb0 )
by Pascal
69:06 queued 53:09
created

Media::getDurationInMiliseconds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

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