GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#20)
by
unknown
02:23
created

View::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
3
/**
4
 * The view class.
5
 *
6
 * Responsible for rendering files as HTML, encode JSON, with some helper methods
7
 *
8
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
9
 * @author     Omar El Gabry <[email protected]>
10
 */
11
12
class View {
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...
13
14
    /**
15
     * controller object that instantiated view object
16
     *
17
     * @var object
18
     */
19
    public $controller;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param Controller $controller
25
     */
26
    public function __construct(Controller $controller){
27
28
        $this->controller = $controller;
29
     }
30
31
    /**
32
     * Magic __get method
33
     *
34
     * @param $proporty
35
     */
36
	public function __get($property)
37
	{
38
		if(property_exists(get_class($this->controller), $property))
39
		{
40
			return (array_key_exists($property, get_object_vars($this->controller)) ? $this->controller->{$property} : $this->controller::$$property);
41
		}
42
		return $this->controller->loadModel($property);
43
	}
44
    /**
45
     * Renders and returns output for the given file with its array of data.
46
     *
47
     * @param  string  $filePath
48
     * @param  array   $data
49
     * @return string  Rendered output
50
     *
51
     */
52
    public function render($filePath, $data = null){
53
54
        if(!empty($data)) {
55
            extract($data);
56
        }
57
58
        // Using include Vs require is better,
59
        // because we may want to include a file more than once.
60
        ob_start();
61
        include $filePath . "" ;
62
        $renderedFile = ob_get_clean();
63
64
        $this->controller->response->setContent($renderedFile);
65
        return $renderedFile;
66
    }
67
68
    /**
69
     * Renders and returns output with header and footer for the given file with its array of data.
70
     *
71
     * @param  string  $layoutDir
72
     * @param  string  $filePath
73
     * @param  array   $data
74
     * @return string  Rendered output
75
     */
76
    public function renderWithLayouts($layoutDir, $filePath, $data = null){
77
78
        if(!empty($data)) {
79
            extract($data);
80
        }
81
82
        // you can use require_once() immediately without ob_start() & ob_get_clean()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
83
        // or use ob_start() & ob_get_clean() then return $renderedFile then echo,
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% 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...
84
        // but, using ob_start() & ob_get_clean() is a handy way, especially for ajax response.
85
        ob_start();
86
        require_once $layoutDir . "header.php";
87
        require_once $filePath  . "" ;
88
        require_once $layoutDir . "footer.php";
89
        $renderedFile = ob_get_clean();
90
91
        $this->controller->response->setContent($renderedFile);
92
        return $renderedFile;
93
    }
94
95
    /**
96
     * Render a JSON view.
97
     *
98
     * @param  array   $data
99
     * @return string  Rendered output
100
     *
101
     */
102
    public function renderJson($data){
103
104
        $jsonData = $this->jsonEncode($data);
105
106
        $this->controller->response->type('application/json')->setContent($jsonData);
107
        return $jsonData;
108
    }
109
110
    /**
111
     * Renders errors
112
     * A json respond will be sent in case of ajax call
113
     *
114
     * @param  array  $errors
115
     * @return mixed  Rendered output
116
     */
117 View Code Duplication
    public function renderErrors($errors){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
119
         $html = $this->render(Config::get('VIEWS_PATH') . 'alerts/errors.php', ["errors" => $errors]);
120
121
         if($this->controller->request->isAjax()){
122
             return $this->renderJson(array("error" => $html));
123
         }else{
124
             $this->controller->response->setContent($html);
125
             return $html;
126
         }
127
     }
128
129
    /**
130
     * Renders success message
131
     * A json respond will be sent in case of ajax call
132
     *
133
     * @param  string  $message
134
     * @return mixed  Rendered output
135
     */
136 View Code Duplication
    public function renderSuccess($message){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
138
         $html = $this->render(Config::get('VIEWS_PATH') . 'alerts/success.php', array("success" => $message));
139
140
         if($this->controller->request->isAjax()){
141
             return $this->renderJson(array("success" => $html));
142
         }else{
143
             $this->controller->response->setContent($html);
144
             return $html;
145
         }
146
     }
147
148
    /** *********************************************** **/
149
    /** **************     JSON View     ************** **/
150
    /** *********************************************** **/
151
152
     /**
153
      * Serialize array to JSON and used for the response
154
      *
155
      * @param  array   $data
156
      * @return string  Rendered output
157
      *
158
      */
159
    public function jsonEncode($data){
160
        return json_encode($data);
161
    }
162
163
    /** *********************************************** **/
164
    /** **************     Text Helper   ************** **/
165
    /** *********************************************** **/
166
167
    /**
168
     * Cuts a string to the length of $length and replaces the last characters
169
     * with the ellipsis => '...' if the text is longer than length.
170
     *
171
     * @param  string $str
172
     * @param  string $len
173
     * @return string the truncated string
174
     */
175
    public function truncate($str, $len){
176
177
        if(empty($str)) {
178
            return "";
179
        }else if(mb_strlen($str, 'UTF-8') > $len){
180
            return mb_substr($str, 0, $len, "UTF-8") . " ...";
181
        }else{
182
            return mb_substr($str, 0, $len, "UTF-8");
183
        }
184
    }
185
186
    /**
187
     * formats timestamp string coming from the database to "Month Day, Year"
188
     *
189
     * @param  string  $timestamp MySQL TIMESTAMP
190
     * @return string  Date after formatting.
191
     */
192
    public function timestamp($timestamp){
193
194
        $unixTime = strtotime($timestamp);
195
        $date = date("F j, Y", $unixTime);
196
197
        // What if date() failed to format? It will return false.
198
        return (empty($date))? "": $date;
199
    }
200
201
    /**
202
     * formats Unix timestamp string to "Month Day, Year"
203
     *
204
     * @param  integer	$unixtime Unix timestamp
205
     * @return string	Date after formatting.
206
     */
207
    public function unixtime($unixtime){
208
209
        $date = date("F j, Y", intval($unixtime));
210
        return (empty($date))? "": $date;
211
    }
212
213
    /**
214
     * formats Unix timestamp to be used in Date Picker in form of: "day/month/year"
215
     *
216
     * @param  integer	$unixtime Unix timestamp
217
     * @return string	Date after formatting.
218
     */
219
    public function datePicker($unixtime){
220
221
        $date = date("d/m/Y", (int)$unixtime);
222
        return (empty($date))? "": $date;
223
    }
224
225
    /**
226
     * Surround the links in a context with anchor tags.
227
     *
228
     * @param  string $str
229
     * @return string
230
     * @see http://stackoverflow.com/questions/5341168/best-way-to-make-links-clickable-in-block-of-text
231
     */
232
    public function autoLinks($str){
233
        return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $str);
234
    }
235
236
    /**
237
     * Converts characters to HTML entities
238
     * This is important to avoid XSS attacks, and attempts to inject malicious code in your page.
239
     *
240
     * @param  string $str The string.
241
     * @return string
242
     */
243
    public function encodeHTML($str){
244
        return htmlentities($str, ENT_QUOTES, 'UTF-8');
245
    }
246
247
    /**
248
     * It's same as encodeHTML(), But, also use nl2br() function in PHP
249
     *
250
     * @param  string	The string.
251
     * @return string	The string after converting characters and inserting br tags.
252
     */
253
    public function encodeHTMLWithBR($str){
254
        return nl2br(htmlentities($str, ENT_QUOTES, 'UTF-8'));
255
    }
256
257
} 
258