Passed
Push — master ( 315e77...e369d0 )
by Jonathan
16:56
created

AssertFilenameExistsTrait::assertFilenameExists()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 3
nop 2
crap 5
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
18
19
/**
20
 * Trait AssertFilenameExistsTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertFilenameExistsTrait
26
{
27
    /**
28
     * @param mixed $value
29
     *
30
     * @return string
31
     */
32
    abstract protected static function valueToString($value): string;
33
34
    /**
35
     * Check filename exists and is readable
36
     *
37
     * @param string $value
38
     * @param string $message
39
     *
40
     * @return void
41
     * @throws InvalidArgumentException
42
     */
43 94
    public static function assertFilenameExists(string $value, string $message = ''): void
44
    {
45 94
        if (!is_readable($value)) {
46 18
            $format  = $message ?: '%1$s does not exist or is not readable';
47 18
            $message = sprintf($format, self::valueToString($value));
48 18
            throw new InvalidArgumentException($message);
49
        }
50
51 76
        if (!is_file($value)) {
52 3
            $format  = $message ?: '%1$s is not a regular file';
53 3
            $message = sprintf($format, self::valueToString($value));
54 3
            throw new InvalidArgumentException($message);
55
        }
56 73
    }
57
}
58