Passed
Push — master ( 3ee3fe...b14545 )
by Amin
04:48
created

Process   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
eloc 33
dl 0
loc 96
rs 10
c 1
b 1
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBinary() 0 11 3
A __construct() 0 3 1
A addCommand() 0 8 2
A run() 0 23 3
A getCommand() 0 3 1
A removeCommand() 0 7 2
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
13
namespace Streaming\Process;
14
15
16
use Streaming\Exception\Exception;
17
use Symfony\Component\Process\ExecutableFinder;
18
use Symfony\Component\Process\Process as SymphonyProcess;
19
20
class Process
21
{
22
23
    protected $commands = [];
24
25
    /**
26
     * ShakaProcess constructor.
27
     * @param $binary
28
     * @throws Exception
29
     */
30
    public function __construct($binary)
31
    {
32
        $this->commands[] = $this->getBinary($binary);
33
    }
34
35
    /**
36
     * @param $binary
37
     * @return mixed
38
     * @throws Exception
39
     */
40
    private function getBinary($binary)
41
    {
42
        if (is_executable($binary)) {
43
            return $binary;
44
        } else {
45
            $finder = new ExecutableFinder();
46
47
            if ($binary = $finder->find($binary)) {
48
                return $binary;
49
            } else {
50
                throw new Exception("We could not find 'Shaka Packager' binary.\nPlease check the path to the shaka binary");
51
            }
52
        }
53
    }
54
55
    /**
56
     * @throws Exception
57
     */
58
    public function run()
59
    {
60
        $commands = $this->getCommand();
61
62
        if (!is_executable(current($commands))) {
63
            throw new Exception('The binary is not executable');
64
        }
65
66
        $process = new SymphonyProcess($commands);
67
        $process->run();
68
69
        if (!$process->isSuccessful()) {
70
            $error = sprintf('The command "%s" failed.' . "\n\nExit Code: %s(%s)\n\nWorking directory: %s",
71
                $process->getCommandLine(),
72
                $process->getExitCode(),
73
                $process->getExitCodeText(),
74
                $process->getWorkingDirectory()
75
            );
76
77
            throw new Exception($error);
78
        }
79
80
        return $process->getOutput();
81
    }
82
83
    /**
84
     * @param array | string $command
85
     * @return Process
86
     */
87
    public function addCommand($command): Process
88
    {
89
        if (is_array($command)) {
90
            $this->commands = array_merge($this->commands, $command);
91
        } else {
92
            $this->commands[] = $command;
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $command
99
     * @return Process
100
     */
101
    public function removeCommand($command): Process
102
    {
103
        if (false !== ($key = array_search($command, $this->commands))) {
104
            unset($this->commands[$key]);
105
        }
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getCommand(): array
114
    {
115
        return $this->commands;
116
    }
117
}