Completed
Push — dev-master ( a50fd8...2b6108 )
by Vijay
03:11
created

Base::addScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace FFCMS\Controllers;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Models};
7
8
/**
9
 * Base Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright (c) Copyright 2016 Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
abstract class Base
16
{
17
    use Traits\UrlHelper,
18
        Traits\Notification,
19
        Traits\SecurityController,
20
        Traits\Validation;
21
22
    /**
23
     * init
24
     * @param \Base $f3
0 ignored issues
show
Bug introduced by
There is no parameter named $f3. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     */
26
    public function __construct()
27
    {
28
        $this->oUrlHelper = Helpers\Url::instance();
29
        $this->oNotification = Helpers\Notifications::instance();
30
    }
31
32
33
    /**
34
     * Add default scripts for displaying templates - override in controller to add more
35
     *
36
     * @return void
37
     * @see app/config/default.ini
38
     */
39
    protected function addScripts()
40
    {
41
        // no scripts to add, override me and set css and js
42
        $this->setScripts();
43
    }
44
45
46
    /**
47
     * Set the scripts to load in the templates
48
     *
49
     * @param array $css list of css to load as defined in config.ini [css]
50
     * @param array $js list of js to load as defined in config.ini [js]
51
     * @return void
52
     * @see app/config/default.ini
53
     */
54
    protected function setScripts(array $css = [], array $js = [])
55
    {
56
        $f3 = \Base::instance();
57
        $env = ('production' == $f3->get('app.env')) ? 'production' : 'dev';
58
        $scripts = [];
59
        $scripts['css']['autoload'] = $css;
60
        $scripts['js']['autoload'] = $js;
61
        foreach (['js', 'css'] as $type) {
62
            $scripts[$type]['autoload'] = array_merge($scripts[$type]['autoload'], $f3->get($type . '.autoload'));
63
            $scripts[$type]['scripts'] = $f3->get($type . '.' . $env);
64
            $scripts[$type]['load'] = array_intersect_key($scripts[$type]['scripts'], array_flip($scripts[$type]['autoload']));
65
            $f3->set($type . '.load', $scripts[$type]['load']);
66
        }
67
    }
68
69
    /**
70
     * Logout if not admin
71
     *
72
     * @param \Base $f3
73
     * @param array $params
74
     * @return void
75
     */
76
     public function beforeRoute(\Base $f3, array $params)
77
    {
78
        // get logged in user info
79
        $usersModel = Models\Users::instance();
80
        $usersMapper = $usersModel->getMapper();
81
82
        // get the logged in user
83
        $uuid = $f3->get('uuid');
84
        if (empty($uuid)) {
85
            return;
86
        }
87
        $usersMapper->load(['uuid = ?', $uuid]);
88
89
        // invalid user, go to logout
90
        if (empty($usersMapper->uuid)) {
91
            $f3->clear('uuid');
92
            $f3->clear('SESSION');
93
            $f3->reroute('@logout');
94
            return;
95
        }
96
97
        // fetch the user scopes
98
        $user = $usersMapper->cast();
99
        $user['scopes'] = empty($user['scopes']) ? [] : preg_split("/[\s,]+/", $user['scopes']);
100
        $user['apiEnabled'] = (int) in_array('api', $user['scopes']);
101
        $f3->set('userScopes', $user['scopes']);
102
        $f3->set('isAdmin', in_array('admin', $user['scopes']));
103
        $f3->set('isRoot', in_array('root', $user['scopes']));
104
105
        // fetch addtional information for the user
106
        $usersData = $usersModel->getUserDetails($usersMapper->uuid, [
107
            'access_token',
108
            'refresh_token',
109
            'email_confirmed',
110
        ]);
111
112
        $f3->set('user', array_merge($user, $usersData));
113
114
        // add default css and js
115
        $this->addScripts();
116
    }
117
118
}
119