Completed
Push — master ( 9ac47a...a56403 )
by Andrey
03:19 queued 53s
created

Content::file()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 29
rs 5.3846
cc 8
eloc 23
nc 36
nop 1
1
<?php
2
namespace humanity;
3
4
class Content {
5
6
    private static $host;
7
    private static $path;
8
    private static $config; 
9
    private static $js;
10
    private static $css;
11
    private static $app;
12
    private static $view;
13
    private static $widget;
14
    private static $title;
15
    private static $description;
16
    private static $keywords;
17
    private static $uri;
18
19
    public function __construct(){
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER 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...
20
        # Config
21
        self::$config = (new Config)->get();
22
        # Host
23
        self::$host = parse_url('http://'.$_SERVER['HTTP_HOST'])['host'];
24
        # Url
25
        self::$path = explode('/',parse_url(urldecode($_SERVER['REQUEST_URI']))['path']);
26 View Code Duplication
        foreach(self::$path as $key=>$value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
27
            $value = trim($value);
28
            if(empty($value)) unset(self::$path[$key]);
29
        }
30
        self::$path = array_values(self::$path);
31
        # Application
32
        self::$app = new Application;
33
        # View
34
        self::$view = new View;
35
        # Widget
36
        self::$widget = new Widget;
37
        # Js
38
        self::$js = new Js;
39
        # Css
40
        self::$css = new Css;
41
        # Title
42
        self::$title = new Title;
43
        # Desctiption
44
        self::$description = new Description;
45
        # Keywords
46
        self::$keywords = new Keywords;
47
        # Uri
48
        self::$uri = explode('/',$_SERVER['REQUEST_URI']);
49 View Code Duplication
        foreach(self::$uri as $key=>$value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
            if(empty($value)) unset(self::$uri[$key]);
51
            else self::$uri[$key] = urldecode($value);
52
        }
53
        self::$uri = array_values(self::$uri);
54
    }
55
56
    public function view($name){
57
        $file = $this->file($name);
58
        if(is_file($file)) require $file;
59
    }
60
61
    public function page(){
62
        # get file
63
        $file = $this->file();
64
        # get content
65
        ob_start();
66
        require $file;
67
        $content = ob_get_clean();
68
        # get javascript
69
        $js = self::$js->get();
70
        # get css
71
        $css = self::$css->get();
72
        # get title
73
        $title = self::$title->get();
74
        # get description
75
        $description = self::$description->get();
76
        # get keywords
77
        $keywords = self::$keywords->get();
78
        # include for page
79
        $content = preg_replace('/<head>/',"<head>$keywords",$content);
80
        $content = preg_replace('/<head>/',"<head>$description",$content);
81
        $content = preg_replace('/<head>/',"<head>$title",$content);
82
        $content = preg_replace('/<\/head>/',"$css</head>",$content);
83
        $content = preg_replace('/<\/body>/',"$js</body>",$content);
84
        echo $content;
85
    }
86
87
    # $type - page,view
88
    public function file($name=false){
89
        if($name === false){
90
            $path = self::$config['core']['page'];
91
        } else {
92
            $path = self::$config['core']['view'].'/'.$name;
93
        }
94
        # Route
95
        $uri = self::$path;
96
        array_push($uri,'');
97
        foreach($uri as $key=>$value){ if(empty(trim($value))) { unset($uri[$key]); continue; } }
98
        $uri = array_values($uri);
99
        $isFile = null;
0 ignored issues
show
Unused Code introduced by
$isFile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
        $currentUri = null;
0 ignored issues
show
Unused Code introduced by
$currentUri is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
        $filePhtml = null;
102
        for($i=count($uri)-1; $i>=0; $i--){
103
          $nextUri = implode('/',array_slice($uri,0,$i+1));
104
          $getFile = function($type) use($path,$nextUri){
105
            $file = $path.'/'.$nextUri.'/index.'.$type;
106
            if(is_file($file)) return $file;
107
            return null;
108
          };
109
          $filePhtml = $getFile('phtml');
110
          if($filePhtml) { $currentUri = $nextUri; break; }
0 ignored issues
show
Unused Code introduced by
$currentUri is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
        }
112
        if($filePhtml === null) {
113
          $filePhtml = $path.'/index.phtml';
114
        }
115
        return $filePhtml;
116
    }
117
118
}
119
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
120