EntityExists::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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
/**
13
 * The EntityExists validator will validate a certain entity was found. Validation will
14
 * fail when the entity is not present
15
 */
16 View Code Duplication
class EntityExists extends AbstractEntityValidator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * Error constants
20
     */
21
    const ERROR_NO_OBJECT_FOUND = 'noObjectFound';
22
23
    /**
24
     * @var array Message templates
25
     */
26
    protected $messageTemplates = array(
27
        self::ERROR_NO_OBJECT_FOUND => "No object matching '%value%' was found",
28
    );
29
30
    /**
31
     * Returns true when $this->entityService returns an entity
32
     * when $this->method is called with the given criteria
33
     *
34
     * @param  mixed $value
35
     * @return boolean
36
     */
37 12
    public function isValid($value)
38
    {
39 12
        $result = $this->fetchResult($value);
40
41
        // The following code will check if we have a valid result. In case of an empty array (we did not get a valid
42
        // match) the not-operator will result to true, causing the error to be set. In case of a null value (no object
43
        // has been found), the not-operator will also result to true.
44 12
        if (!$result) {
45 3
            $this->error(self::ERROR_NO_OBJECT_FOUND, $value);
46 3
            return false;
47
        }
48
49 9
        return true;
50
    }
51
}
52