AssertTemplateNameTrait::assertTemplateName()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.6111
cc 5
nc 5
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://git.io/Jejj2 for the canonical source repository
11
 * @license   https://git.io/Jejjr
12
 * @copyright © 2022 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
18
19
/**
20
 * Trait AssertTemplateNameTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertTemplateNameTrait
26
{
27
    use ValueToStringTrait;
28
    use AssertTemplateFormatTrait;
29
30
    /**
31
     * Check value is a valid template name
32
     *
33
     * @param string $value
34
     * @param string $message
35
     *
36
     * @return void
37
     * @throws InvalidArgumentException
38
     */
39 62
    public static function assertTemplateName(string $value, string $message = ''): void
40
    {
41 62
        if (basename($value) != $value) {
42 16
            $format  = 0 === strlen($message) ? '%1$s contains path information (\'/\', \'.\', or \'..\')' : $message;
43 16
            $message = sprintf($format, self::valueToString($value));
44 16
            throw new InvalidArgumentException($message);
45
        }
46
47 46
        $extension = pathinfo($value, PATHINFO_EXTENSION);
48
49
        try {
50 46
            self::assertTemplateFormat(/** @scrutinizer ignore-type */ $extension);
51 10
        } catch (InvalidArgumentException $e) {
52 10
            $format  = 0 === strlen($message) ? '%1$s contains an unsupported file extension' : $message;
53 10
            $message = sprintf($format, self::valueToString($value));
54 10
            throw new InvalidArgumentException($message);
55
        }
56
    }
57
}
58