Completed
Pull Request — master (#13)
by
unknown
02:52
created

XFrameOptions::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
namespace FMUP\Response\Header;
3
4
use FMUP\Response\Header;
5
6
class XFrameOptions extends Header
7
{
8
    const TYPE = 'X-Frame-Options';
9
10
    const OPTIONS_DENY = 'Deny';
11
    const OPTIONS_SAMEORIGIN = 'Sameorigin';
12
    const OPTIONS_ALLOW_FROM = 'ALLOW_FROM';
13
    const OPTIONS_ALLOW_FROM_URI_DEFAULT = '*';
14
15
    private $options = self::OPTIONS_DENY;
16
    private $uri = self::OPTIONS_DENY;
17
18
    /**
19
     * @param bool $options
20
     */
21
    public function __construct($options,$uri = array())
22
    {
23
        $this->setOptions($options);
24
        $this->setURI($uri);
25
    }
26
27
    public function setOptions($options){
28
       $this->options = $options;
29
       return $this;
30
    }
31
32
    public function getOptions(){
33
        return $this->options;
34
    }
35
36
37
    public function setURI($uri)
38
    {
39
        $this->uri = $uri;
40
        return $this;
41
    }
42
43
    public function getURI()
44
    {
45
        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...
46
            $this->uri = array(self::OPTIONS_ALLOW_FROM_URI_DEFAULT);
47
        }
48
        return $this->uri;
49
    }
50
51
    /**
52
     * Value returned in the header
53
     * @return string
54
     */
55
    public function getValue()
56
    {
57
        $retour='';
58
        if($this->getOptions()==self::OPTIONS_ALLOW_FROM){
59
            $urls = $this->getURI();
60
            foreach($urls as $url){
61
                $retour.= $this->options.' '.$url.';';
62
            }
63
        }else{
64
            $retour = $this->options;
65
        }
66
        return $retour;
67
    }
68
69
    /**
70
     * Type for the header. Can be used to determine header to send
71
     * @return string
72
     */
73
    public function getType()
74
    {
75
        return self::TYPE;
76
    }
77
}
78