Category::posts()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 3
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers;
4
5
use App\Models\CategoryModel;
6
use App\Models\PostModel;
7
use Core\Constant;
8
use Core\Container;
9
use Core\Controller;
10
11
class Category extends Controller
12
{
13
14
    protected $siteConfig;
15
16
    private $postModel;
17
    private $categoryModel;
18
19
    public function __construct(Container $container)
20
    {
21
        $this->loadModules[] = 'SiteConfig';
22
        parent::__construct($container);
23
        $this->postModel = new PostModel($this->container);
24
        $this->categoryModel = new CategoryModel($this->container);
25
26
        $this->sendSessionVars();
27
28
    }
29
30
    /**
31
     * show all posts in a category
32
     * @param string $categorySlug the slug passed via url
33
     * @param string $page
34
     * @param int $linesPerPage
35
     * @throws \ReflectionException
36
     * @throws \Twig_Error_Loader
37
     * @throws \Twig_Error_Runtime
38
     * @throws \Twig_Error_Syntax
39
     */
40
    public function posts(string $categorySlug, string $page = "page-1", int $linesPerPage = Constant::POSTS_PER_PAGE)
41
    {
42
        $categoryId = $this->categoryModel->getCategoryIdFromSlug($categorySlug);
43
        $totalPosts = $this->postModel->totalNumberPostsInCategory($categoryId);
44
        $pagination = $this->pagination->getPagination($page, $totalPosts);
45
46
        if ($linesPerPage !== Constant::POSTS_PER_PAGE) {
47
            $this->data['paginationPostsPerPage'] = $linesPerPage;
48
        }
49
50
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
51
        $this->data['navigation'] = $this->siteConfig->getMenu();
52
        $this->data['posts'] = $this->postModel->getPostsInCategory($categoryId, $pagination["offset"], $linesPerPage);
53
        $this->data['pagination'] = $pagination;
54
        $this->data['categorySlug'] = $categorySlug;
55
        $this->data['category'] = $this->categoryModel->getCategoryDetails($categoryId);
56
57
        $this->renderView('Category');
58
59
    }
60
61
    /**
62
     * list all the posts no matter what category
63
     * @param string $page
64
     * @param int $linesPerPage
65
     * @throws \ErrorException
66
     * @throws \ReflectionException
67
     * @throws \Twig_Error_Loader
68
     * @throws \Twig_Error_Runtime
69
     * @throws \Twig_Error_Syntax
70
     */
71
    public function allPosts(string $page = "page-1", int $linesPerPage = Constant::POSTS_PER_PAGE)
72
    {
73
        $totalPosts = $this->postModel->totalNumberPosts();
74
        $pagination = $this->pagination->getPagination($page, $totalPosts, $linesPerPage);
75
76
        if ($linesPerPage !== Constant::POSTS_PER_PAGE) {
77
            $this->data['paginationPostsPerPage'] = $linesPerPage;
78
        }
79
80
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
81
        $this->data['navigation'] = $this->siteConfig->getMenu();
82
        $this->data['posts'] = $this->postModel->getPosts($pagination["offset"], [], $linesPerPage);
83
        $this->data['pagination'] = $pagination;
84
85
        $this->renderView('Category');
86
    }
87
}