Response::options()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * ******************************************************************
4
 * Created by   Marko Kungla on 09 Oct 2016
5
 * @package     toolshedr
6
 * Encoding     UTF-8
7
 * File         Response.php
8
 * Code format  PSR-2 and 12
9
 * *******************************************************************/
10
11
namespace Toolshedr\Core;
12
13
use \Toolshedr\Interfaces\ResponseDataInterface;
14
use \Toolshedr\Core\{
15
    Headers,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Toolshedr\Core\Headers.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
    Request
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Toolshedr\Core\Request.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
};
18
19
class Response
20
{
21
    /**
22
     * @var int Response status code
23
     */
24
    private $code;
25
26
    /**
27
     * @var string status message
28
     */
29
    private $message;
30
31
    /**
32
     * @var ResponseDataInterface data to be returned
33
     */
34
    private $data;
35
36
    /**
37
     * Response constructor.
38
     */
39 4
    public function __construct()
40
    {
41 4
        $this->code = 200;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42 4
        $this->message = "OK";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal OK does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
43 4
    }
44
45
    /**
46
     * Set status code
47
     *
48
     * @param int $code
49
     */
50
    public function setCode(int $code)
51
    {
52
        $this->code = $code;
53
    }
54
55
    /**
56
     * Set response message
57
     *
58
     * @param string $message
59
     */
60
    public function setMessage(string $message)
61
    {
62
        $this->message = $message;
63
    }
64
65
    /**
66
     * Set response data object
67
     *
68
     * @param ResponseDataInterface $data
69
     */
70
    public function setData(ResponseDataInterface $data)
71
    {
72
        $this->data = $data;
73
    }
74
75
    /**
76
     * Handle Options request
77
     *
78
     * @param Headers $headers
79
     */
80
    public function options(Headers $headers)
81
    {
82
        if ($headers->containsRequiredHeaders()) {
83
            $this->setCode(202);
84
            $this->setMessage('Good to continue!!!');
85
            $headers->setStatusCode(202);
86
        } else {
87
            $this->setCode(400);
88
            $this->setMessage('Bad Request!');
89
            $headers->setStatusCode(400);
90
        }
91
92
    }
93
94
    /**
95
     * Handle requests
96
     *
97
     * @param Request $request
98
     * @param Header $headers
0 ignored issues
show
Documentation introduced by
Should the type for parameter $headers not be Headers?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
99
     */
100
    public function handle(Request &$request, Headers $headers)
101
    {
102
        if ($request->authenticate()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
103
            // go go go go
104
        } else {
105
            $this->setCode(401);
106
            $this->setMessage('Unauthorized!');
107
            $headers->setStatusCode(401);
108
        }
109
110
111
    }
112
113
    /**
114
     * Return Output object
115
     *
116
     * @return \stdClass
117
     */
118
    public function output()
119
    {
120
        $output = new \stdClass();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
121
        $output->code = $this->code;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
122
        $output->message = $this->message;
123
124
        if (is_object($this->data)) {
125
            $output->data = $this->data;
126
        }
127
128
        return $output;
129
    }
130
}
131