Issues (281)

Branch: master

src/Frontend/Modules/Blog/Actions/Category.php (1 issue)

1
<?php
2
3
namespace Frontend\Modules\Blog\Actions;
4
5
use Frontend\Core\Engine\Base\Block as FrontendBaseBlock;
6
use Frontend\Core\Language\Language as FL;
7
use Frontend\Core\Engine\Navigation as FrontendNavigation;
8
use Frontend\Modules\Blog\Engine\Model as FrontendBlogModel;
0 ignored issues
show
The type Frontend\Modules\Blog\Engine\Model 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...
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10
11
class Category extends FrontendBaseBlock
12
{
13
    /** @var array */
14
    private $articles;
15
16
    /** @var array */
17
    private $category;
18
19 4
    public function execute(): void
20
    {
21 4
        parent::execute();
22 4
        $this->loadTemplate();
23 4
        $this->getData();
24 3
        $this->parse();
25 3
    }
26
27 4
    private function getCategory(): array
28
    {
29 4
        $slug = $this->url->getParameter(1);
30 4
        if (empty($slug)) {
31
            throw new NotFoundHttpException();
32
        }
33
34 4
        $category = FrontendBlogModel::getCategory($slug);
35
36 4
        if (empty($category)) {
37 1
            throw new NotFoundHttpException();
38
        }
39
40 3
        return $category;
41
    }
42
43 3
    private function buildUrl(): string
44
    {
45 3
        return FrontendNavigation::getUrlForBlock($this->getModule(), $this->getAction())
46 3
               . '/' . $this->category['url'];
47
    }
48
49 3
    private function buildPaginationConfig(): array
50
    {
51 3
        $requestedPage = $this->url->getParameter('page', 'int', 1);
52 3
        $numberOfItems = FrontendBlogModel::getAllForCategoryCount($this->category['url']);
53
54 3
        $limit = $this->get('fork.settings')->get($this->getModule(), 'overview_num_items', 10);
55 3
        $numberOfPages = (int) ceil($numberOfItems / $limit);
56
57 3
        if ($numberOfPages === 0) {
58
            $numberOfPages = 1;
59
        }
60
61
        // Check if the page exists
62 3
        if ($requestedPage > $numberOfPages || $requestedPage < 1) {
63 1
            throw new NotFoundHttpException();
64
        }
65
66
        return [
67 3
            'url' => $this->buildUrl(),
68 3
            'limit' => $limit,
69 3
            'offset' => ($requestedPage * $limit) - $limit,
70 3
            'requested_page' => $requestedPage,
71 3
            'num_items' => $numberOfItems,
72 3
            'num_pages' => $numberOfPages,
73
        ];
74
    }
75
76 4
    private function getData(): void
77
    {
78 4
        $this->category = $this->getCategory();
79 3
        $this->pagination = $this->buildPaginationConfig();
80
81 3
        $this->articles = FrontendBlogModel::getAllForCategory(
82 3
            $this->category['url'],
83 3
            $this->pagination['limit'],
84 3
            $this->pagination['offset']
85
        );
86 3
    }
87
88 3
    private function addLinkToRssFeed(): void
89
    {
90 3
        $this->header->addRssLink(
91 3
            $this->get('fork.settings')->get($this->getModule(), 'rss_title_' . LANGUAGE, SITE_DEFAULT_TITLE),
92 3
            FrontendNavigation::getUrlForBlock($this->getModule(), 'Rss')
93
        );
94 3
    }
95
96 3
    private function addCategoryToBreadcrumb(): void
97
    {
98 3
        $this->breadcrumb->addElement(\SpoonFilter::ucfirst(FL::lbl('Category')));
99 3
        $this->breadcrumb->addElement($this->category['label']);
100 3
    }
101
102 3
    private function setPageTitle(): void
103
    {
104 3
        $this->header->setPageTitle(\SpoonFilter::ucfirst(FL::lbl('Category')));
105 3
    }
106
107 3
    private function parse(): void
108
    {
109 3
        $this->addLinkToRssFeed();
110 3
        $this->addCategoryToBreadcrumb();
111 3
        $this->setPageTitle();
112 3
        $this->parsePagination();
113
114 3
        $this->template->assign('category', $this->category);
115 3
        $this->template->assign('items', $this->articles);
116 3
        $this->setMeta($this->category['meta']);
117 3
    }
118
}
119