NoObjectExists::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Validator;
6
7
use function is_object;
8
9
/**
10
 * Class that validates if objects does not exist in a given repository with a given list of matched fields
11
 *
12
 * @link    http://www.doctrine-project.org/
13
 */
14
class NoObjectExists extends ObjectExists
15
{
16
    /**
17
     * Error constants
18
     */
19
    public const ERROR_OBJECT_FOUND = 'objectFound';
20
21
    /** @var mixed[] Message templates */
22
    protected $messageTemplates = [self::ERROR_OBJECT_FOUND => "An object matching '%value%' was found"];
23
24
    /**
25
     * {@inheritDoc}
26
     */
27 3
    public function isValid($value)
28
    {
29 3
        $cleanedValue = $this->cleanSearchValue($value);
30 3
        $match        = $this->objectRepository->findOneBy($cleanedValue);
31
32 3
        if (is_object($match)) {
33 2
            $this->error(self::ERROR_OBJECT_FOUND, $value);
34
35 2
            return false;
36
        }
37
38 1
        return true;
39
    }
40
}
41