Checks   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 168
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A _validate() 0 13 2
B existance() 0 26 4
A setRoute() 0 15 2
A setTemplate() 0 10 1
A result() 0 15 4
1
<?php
2
3
/**
4
 * Contains some tools to check plugins with
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Plugins
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\plugins;
17
18
/**
19
 * Contains some tools to check plugins with
20
 *
21
 * @category  Core
22
 * @package   Plugins
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Checks
30
{
31
    /**
32
     * Plugin that will be used
33
     **/
34
    private $_plugin = '';
35
36
    /**
37
     * Local path
38
     **/
39
    private $_path = '';
40
41
    /**
42
     * File that will be used
43
     **/
44
    private $_file = '';
45
46
    /**
47
     * Remember positive existance checks
48
     **/
49
    private $_existance = false;
50
51
    /**
52
     * Get plugin to see if it exists
53
     *
54
     * @param string $plugin Name of plugin
55
     *
56
     * @return \csphere\core\plugins\Checks
57
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
58
59
    public function __construct($plugin)
60
    {
61
        $this->_path = \csphere\core\init\path();
62
63
        $this->_plugin = $plugin;
64
65
        // Check plugin details
66
        $this->_validate();
67
    }
68
69
    /**
70
     * Check if plugin is valid
71
     *
72
     * @throws \Exception
73
     *
74
     * @return void
75
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
76
77
    private function _validate()
78
    {
79
        $meta = new \csphere\core\plugins\Metadata();
80
81
        $exists = $meta->exists($this->_plugin);
82
83
        if ($exists == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
84
85
            $msg = 'Plugin not found: "' . $this->_plugin . '"';
86
87
            throw new \Exception($msg);
88
        }
89
    }
90
91
    /**
92
     * Checks if the target file exists
93
     *
94
     * @throws \Exception
95
     *
96
     * @return boolean
97
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
98
99
    public function existance()
100
    {
101
        $result = false;
102
103
        // Check for file existance if not done already
104
        if ($this->_existance == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
105
106
            $result = true;
107
        } else {
108
109
            $target = $this->_path . $this->_file;
110
111
            // File must set before
112
            if (empty($this->_file)) {
113
114
                throw new \Exception('No plugin file set to check for');
115
116
            } elseif (file_exists($target)) {
117
118
                $this->_existance = true;
119
                $result = true;
120
            }
121
        }
122
123
        return $result;
124
    }
125
126
    /**
127
     * Set target for requests
128
     *
129
     * @param string $target Name of target
130
     *
131
     * @throws \Exception
132
     *
133
     * @return void
134
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
135
136
    public function setRoute($target)
137
    {
138
        // Route should not contain other chars
139
        if (!preg_match("=^[_a-z0-9-]+$=i", $target)) {
140
141
            throw new \Exception('Name of plugin target contains unallowed chars');
142
        }
143
144
        $metadata = new metadata();
145
        $type = $metadata->templateType($this->_plugin, $target);
146
147
        $this->_file = 'csphere/plugins/' . $this->_plugin
148
            . '/actions/' . $type . '/' . $target . '.php';
149
150
    }
151
152
    /**
153
     * Set target for template files
154
     *
155
     * @param string $target Name of target
156
     *
157
     * @return void
158
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
159
160
    public function setTemplate($target)
161
    {
162
163
        $metadata = new metadata();
164
        $type = $metadata->templateType($this->_plugin, $target);
165
166
        $this->_file = 'csphere/plugins/' . $this->_plugin
167
            . '/templates/' . $type . '/' . $target . '.tpl';
168
169
    }
170
171
    /**
172
     * Replies with the target file
173
     *
174
     * @param boolean $path Append local path which defaults to true
175
     *
176
     * @throws \Exception
177
     *
178
     * @return string
179
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
180
181
    public function result($path = true)
182
    {
183
        $exists = $this->existance();
184
185
        // File must be there to return its origin
186
        if ($exists == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
187
188
            throw new \Exception('Plugin target not found: "' . $this->_file . '"');
189
        } else {
190
191
            $place = ($path == true) ? $this->_path . $this->_file : $this->_file;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
192
        }
193
194
        return isset($place) ? $place : '';
195
    }
196
}
197