Passed
Push — Showing-Posts ( b1a8d3...c3e4ac )
by Stone
02:25
created

Category::posts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 18
rs 9.9666
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 App\Models\SlugModel;
8
use Core\Constant;
9
use Core\Container;
10
use Core\Controller;
11
use Core\Traits\StringFunctions;
12
13
class Category extends Controller
14
{
15
    use StringFunctions;
0 ignored issues
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Category.
Loading history...
16
17
    protected $siteConfig;
18
    protected $pagination;
19
20
    private $slugModel;
21
    private $postModel;
22
    private $categoryModel;
23
24
    public function __construct(Container $container)
25
    {
26
        $this->loadModules[] = 'SiteConfig';
27
        $this->loadModules[] = 'pagination';
28
        parent::__construct($container);
29
        $this->slugModel = new SlugModel($this->container);
30
        $this->postModel = new PostModel($this->container);
31
        $this->categoryModel = new CategoryModel($this->container);
32
33
    }
34
35
    /**
36
     * show all posts in a category
37
     * @param string $categorySlug the slug passed via url
38
     * @param string $page
39
     * @throws \ErrorException
40
     * @throws \ReflectionException
41
     * @throws \Twig_Error_Loader
42
     * @throws \Twig_Error_Runtime
43
     * @throws \Twig_Error_Syntax
44
     */
45
    public function posts(string $categorySlug, string $page="page-1")
46
    {
47
48
49
        $categoryId = $this->slugModel->getIdFromSlug($categorySlug, "categories", "categories_slug", "idcategories");
50
        $totalPosts = $this->postModel->totalNumberPostsInCategory($categoryId) ;
51
52
        $pagination = $this->pagination->getPagination($page, $totalPosts);
53
54
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
55
56
57
        $this->data['navigation'] = $this->categoryModel->getMenu();
58
        $this->data['posts'] = $this->postModel->getPostsInCategory($categoryId,$pagination["offset"]);
59
        $this->data['pagination'] = $pagination;
60
        $this->data['categorySlug'] = $categorySlug;
61
62
        $this->renderView('Category');
63
64
    }
65
66
    public function allPosts(string $page="page-1")
67
    {
68
        $totalPosts = $this->postModel->totalNumberPosts();
69
70
        $pagination = $this->pagination->getPagination($page, $totalPosts);
71
72
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
73
74
75
        $this->data['navigation'] = $this->categoryModel->getMenu();
76
        $this->data['posts'] = $this->postModel->getPosts($pagination["offset"]);
77
        $this->data['pagination'] = $pagination;
78
79
        $this->renderView('Category');
80
    }
81
}