CaptchaValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 51
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate() 0 16 4
A client() 0 4 1
1
<?php /** MicroCaptchaValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Base\Exception;
6
use Micro\Form\IFormModel;
7
use Micro\Web\UserInjector;
8
9
/**
10
 * CaptchaValidator class file.
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright (c) 2013 Oleg Lunegov
15
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
16
 * @package Micro
17
 * @subpackage Validator
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class CaptchaValidator extends BaseValidator
22
{
23
    /** @var string $captcha compiled captcha */
24
    protected $captcha = '';
25
26
27
    /**
28
     * Constructor validator
29
     *
30
     * @access public
31
     *
32
     * @param array $params Configuration array
33
     *
34
     * @result void
35
     * @throws Exception
36
     */
37
    public function __construct(array $params)
38
    {
39
        parent::__construct($params);
40
41
        $this->captcha = (new UserInjector)->build()->getCaptcha();
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function validate(IFormModel $model)
48
    {
49
        foreach ($this->elements AS $element) {
50
            if (!$model->checkAttributeExists($element)) {
51
                $this->errors[] = 'Parameter '.$element.' not defined in class '.get_class($model);
52
53
                return false;
54
            }
55
56
            if ((new UserInjector)->build()->checkCaptcha($this->captcha)) {
57
                return false;
58
            }
59
        }
60
61
        return true;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function client(IFormModel $model)
68
    {
69
        return '';
70
    }
71
}
72