Passed
Push — master ( cf4530...c38ca0 )
by Sébastien
02:37
created

BinaryAssertionsTrait::ensureBinaryAvailable()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 4
nop 1
dl 0
loc 17
ccs 12
cts 12
cp 1
crap 6
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 3
    protected function ensureBinaryAvailable(string $binaryFile): void
17
    {
18
        // Case of binary (no path given), we cannot tell
19 3
        if (basename($binaryFile) === $binaryFile) {
20 1
            $exists     = true; // assume it exists
21 1
            $executable = true;
22
        } else {
23 2
            $exists     = file_exists($binaryFile);
24 2
            $executable = is_executable($binaryFile);
25
        }
26
27 3
        if (!$exists || !$executable) {
28 1
            throw new MissingBinaryException(sprintf(
29 1
                'Missing \'%s\' binary: (exists: %s, executable: %s)',
30 1
                $binaryFile,
31 1
                $exists ? 'true' : 'false',
32 1
                $executable ? 'true' : 'false'
33
            ));
34
        }
35 2
    }
36
}
37