AuthorUniqueName   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 31
ccs 8
cts 8
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A check() 0 7 2
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