Passed
Push — master ( 75052b...8ecf83 )
by Dmitri
01:50
created

AuthorService::fetch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Application\Service;
6
7
use App\Application\Dto\Assembler;
8
use App\Application\Dto\AuthorDto;
9
use App\Application\Exception\AuthorNotFound;
10
use App\Domain\Model\AuthorId;
11
use App\Domain\Model\AuthorRepository;
12
13
final class AuthorService
14
{
15
    private $authors;
16
    private $assembler;
17
18
    public function __construct(AuthorRepository $authors, Assembler $assembler)
19
    {
20
        $this->authors = $authors;
21
        $this->assembler = $assembler;
22
    }
23
24
    /**
25
     * @throws AuthorNotFound
26
     */
27
    public function fetch(string $id): AuthorDto
28
    {
29
        $authorId = AuthorId::fromString($id);
30
31
        if (null === $author = $this->authors->byId($authorId)) {
32
            throw AuthorNotFound::byId($authorId);
33
        }
34
35
        return $this->assembler->toAuthorDto($author);
36
    }
37
38
    /**
39
     * @return AuthorDto[]
40
     */
41
    public function fetchAll(): array
42
    {
43
        return array_map([$this->assembler, 'toAuthorDto'], $this->authors->all());
44
    }
45
}
46