Passed
Push — master ( 72713e...d5a1de )
by Jonathan
09:55
created

ApiKey::isValid()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.9713
cc 3
eloc 13
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud\Validator;
15
16
use TxTextControl\ReportingCloud\Validator\TypeString as TypeStringValidator;
17
use Zend\Validator\StringLength as StringLengthValidator;
18
19
/**
20
 * ApiKey validator
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
class ApiKey extends AbstractValidator
26
{
27
    /**
28
     * Minimum API key length
29
     *
30
     * @const MIN
31
     */
32
    const MIN = 20;
33
34
    /**
35
     * Maximum API key length
36
     *
37
     * @const MIN
38
     */
39
    const MAX = 45;
40
41
    /**
42
     * Invalid type
43
     *
44
     * @const INVALID_TYPE
45
     */
46
    const INVALID_TYPE = 'invalidType';
47
48
    /**
49
     * Invalid zoom factor
50
     *
51
     * @const INVALID_STRING
52
     */
53
    const INVALID_STRING = 'invalidString';
54
55
    /**
56
     * Message templates
57
     *
58
     * @var array
59
     */
60
    protected $messageTemplates
61
        = [
62
            self::INVALID_TYPE   => "'%value%' must be an integer",
63
            self::INVALID_STRING => "'%value%' contains an invalid API key",
64
        ];
65
66
    /**
67
     * Returns true, if value is valid. False otherwise.
68
     *
69
     * @param mixed $value
70
     *
71
     * @return bool
72
     */
73 12
    public function isValid($value)
74
    {
75 12
        $this->setValue($value);
76
77 12
        $typeStringValidator = new TypeStringValidator();
78 12
        if (!$typeStringValidator->isValid($value)) {
79 2
            $this->error(self::INVALID_TYPE);
80
81 2
            return false;
82
        }
83
84
        $options = [
85 10
            'min' => self::MIN,
86 10
            'max' => self::MAX,
87 5
        ];
88
89 10
        $stringLengthValidator = new StringLengthValidator($options);
90 10
        if (!$stringLengthValidator->isValid($value)) {
91 2
            $this->error(self::INVALID_STRING);
92
93 2
            return false;
94
        }
95
96 10
        return true;
97
    }
98
}
99