Tag::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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 App\Models\TagModel;
8
use Core\Constant;
9
use Core\Controller;
10
use Core\Container;
11
12
class Tag extends Controller
13
{
14
15
    protected $siteConfig;
16
17
    public function __construct(Container $container)
18
    {
19
        $this->loadModules[] = 'SiteConfig';
20
        parent::__construct($container);
21
    }
22
23
    /**
24
     * @param int $tagId
25
     * @param string $page
26
     * @param int $linesPerPage
27
     * @throws \ErrorException
28
     * @throws \ReflectionException
29
     * @throws \Twig_Error_Loader
30
     * @throws \Twig_Error_Runtime
31
     * @throws \Twig_Error_Syntax
32
     */
33
    public function posts(int $tagId, string $page = "page-1", int $linesPerPage = Constant::POSTS_PER_PAGE)
34
    {
35
        $postModel = new PostModel($this->container);
36
        $tagModel = new TagModel($this->container);
37
38
        $totalPosts = $postModel->totalNumberPostsByTag($tagId);
39
        if ($totalPosts < 1) {
40
            throw new \Exception("Tag has no posts", 404);
41
        }
42
        $pagination = $this->pagination->getPagination($page, $totalPosts, $linesPerPage);
43
44
        if ($linesPerPage !== Constant::POSTS_PER_PAGE) {
45
            $this->data['paginationPostsPerPage'] = $linesPerPage;
46
        }
47
48
        $this->sendSessionVars();
49
        $this->data['posts'] = $postModel->getPostsWithTag($tagId, $pagination["offset"]);
50
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
51
        $this->data['navigation'] = $this->siteConfig->getMenu();
52
        $this->data['pagination'] = $pagination;
53
        $this->data['tagId'] = $tagId;
54
        $this->data['tag'] = $tagModel->getTagDetails($tagId);
55
        $this->renderView('Tag');
56
    }
57
}