RequestUtils   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 38
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 172
rs 8.3999

11 Methods

Rating   Name   Duplication   Size   Complexity  
A isPost() 0 4 1
B preventSpam() 0 38 6
A parseSeed() 0 10 3
B parseSpacebarSize() 0 12 5
A parseText() 0 7 2
A parsePrimaryColor() 0 10 3
A parseSecondaryColor() 0 10 3
A parseKeyboardLayout() 0 10 3
C parsePattern() 0 37 8
A isChecked() 0 10 3
A preparePdfHeader() 0 11 1
1
<?php
2
namespace raphiz\passwordcards;
3
4
class RequestUtils
5
{
6
    public static function isPost()
0 ignored issues
show
Coding Style introduced by
isPost 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...
7
    {
8
        return $_SERVER['REQUEST_METHOD'] == "POST";
9
    }
10
11
    public static function preventSpam()
0 ignored issues
show
Coding Style introduced by
preventSpam 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...
12
    {
13
        $ip = $_SERVER['REMOTE_ADDR'];
14
        $blacklistfile = __DIR__ . '/../blacklist/' . $ip;
15
        $count = 0;
16
        $creationDate = 0;
17
        if (file_exists($blacklistfile)) {
18
            $contents = (int)file_get_contents($blacklistfile);
19
            // If the stored value is big, it's the unix timestamp.
20
            // Otherwise it's the amount of created cards.
21
            if ($contents > 5) {
22
                $creationDate = $contents;
23
            } else {
24
                $count = $contents;
25
            }
26
        }
27
28
29
        if ($creationDate > 0) {
30
            // If blocked time is over, release lock
31
            if ($creationDate - time() < 0) {
32
                file_put_contents($blacklistfile, 0);
33
            } else {
34
                return $creationDate - time();
35
            }
36
        }
37
38
        if ($count === 5) {
39
            // Write unix timestamp into the blacklist file. The
40
            // ip is blocked till then.
41
            file_put_contents($blacklistfile, time() + 5*60);
42
        } else {
43
            // increment count...
44
            file_put_contents($blacklistfile, ($count+1));
45
        }
46
47
        return true;
48
    }
49
50
    public static function parseSeed()
0 ignored issues
show
Coding Style introduced by
parseSeed uses the super-global variable $_POST 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...
51
    {
52
        if (
53
            isset($_POST['seed']) &&
54
            is_numeric($_POST['seed'])
55
        ) {
56
            return $_POST['seed'];
57
        }
58
        return null;
59
    }
60
61
    public static function parseSpacebarSize()
0 ignored issues
show
Coding Style introduced by
parseSpacebarSize uses the super-global variable $_POST 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...
62
    {
63
        if (
64
            isset($_POST['space-length']) &&
65
            is_numeric($_POST['space-length']) &&
66
            $_POST['space-length'] < 8 &&
67
            $_POST['space-length'] > 0
68
        ) {
69
            return $_POST['space-length'];
70
        }
71
        return 8;
72
    }
73
74
    public static function parseText()
0 ignored issues
show
Coding Style introduced by
parseText uses the super-global variable $_POST 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...
75
    {
76
        if (isset($_POST['msg'])) {
77
            return substr($_POST['msg'], 0, 20);
78
        }
79
        return '';
80
    }
81
82
    public static function parsePrimaryColor()
0 ignored issues
show
Coding Style introduced by
parsePrimaryColor uses the super-global variable $_POST 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...
83
    {
84
        if (
85
            isset($_POST['primaryColor']) &&
86
            preg_match("/#[0-9a-zA-Z]{6}/", $_POST['primaryColor'])
87
        ) {
88
            return $_POST['primaryColor'];
89
        }
90
        return '#1ABC9C';
91
    }
92
93
    public static function parseSecondaryColor()
0 ignored issues
show
Coding Style introduced by
parseSecondaryColor uses the super-global variable $_POST 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...
94
    {
95
        if (
96
            isset($_POST['secondaryColor']) &&
97
            preg_match("/#[0-9a-zA-Z]{6}/", $_POST['secondaryColor'])
98
        ) {
99
            return $_POST['secondaryColor'];
100
        }
101
        return '#ffffff';
102
    }
103
104
    public static function parseKeyboardLayout()
0 ignored issues
show
Coding Style introduced by
parseKeyboardLayout uses the super-global variable $_POST 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...
105
    {
106
        if (
107
            isset($_POST['keyboardlayout']) &&
108
            preg_match("/qwerty|qwertz/", $_POST['keyboardlayout'])
109
        ) {
110
            return strtolower($_POST['keyboardlayout']);
111
        }
112
        return 'qwerty';
113
    }
114
    public static function parsePattern()
0 ignored issues
show
Coding Style introduced by
parsePattern uses the super-global variable $_POST 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...
115
    {
116
         $pattern = "";
117
118
        // With numbers?
119
        if (self::isChecked('with-numbers')) {
120
            $pattern .= '0-9';
121
        }
122
123
        // With lower?
124
        if (self::isChecked('with-lower')) {
125
            $pattern .= 'a-z';
126
        }
127
128
        // With upper?
129
        if (self::isChecked('with-upper')) {
130
            $pattern .= 'A-Z';
131
        }
132
133
        // With symbols?
134
        if (self::isChecked('with-symbols')) {
135
            $pattern .= '*-*';
136
        }
137
138
        // With space?
139
        if (self::isChecked('with-space')) {
140
            $pattern .= ' ';
141
        }
142
143
        // With others?
144
        if (self::isChecked('with-other')) {
145
            if (isset($_POST['other-chars'])) {
146
                $pattern .= substr($_POST['other-chars'], 0, 20);
147
            }
148
        }
149
        return $pattern;
150
    }
151
152
    private static function isChecked($parameter)
0 ignored issues
show
Coding Style introduced by
isChecked uses the super-global variable $_POST 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...
153
    {
154
        if (
155
            isset($_POST[$parameter]) &&
156
            $_POST[$parameter] === "on"
157
        ) {
158
            return true;
159
        }
160
        return false;
161
    }
162
163
    public static function preparePdfHeader($length)
164
    {
165
        header('Content-Description: File Transfer');
166
        header('Content-Type: application/pdf');
167
        header('Content-Disposition: attachment; filename=passwordcard.pdf');
168
        header('Content-Transfer-Encoding: binary');
169
        header('Content-Length: ' . $length);
170
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
171
        header('Expires: 0');
172
        header('Pragma: public');
173
    }
174
175
}
176