Completed
Push — master ( fc0ef9...fd9cb0 )
by Andrew
04:08
created

SettingsValidator::validateCookiePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace ddlzz\AmoAPI\Validator;
5
6
use ddlzz\AmoAPI\Exception\InvalidArgumentException;
7
use ddlzz\AmoAPI\Utils\StringUtil;
8
9
10
/**
11
 * Class SettingsValidator
12
 * @package ddlzz\AmoAPI\Validator
13
 * @author ddlzz
14
 */
15
class SettingsValidator
16
{
17
    /**
18
     * @param string $scheme
19
     * @return bool
20
     * @throws InvalidArgumentException
21
     */
22 4
    public function validateScheme($scheme)
23
    {
24 4
        if (!StringUtil::isOnlyLetters($scheme)) {
25 3
            $message = sprintf('"%s" is not a valid scheme', $scheme);
26 3
            throw new InvalidArgumentException($message);
27
        }
28
29 1
        return true;
30
    }
31
32
    /**
33
     * @param string $domain
34
     * @return bool
35
     * @throws InvalidArgumentException
36
     */
37 4
    public function validateDomain($domain)
38
    {
39 4
        if (!StringUtil::isDomain($domain)) {
40 3
            $message = sprintf('"%s" is not a valid domain', $domain);
41 3
            throw new InvalidArgumentException($message);
42
        }
43
44 1
        return true;
45
    }
46
47
    /**
48
     * @param string $userAgent
49
     * @return bool
50
     * @throws InvalidArgumentException
51
     */
52 4
    public function validateUserAgent($userAgent)
53
    {
54 4
        if (!StringUtil::isText($userAgent)) {
55 3
            $message = sprintf('"%s" is not a valid user agent', $userAgent);
56 3
            throw new InvalidArgumentException($message);
57
        }
58
59 1
        return true;
60
    }
61
62
    /**
63
     * @param array $paths
64
     * @return bool
65
     * @throws InvalidArgumentException
66
     */
67 4
    public function validateMethodsPaths(array $paths)
68
    {
69 4
        if (empty($paths)) {
70 1
            throw new InvalidArgumentException('An array "method Paths" must not be empty');
71
        }
72
73 3
        foreach ($paths as $key => $item) {
74 3
            if (empty($item)) {
75 1
                throw new InvalidArgumentException("An array item \"$key\" must not be empty");
76
            }
77
78 2
            if (!StringUtil::isUrlPath($item)) {
79 1
                $message = sprintf('"%s" is not a valid method path', $item);
80 2
                throw new InvalidArgumentException($message);
81
            }
82
        }
83
84 1
        return true;
85
    }
86
87
    /**
88
     * @param string $path
89
     * @return bool
90
     * @throws InvalidArgumentException
91
     */
92 4
    public function validateCookiePath($path)
93
    {
94 4
        if (!StringUtil::isFilePath($path)) {
95 3
            $message = sprintf('"%s" is not a valid file path', $path);
96 3
            throw new InvalidArgumentException($message);
97
        }
98
99 1
        return true;
100
    }
101
}