AbstractEntityValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 70
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setEntityService() 0 4 1
A setMethod() 0 4 1
A setField() 0 4 1
A fetchResult() 0 12 1
1
<?php
2
/**
3
 * Polder Knowledge / entityservice-zend-validator (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/entityservice-zend-validator for the canonical source repository
6
 * @copyright Copyright (c) 2016 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/entityservice-zend-validator/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\EntityService\Validator;
11
12
use PolderKnowledge\EntityService\EntityServiceInterface;
13
use Zend\Validator\AbstractValidator;
14
15
/**
16
 * Base class for validators using an EntityServiceInterface.
17
 */
18
abstract class AbstractEntityValidator extends AbstractValidator
19
{
20
    /**
21
     * EntityService instance used for validation
22
     *
23
     * @var EntityServiceInterface
24
     */
25
    protected $entityService;
26
27
    /**
28
     * method name that will be called on validation
29
     *
30
     * @var string Method to be called
31
     */
32
    protected $method = 'findBy';
33
34
    /**
35
     * @var string Name of the field to search
36
     */
37
    protected $field = 'id';
38
39
    /**
40
     * Set entity service used for validation
41
     *
42
     * @param EntityServiceInterface $entityService
43
     */
44 15
    public function setEntityService(EntityServiceInterface $entityService)
45
    {
46 15
        $this->entityService = $entityService;
47 15
    }
48
49
    /**
50
     * Sets the method option.
51
     *
52
     * @param string $method The name of the method to set.
53
     */
54 9
    public function setMethod($method)
55
    {
56 9
        $this->method = $method;
57 9
    }
58
59
    /**
60
     * Sets the field option.
61
     *
62
     * @param string $field The name of the field to set.
63
     */
64 3
    public function setField($field)
65
    {
66 3
        $this->field = $field;
67 3
    }
68
69
    /**
70
     * Will call the configured method on self::$entityService using the value as criteria
71
     *
72
     * @param mixed $value
73
     * @return object
74
     */
75 21
    protected function fetchResult($value)
76
    {
77 21
        return call_user_func_array(
78
            array(
79 21
                $this->entityService,
80 21
                $this->method
81 21
            ),
82
            array(
83 21
                'criteria' => array($this->field => $value)
84 21
            )
85 21
        );
86
    }
87
}
88