Completed
Push — master ( af91ef...d75670 )
by Pascal
01:24
created

SegmentedExporter::getFullPathForFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
    private function getFullPathForFilename(string $filename)
58
    {
59
        return implode(DIRECTORY_SEPARATOR, [
60
            pathinfo($this->playlistPath, PATHINFO_DIRNAME), $filename,
61
        ]);
62
    }
63
64
    public function getPlaylistFullPath(): string
65
    {
66
        return $this->getFullPathForFilename(
67
            $this->getPlaylistFilename()
68
        );
69
    }
70
71
    public function getSegmentFullPath(): string
72
    {
73
        return $this->getFullPathForFilename(
74
            $this->getSegmentFilename()
75
        );
76
    }
77
78
    public function getPlaylistName(): string
79
    {
80
        return pathinfo($this->playlistPath, 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