Passed
Pull Request — master (#265)
by Pascal
05:56
created

WatermarkFilter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 17
c 4
b 0
f 0
dl 0
loc 62
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizePath() 0 3 2
A normalizeWindowsPath() 0 6 1
A __construct() 0 5 1
A wrapInParentheses() 0 5 1
A getCommands() 0 13 2
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filters;
4
5
use FFMpeg\Filters\Video\WatermarkFilter as FFMpegWatermarkFilter;
6
7
/**
8
 * Partly based on this PR:
9
 * https://github.com/PHP-FFMpeg/PHP-FFMpeg/pull/754/files
10
 */
11
class WatermarkFilter extends FFMpegWatermarkFilter
12
{
13
    protected $path;
14
    protected $wrapInParentheses = false;
15
16
    public function __construct($watermarkPath, array $coordinates = [], $priority = 0)
17
    {
18
        parent::__construct($watermarkPath, $coordinates, $priority);
19
20
        $this->path = $watermarkPath;
21
    }
22
23
    public function wrapInParentheses(): self
24
    {
25
        $this->wrapInParentheses = true;
26
27
        return $this;
28
    }
29
30
    /**
31
     * Gets the commands from the base filter and normalizes the path.
32
     *
33
     * @return array
34
     */
35
    protected function getCommands()
36
    {
37
        $commands = parent::getCommands();
38
39
        $replace = static::normalizePath($this->path);
40
41
        if ($this->wrapInParentheses) {
42
            $replace = "'{$replace}'";
43
        }
44
45
        $commands[1] = str_replace($this->path, $replace, $commands[1]);
46
47
        return $commands;
48
    }
49
50
    /**
51
     * Normalizes the path when running on Windows.
52
     *
53
     * @param string $path
54
     * @return string
55
     */
56
    public static function normalizePath(string $path): string
57
    {
58
        return windows_os() ? static::normalizeWindowsPath($path) : $path;
59
    }
60
61
    /**
62
     * Replaces the slashes and escapes the colon.
63
     *
64
     * @param string $path
65
     * @return string
66
     */
67
    public static function normalizeWindowsPath(string $path): string
68
    {
69
        $path = str_replace('/', '\\', $path);
70
        $path = str_replace(':', '\\:', $path);
71
72
        return $path;
73
    }
74
}
75