Passed
Pull Request — master (#572)
by Dmitriy
01:35
created

BlogService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Application\Blog\Service;
6
7
use App\Application\Blog\Entity\Post\Post;
8
use App\Application\Blog\Entity\Post\PostRepository;
9
use App\Application\Exception\NotFoundException;
10
use Yiisoft\Data\Paginator\OffsetPaginator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Paginator\OffsetPaginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\Data\Paginator\PaginatorInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Paginator\PaginatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
final class BlogService
14
{
15
    private const POSTS_PER_PAGE = 10;
16
    private PostRepository $postRepository;
17
18
    public function __construct(PostRepository $postRepository)
19
    {
20
        $this->postRepository = $postRepository;
21
    }
22
23
    /**
24
     * @psalm-return PaginatorInterface<array-key, Post>
25
     */
26
    public function getPosts(int $page): PaginatorInterface
27
    {
28
        $dataReader = $this->postRepository->findAll();
29
30
        /** @psalm-var PaginatorInterface<array-key, Post> */
31
        return (new OffsetPaginator($dataReader))
32
            ->withPageSize(self::POSTS_PER_PAGE)
33
            ->withCurrentPage($page);
34
    }
35
36
    /**
37
     * @param int $id
38
     *
39
     * @throws NotFoundException
40
     *
41
     * @return Post
42
     */
43
    public function getPost(int $id): Post
44
    {
45
        /**
46
         * @var Post|null $post
47
         */
48
        $post = $this->postRepository->findOne(['id' => $id]);
49
        if ($post === null) {
50
            throw new NotFoundException();
51
        }
52
53
        return $post;
54
    }
55
}
56