Passed
Push — Auth ( c6ffd2...76831f )
by Stone
02:02
created

SiteConfig::__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\Modules;
4
5
use App\Models\CategoryModel;
6
use App\Models\UserModel;
7
use Core\Container;
8
use Core\Modules\Module;
9
use App\Models\ConfigModel;
10
11
class SiteConfig extends Module
12
{
13
    public function __construct(Container $container)
14
    {
15
        parent::__construct($container);
16
        $this->loginFromRememberMe();
17
    }
18
19
    /**
20
     * Gets the entire site configuration and arranges it into a displayable list
21
     * @return array the config ordered and ready to display
22
     * @throws \ReflectionException
23
     */
24
    public function getSiteConfig():array
25
    {
26
27
        $configs = new ConfigModel($this->container);
28
        $siteConfig = $configs->getAllConfig();
29
        $data = [];
30
        foreach ($siteConfig as $config) {
31
            $data[$config->configs_name] = $config->configs_value;
32
        }
33
        return $data;
34
    }
35
36
    /**
37
     * create the front end menu object to be sent to twig and add the urls
38
     * @return array
39
     * @throws \ReflectionException
40
     */
41
    public function getMenu():array
42
    {
43
        $categoryModel = new CategoryModel($this->container);
44
45
        $data = [];
46
        //get the categories from database
47
        $categories = $categoryModel->getCategories();
48
        foreach ($categories as $category) {
49
            $data += [
50
                $category->category_name => '/category/posts/' . $category->categories_slug
51
            ];
52
        }
53
        return $data;
54
    }
55
56
    public function loginFromRememberMe()
57
    {
58
        $cookie = $this->container->getCookie();
59
        $userModel = new UserModel($this->container);
60
        $session = $this->container->getSession();
61
62
        $userToken = $cookie->getCookie("rememberMe");
63
        if($userToken)
64
        {
65
            //we have a rememberMe Hash, login
66
            $rememberedLogin = $userModel->findByToken($userToken);
0 ignored issues
show
Bug introduced by
$userToken of type true is incompatible with the type string expected by parameter $token of App\Models\UserModel::findByToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            $rememberedLogin = $userModel->findByToken(/** @scrutinizer ignore-type */ $userToken);
Loading history...
67
            if($rememberedLogin){
68
                //we have a hash, login
69
                $user = $userModel->getUserDetailsById($rememberedLogin->users_idusers);
70
                $session->regenerateSessionId(); //regenerate the ID to avoid session ghosting
71
                $session->set("user", $user);
72
                $userRoleName = $user->role_name ?? "";
73
                $userRoleLevel = $user->role_level ?? 0;
74
                $session->set('user_role_name', $userRoleName);
75
                $session->set('user_role_level', $userRoleLevel);
76
            }
77
78
        }
79
80
    }
81
}