Passed
Pull Request — master (#265)
by Pascal
02:52
created

WatermarkFilter::wrapInParentheses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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