Passed
Push — master ( fbc447...748ca4 )
by Jonathan
18:21
created

AssertTemplateNameTrait::assertTemplateName()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.5555
c 0
b 0
f 0
cc 5
nc 4
nop 2
crap 5
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper 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 InvalidArgumentException;
18
19
/**
20
 * Trait AssertTemplateNameTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertTemplateNameTrait
26
{
27
    /**
28
     * Validate template format
29
     *
30
     * @param string $value
31
     * @param string $message
32
     *
33
     * @return null
34
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
35
     */
36
    abstract public static function assertTemplateFormat(string $value, string $message = '');
37
38
    /**
39
     * Validate template name
40
     *
41
     * @param string $value
42
     * @param string $message
43
     *
44
     * @return null
45
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
46
     */
47 84
    public static function assertTemplateName(string $value, string $message = '')
48
    {
49 84
        if (basename($value) != $value) {
50 24
            $format  = "%s contains path information ('/', '.', or '..')";
51 24
            $message = sprintf($message ?: $format, self::valueToString($value));
52 24
            self::reportInvalidArgument($message);
53
        }
54
55 60
        $extension = pathinfo($value, PATHINFO_EXTENSION);
56
57
        try {
58 60
            self::assertTemplateFormat($extension);
59 15
        } catch (InvalidArgumentException $e) {
60 15
            $format  = "%s contains an unsupported file extension";
61 15
            $message = sprintf($message ?: $format, self::valueToString($value));
62 15
            self::reportInvalidArgument($message);
63
        }
64
65 45
        return null;
66
    }
67
}
68