Passed
Push — master ( bf86e6...5ea322 )
by Sébastien
07:21
created

BinaryAssertionsTrait::ensureIsExecutable()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 4
nop 1
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Common\Assert;
6
7
use Soluble\MediaTools\Common\Exception\MissingBinaryException;
8
9
trait BinaryAssertionsTrait
10
{
11
    /**
12
     * Check for executable presence (skipped for executable in %PATH%).
13
     *
14
     * @throws MissingBinaryException
15
     */
16
    public function ensureIsExecutable(string $binaryFile): void
17
    {
18
        // Case of binary (no path given), we cannot tell
19
        if (basename($binaryFile) === $binaryFile) {
20
            $exists     = true; // assume it exists
21
            $executable = true;
22
        } else {
23
            $exists     = file_exists($binaryFile);
24
            $executable = is_executable($binaryFile);
25
        }
26
27
        if (!$exists || !$executable) {
28
            throw new MissingBinaryException(sprintf(
29
                'Missing \'%s\' binary: (exists: %s, executable: %s)',
30
                $binaryFile,
31
                $exists ? 'true' : 'false',
32
                $executable ? 'true' : 'false'
33
            ));
34
        }
35
    }
36
}
37