Completed
Pull Request — master (#13)
by
unknown
03:09
created

XFrameOptions::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace FMUP\Response\Header;
4
5
use FMUP\Response\Header;
6
7
class XFrameOptions extends Header
8
{
9
10
    const TYPE = 'X-Frame-Options';
11
    const OPTIONS_DENY = 'Deny';
12
    const OPTIONS_SAMEORIGIN = 'Sameorigin';
13
    const OPTIONS_ALLOW_FROM = 'ALLOW_FROM';
14
    const OPTIONS_ALLOW_FROM_URI_DEFAULT = '*';
15
16
    private $options = self::OPTIONS_DENY;
17
    private $uri = self::OPTIONS_DENY;
18
19
20
    /**
21
     * XFrameOptions constructor.
22
     * @param $options
23
     * @param array $uri
24
     */
25
    public function __construct($options, $uri = array())
26
    {
27
        $this->setOptions($options);
28
        $this->setUri($uri);
29
    }
30
31
    /**
32
     * Value returned in the header
33
     * @return string
34
     */
35 2
    public function getValue()
36
    {
37 2
        $return = '';
38 2
        $options = $this->getOptions();
39 2
        if ($options == self::OPTIONS_ALLOW_FROM) {
40 1
            $urls = $this->getUri();
41 1
            foreach ($urls as $url) {
42 1
                $return .= $options . ' ' . $url . ';';
43
            }
44
        } else {
45 1
            $return = $options;
46
        }
47 2
        return $return;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getOptions()
54
    {
55
        return $this->options;
56
    }
57
58
    /**
59
     * @param string $options
60
     * @return $this
61
     */
62
    public function setOptions($options)
63
    {
64
        $this->options = $options;
65
        return $this;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getUri()
72
    {
73
        if (!isset($this->uri) or empty($this->uri)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
74
            $this->uri = array(self::OPTIONS_ALLOW_FROM_URI_DEFAULT);
75
        }
76
        return $this->uri;
77
    }
78
79
    /**
80
     * @param array $uri
81
     * @return $this
82
     */
83
    public function setUri($uri)
84
    {
85
        $this->uri = $uri;
86
        return $this;
87
    }
88
89
    /**
90
     * Type for the header. Can be used to determine header to send
91
     * @return string
92
     */
93
    public function getType()
94
    {
95
        return self::TYPE;
96
    }
97
}
98