Completed
Push — master ( 262094...52cd34 )
by Pascal
02:13
created

SegmentedExporter::getPlaylistFullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg;
4
5
class SegmentedExporter extends MediaExporter
6
{
7
    protected $filter;
8
9
    protected $playlistPath;
10
11
    protected $segmentLength = 10;
12
13
    protected $saveMethod = 'saveStream';
14
15
    public function setPlaylistPath(string $playlistPath): MediaExporter
16
    {
17
        $this->playlistPath = $playlistPath;
18
19
        return $this;
20
    }
21
22
    public function setSegmentLength(int $segmentLength): MediaExporter
23
    {
24
        $this->segmentLength = $segmentLength;
25
26
        return $this;
27
    }
28
29
    public function getFilter(): SegmentedFilter
30
    {
31
        if ($this->filter) {
32
            return $this->filter;
33
        }
34
35
        return $this->filter = new SegmentedFilter(
36
            $this->getPlaylistFullPath(),
37
            $this->segmentLength
38
        );
39
    }
40
41
    public function saveStream(string $playlistPath): MediaExporter
42
    {
43
        $this->setPlaylistPath($playlistPath);
44
45
        $this->media->addFilter(
46
            $this->getFilter()
47
        );
48
49
        $this->media->save(
50
            $this->getFormat(),
51
            $this->getSegmentFullPath()
52
        );
53
54
        return $this;
55
    }
56
57 View Code Duplication
    public function getPlaylistFullPath(): string
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...
58
    {
59
        return implode(DIRECTORY_SEPARATOR, [
60
            pathinfo($this->playlistPath, PATHINFO_DIRNAME),
61
            $this->getPlaylistFilename(),
62
        ]);
63
    }
64
65 View Code Duplication
    public function getSegmentFullPath(): string
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...
66
    {
67
        return implode(DIRECTORY_SEPARATOR, [
68
            pathinfo($this->playlistPath, PATHINFO_DIRNAME),
69
            $this->getSegmentFilename(),
70
        ]);
71
    }
72
73
    public function getPlaylistPath(): string
74
    {
75
        return $this->playlistPath;
76
    }
77
78
    public function getPlaylistName(): string
79
    {
80
        return pathinfo($this->getPlaylistPath(), PATHINFO_FILENAME);
81
    }
82
83
    public function getPlaylistFilename(): string
84
    {
85
        return $this->getFormattedFilename('.m3u8');
86
    }
87
88
    public function getSegmentFilename(): string
89
    {
90
        return $this->getFormattedFilename('_%05d.ts');
91
    }
92
93
    protected function getFormattedFilename(string $suffix = ''): string
94
    {
95
        return implode([
96
            $this->getPlaylistName(),
97
            '_',
98
            $this->getFormat()->getKiloBitrate(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FFMpeg\Format\FormatInterface as the method getKiloBitrate() does only exist in the following implementations of said interface: FFMpeg\Format\Video\DefaultVideo, FFMpeg\Format\Video\Ogg, FFMpeg\Format\Video\WMV, FFMpeg\Format\Video\WMV3, FFMpeg\Format\Video\WebM, FFMpeg\Format\Video\X264.

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...
99
        ]) . $suffix;
100
    }
101
}
102