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

Post::__construct()   A

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\PostModel;
6
use App\Models\TagModel;
7
use Core\Controller;
8
use Core\Container;
9
10
class Post extends Controller
11
{
12
13
    protected $siteConfig;
14
15
    public function __construct(Container $container)
16
    {
17
        $this->loadModules[] = 'SiteConfig';
18
        parent::__construct($container);
19
    }
20
21
    /**
22
     * @param $slug
23
     * @throws \Exception
24
     * @throws \ReflectionException
25
     * @throws \Twig_Error_Loader
26
     * @throws \Twig_Error_Runtime
27
     * @throws \Twig_Error_Syntax
28
     */
29
    public function viewPost(string $slug)
30
    {
31
32
        $tagModel = new TagModel($this->container);
33
        $postModel = new PostModel($this->container);
34
35
        $postId = $postModel->getPostIdFromSlug($slug);
36
37
        $posts = $postModel->getSinglePost($postId);
38
39
        //only admins can view unpublished posts
40
        if (!$posts->published) {
41
            if (!$this->auth->isAdmin()) {
42
                throw new \Exception("File does not exist", "404");
43
            }
44
            $this->alertBox->setAlert('This post is not yet published', 'warning');
45
        }
46
47
48
        $this->sendSessionVars();
49
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
50
        $this->data['post'] = $posts;
51
        $this->data['postTags'] = $tagModel->getTagsOnPost($postId);
52
        $this->data['navigation'] = $this->siteConfig->getMenu();
53
54
        $this->renderView('post');
55
56
    }
57
}