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

SiteConfig::getMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 13
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;
0 ignored issues
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Modules\SiteConfig.
Loading history...
16
    public function __construct(Container $container)
17
    {
18
        parent::__construct($container);
19
        $this->loginFromRememberMe();
20
    }
21
22
    /**
23
     * Gets the entire site configuration and arranges it into a displayable list
24
     * @return array the config ordered and ready to display
25
     * @throws \ReflectionException
26
     */
27
    public function getSiteConfig():array
28
    {
29
30
        $configs = new ConfigModel($this->container);
31
        $siteConfig = $configs->getAllConfig();
32
        $data = [];
33
        foreach ($siteConfig as $config) {
34
            $data[$config->configs_name] = $config->configs_value;
35
        }
36
        return $data;
37
    }
38
39
    /**
40
     * create the front end menu object to be sent to twig and add the urls
41
     * @return array
42
     * @throws \ReflectionException
43
     */
44
    public function getMenu():array
45
    {
46
        $categoryModel = new CategoryModel($this->container);
47
48
        $data = [];
49
        //get the categories from database
50
        $categories = $categoryModel->getCategories();
51
        foreach ($categories as $category) {
52
            $data += [
53
                $category->category_name => '/category/posts/' . $category->categories_slug
54
            ];
55
        }
56
        return $data;
57
    }
58
59
    /**
60
     * auto login if the remember me cookie is set
61
     * @throws \Exception
62
     */
63
    public function loginFromRememberMe()
64
    {
65
        $cookie = $this->container->getCookie();
66
        $userModel = new UserModel($this->container);
67
        $rememberedLoginModel = new Remembered_loginModel($this->container);
68
        $session = $this->container->getSession();
69
70
        $userToken = $cookie->getCookie("rememberMe");
71
        if($userToken && $this->isHexa($userToken))
72
        {
73
            //we have a rememberMe Hash, login
74
            $rememberedLogin = $rememberedLoginModel->findByToken($userToken);
75
            if($rememberedLogin){
76
                //we have a hash, login
77
                $user = $userModel->getUserDetailsById($rememberedLogin->users_idusers);
78
                $session->regenerateSessionId(); //regenerate the ID to avoid session ghosting
79
                $session->set("user", $user);
80
                $userRoleName = $user->role_name ?? "";
81
                $userRoleLevel = $user->role_level ?? 0;
82
                $session->set('user_role_name', $userRoleName);
83
                $session->set('user_role_level', $userRoleLevel);
84
            }
85
86
        }
87
88
    }
89
}