AuthorUniqueName::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LibraryCatalog\Service\Validation\Rule;
6
7
use LibraryCatalog\Service\Repository\AuthorRepositoryInterface;
8
use Rakit\Validation\Rule;
9
10
class AuthorUniqueName extends Rule
11
{
12
    /** @var string */
13
    protected $message = "Name and birthdate should be unique";
14
    /** @var AuthorRepositoryInterface */
15
    protected AuthorRepositoryInterface $authorRepository;
16
    /** @var string */
17
    protected string $birthdate;
18
19
    /**
20
     * AuthorUniqueName constructor.
21
     * @param AuthorRepositoryInterface $authorRepository
22
     * @param string $birthdate
23
     */
24 2
    public function __construct(AuthorRepositoryInterface $authorRepository, string $birthdate)
25
    {
26 2
        $this->authorRepository = $authorRepository;
27 2
        $this->birthdate = $birthdate;
28 2
    }
29
30
    /**
31
     * @param $value
32
     * @return bool
33
     */
34 2
    public function check($value): bool
35
    {
36 2
        if ($this->birthdate === '') {
37 1
            return true;
38
        }
39
40 1
        return $this->authorRepository->loadByNameBirthdate((string)$value, $this->birthdate) === null;
41
    }
42
}
43