SiteConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Modules;
4
5
use App\Models\CategoryModel;
6
use App\Models\Remembered_loginModel;
7
use App\Models\UserModel;
8
use Core\Container;
9
use Core\Modules\Module;
10
use App\Models\ConfigModel;
11
use Core\Traits\StringFunctions;
12
13
class SiteConfig extends Module
14
{
15
    use StringFunctions;
16
17
    private $configs;
18
    private $categoryModel;
19
    private $userModel;
20
    private $rememberedLoginModel;
21
22
    public function __construct(Container $container)
23
    {
24
        parent::__construct($container);
25
26
        $this->configs = new ConfigModel($this->container);
27
        $this->categoryModel = new CategoryModel($this->container);
28
        $this->userModel = new UserModel($this->container);
29
        $this->rememberedLoginModel = new Remembered_loginModel($this->container);
30
31
        $this->loginFromRememberMe();
32
    }
33
34
    /**
35
     * Gets the entire site configuration and arranges it into a displayable list
36
     * @return array the config ordered and ready to display
37
     * @throws \ReflectionException
38
     */
39
    public function getSiteConfig():array
40
    {
41
42
43
        $siteConfig = $this->configs->getAllConfig();
44
        $data = [];
45
        foreach ($siteConfig as $config) {
46
            $data[$config->configs_name] = $config->configs_value;
47
        }
48
        return $data;
49
    }
50
51
    /**
52
     * create the front end menu object to be sent to twig and add the urls
53
     * @return array
54
     * @throws \ReflectionException
55
     */
56
    public function getMenu():array
57
    {
58
        $data = [];
59
        //get the categories from database
60
        $categories = $this->categoryModel->getCategories();
61
        foreach ($categories as $category) {
62
            $data += [
63
                $category->category_name => '/category/posts/' . $category->categories_slug
64
            ];
65
        }
66
        return $data;
67
    }
68
69
    /**
70
     * auto login if the remember me cookie is set
71
     * @throws \Exception
72
     */
73
    public function loginFromRememberMe()
74
    {
75
        $cookie = $this->container->getCookie();
76
        $session = $this->container->getSession();
77
        $userToken = $cookie->getCookie("rememberMe");
78
79
        if($userToken && $this->isHexa($userToken))
80
        {
81
            //we have a rememberMe Hash, login
82
            $rememberedLogin = $this->rememberedLoginModel->findByToken($userToken);
83
            if($rememberedLogin){
84
                //we have a hash, login
85
                $user = $this->userModel->getUserDetailsById($rememberedLogin->users_idusers);
86
                $session->regenerateSessionId(); //regenerate the ID to avoid session ghosting
87
                $session->set("user", $user);
88
                $session->set("userId", $user->idusers);
89
                $userRoleName = $user->role_name ?? "";
90
                $userRoleLevel = $user->role_level ?? 0;
91
                $session->set('user_role_name', $userRoleName);
92
                $session->set('user_role_level', $userRoleLevel);
93
            }
94
95
        }
96
97
    }
98
}