Passed
Pull Request — master (#49)
by
unknown
14:23
created

BlogController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 27
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 43
rs 9.488
1
<?php
2
3
namespace App\Blog;
4
5
use App\Controller;
6
use App\Blog\Entity\Post;
7
use App\Blog\Entity\Tag;
8
use App\Blog\Post\PostRepository;
9
use App\Blog\Tag\TagRepository;
10
use App\Pagination\PaginationSet;
11
use Cycle\ORM\ORMInterface;
12
use Psr\Http\Message\ResponseInterface as Response;
13
use Psr\Http\Message\ServerRequestInterface as Request;
14
use Yiisoft\Data\Paginator\OffsetPaginator;
15
use Yiisoft\Router\UrlGeneratorInterface;
16
17
class BlogController extends Controller
18
{
19
    private const POSTS_PER_PAGE = 3;
20
    private const POPULAR_TAGS_COUNT = 10;
21
22
    protected function getId(): string
23
    {
24
        return 'blog';
25
    }
26
27
    public function index(
28
        Request $request,
29
        ORMInterface $orm,
30
        UrlGeneratorInterface $urlGenerator
31
    ): Response {
32
        /** @var PostRepository $postRepo */
33
        $postRepo = $orm->getRepository(Post::class);
34
        /** @var TagRepository $postRepo */
35
        $tagRepo = $orm->getRepository(Tag::class);
36
37
        $pageNum = (int)$request->getAttribute('page', 1);
38
        $year = $request->getAttribute('year', null);
39
        $month = $request->getAttribute('month', null);
40
        $isArchive = $year !== null && $month !== null;
41
42
        if ($isArchive) {
43
            $dataReader = $postRepo->findArchivedPublic($year, $month);
0 ignored issues
show
Bug introduced by
The method findArchivedPublic() does not exist on App\Blog\Tag\TagRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            /** @scrutinizer ignore-call */ 
44
            $dataReader = $postRepo->findArchivedPublic($year, $month);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            $pageUrlGenerator = fn ($page) => $urlGenerator->generate(
45
                'blog/archive',
46
                ['year' => $year, 'month' => $month, 'page' => $page]
47
            );
48
        } else {
49
            $dataReader = $postRepo->findAllPreloaded();
0 ignored issues
show
Bug introduced by
The method findAllPreloaded() does not exist on App\Blog\Tag\TagRepository. Did you maybe mean findAll()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            /** @scrutinizer ignore-call */ 
50
            $dataReader = $postRepo->findAllPreloaded();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
            $pageUrlGenerator = fn ($page) => $urlGenerator->generate('blog/index', ['page' => $page]);
51
        }
52
53
        $paginationSet = new PaginationSet(
0 ignored issues
show
Deprecated Code introduced by
The class App\Pagination\PaginationSet has been deprecated: Should be replaced to a Pagination Widget ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
        $paginationSet = /** @scrutinizer ignore-deprecated */ new PaginationSet(
Loading history...
54
            (new OffsetPaginator($dataReader))
55
                ->withPageSize(self::POSTS_PER_PAGE)
56
                ->withCurrentPage($pageNum),
57
            $pageUrlGenerator
58
        );
59
60
        $data = [
61
            'paginationSet' => $paginationSet,
62
            'archive' => $postRepo->getArchive()->withLimit(12),
0 ignored issues
show
Bug introduced by
The method getArchive() does not exist on App\Blog\Tag\TagRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            'archive' => $postRepo->/** @scrutinizer ignore-call */ getArchive()->withLimit(12),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            'tags' => $tagRepo->getTagMentions(self::POPULAR_TAGS_COUNT),
0 ignored issues
show
Bug introduced by
The method getTagMentions() does not exist on Cycle\ORM\RepositoryInterface. It seems like you code against a sub-type of Cycle\ORM\RepositoryInterface such as App\Blog\Tag\TagRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            'tags' => $tagRepo->/** @scrutinizer ignore-call */ getTagMentions(self::POPULAR_TAGS_COUNT),
Loading history...
64
        ];
65
        $output = $this->render(__FUNCTION__, $data);
66
67
        $response = $this->responseFactory->createResponse();
68
        $response->getBody()->write($output);
69
        return $response;
70
    }
71
}
72