Passed
Pull Request — master (#58)
by Stone
04:29 queued 02:04
created

Category::allPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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