Completed
Push — master ( 8ca430...3024c9 )
by Michael
03:12
created

XoopstubeXoopstube::initModule()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 9.2
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
/**
12
 *  Xoopstube class
13
 *
14
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
15
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package         Xoopstube
17
 * @subpackage      Utils
18
 * @since           1.0
19
 * @author          trabis <[email protected]>
20
 */
21
// defined('XOOPS_ROOT_PATH') || die('XOOPS Root Path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
23
/**
24
 * Class XoopstubeXoopstube
25
 */
26
class XoopstubeXoopstube
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    var $dirname;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $dirname.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
29
    var $module;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $module.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
30
    var $handler;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $handler.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
31
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
32
    var $debug;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $debug.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
33
    var $debugArray = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $debugArray.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
34
35
    /**
36
     * @param $debug
37
     */
38
    protected function __construct($debug)
39
    {
40
        $this->debug   = $debug;
41
        $this->dirname = basename(dirname(__DIR__));
42
    }
43
44
    /**
45
     * @param bool $debug
46
     *
47
     * @return XoopstubeXoopstube
48
     */
49
    static function &getInstance($debug = false)
50
    {
51
        static $instance = false;
52
        if (!$instance) {
53
            $instance = new self($debug);
54
        }
55
//error_log("istance: [" . print_r($istance,true) . "]");
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
//phpinfo();
57
//debug_print_backtrace ();
58
        return $instance;
59
    }
60
61
    function &getModule()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
62
    {
63
        if ($this->module == null) {
64
            $this->initModule();
65
        }
66
67
        return $this->module;
68
    }
69
70
    /**
71
     * @param null $name
72
     *
73
     * @return null
74
     */
75
    function getConfig($name = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
    {
77
        if ($this->config == null) {
78
            $this->initConfig();
79
        }
80
        if (!$name) {
81
            $this->addLog("Getting all config");
82
83
            return $this->config;
84
        }
85
        if (!isset($this->config[$name])) {
86
            $this->addLog("ERROR :: CONFIG '{$name}' does not exist");
87
88
            return null;
89
        }
90
        $this->addLog("Getting config '{$name}' : " . print_r($this->config[$name], true));
91
92
        return $this->config[$name];
93
    }
94
95
    /**
96
     * @param null $name
97
     * @param null $value
98
     *
99
     * @return mixed
100
     */
101
    function setConfig($name = null, $value = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
102
    {
103
        if ($this->config == null) {
104
            $this->initConfig();
105
        }
106
        $this->config[$name] = $value;
107
        $this->addLog("Setting config '{$name}' : " . $this->config[$name]);
108
109
        return $this->config[$name];
110
    }
111
112
    /**
113
     * @param $name
114
     *
115
     * @return mixed
116
     */
117
    function &getHandler($name)
118
    {
119
        if (!isset($this->handler[$name . '_handler'])) {
120
            $this->initHandler($name);
121
        }
122
        $this->addLog("Getting handler '{$name}'");
123
124
        return $this->handler[$name . '_handler'];
125
    }
126
127
    function initModule()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
    {
129
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
130
        if (isset($xoopsModule) && is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $this->dirname) {
131
            $this->module = $xoopsModule;
132
        } else {
133
            $hModule      = xoops_gethandler('module');
134
            $this->module = $hModule->getByDirname($this->dirname);
135
        }
136
        $this->addLog('INIT MODULE');
137
    }
138
139
    function initConfig()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
140
    {
141
        $this->addLog('INIT CONFIG');
142
        $hModConfig   = xoops_gethandler('config');
143
        $this->config = $hModConfig->getConfigsByCat(0, $this->getModule()->getVar('mid'));
144
    }
145
146
    /**
147
     * @param $name
148
     */
149
    function initHandler($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
150
    {
151
        $this->addLog('INIT ' . $name . ' HANDLER');
152
        $this->handler[$name . '_handler'] = xoops_getModuleHandler($name, $this->dirname);
153
    }
154
155
    /**
156
     * @param $log
157
     */
158
    function addLog($log)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
addLog uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
159
    {
160
        if ($this->debug) {
161
            if (is_object($GLOBALS['xoopsLogger'])) {
162
                $GLOBALS['xoopsLogger']->addExtra($this->module->name(), $log);
163
            }
164
        }
165
    }
166
}
167
168
/**
169
 * Class WfdownloadsDownloadHandler
170
 */
171
class XoopstubeXoopstubeHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
172
{
173
    /**
174
     * @var WfdownloadsWfdownloads
175
     * @access public
176
     */
177
    public $xoopstube = null;
178
179
    /**
180
     * @param null|object $db
181
     */
182
    public function __construct(&$db)
183
    {
184
        parent::__construct($db, 'xoopstube_videos', 'XoopstubeXoopstube', 'lid', 'title');
185
        $this->xoopstube = XoopstubeXoopstube::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \XoopstubeXoopstube::getInstance() of type object<XoopstubeXoopstube> is incompatible with the declared type object<WfdownloadsWfdownloads> of property $xoopstube.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
186
    }
187
188
    /**
189
     * Get criteria for active videos
190
     *
191
     * @return CriteriaElement
0 ignored issues
show
Documentation introduced by
Should the return type not be CriteriaCompo?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
192
     */
193
    public function getActiveCriteria()
194
    {
195
        global $xoopsUser;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
196
        $gperm_handler = xoops_gethandler('groupperm');
197
198
        $criteria = new CriteriaCompo(new Criteria('offline', false));
199
        $criteria->add(new Criteria('published', 0, '>'));
200
        $criteria->add(new Criteria('published', time(), '<='));
201
        $expiredCriteria = new CriteriaCompo(new Criteria('expired', 0));
202
        $expiredCriteria->add(new Criteria('expired', time(), '>='), 'OR');
203
        $criteria->add($expiredCriteria);
204
        // add criteria for categories that the user has permissions for
205
        $groups                   = is_object($xoopsUser) ? $xoopsUser->getGroups() : array(0 => XOOPS_GROUP_ANONYMOUS);
206
        $allowedDownCategoriesIds = $gperm_handler->getItemIds('XTubeCatPerm', $groups, $this->xoopstube->getModule()->mid());
207
        $criteria->add(new Criteria('cid', '(' . implode(',', $allowedDownCategoriesIds) . ')', 'IN'));
208
209
        return $criteria;
210
    }
211
}
212