Test Failed
Branch development (5babc9)
by Jonathan
04:47
created

ApiKey::isValid()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
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
    public function isValid($value)
74
    {
75
        $this->setValue($value);
76
77
        $typeStringValidator = new TypeStringValidator();
78
        if (!$typeStringValidator->isValid($value)) {
79
            $this->error(self::INVALID_TYPE);
80
81
            return false;
82
        }
83
84
        $options = [
85
            'min' => self::MIN,
86
            'max' => self::MAX,
87
        ];
88
89
        $stringLengthValidator = new StringLengthValidator($options);
90
        if (!$stringLengthValidator->isValid($value)) {
91
            $this->error(self::INVALID_STRING);
92
93
            return false;
94
        }
95
96
        return true;
97
    }
98
}
99