Completed
Push — master ( f86b78...03e4c8 )
by Samuel
12:42
created

CustomHeaderMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 14 3
1
<?php
2
3
namespace Kelemen\ApiNette\Middleware;
4
5
use Nette\Http\Request;
6
use Nette\Http\Response;
7
8
class CustomHeaderMiddleware implements Middleware
9
{
10
    public function __construct($name, $value, $where = 'before')
11
    {
12
        $this->name = $name;
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13
        $this->value = $value;
0 ignored issues
show
Bug introduced by
The property value does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
        $this->where = $where;
0 ignored issues
show
Bug introduced by
The property where does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
    }
16
17
    public function __invoke(Request $request, Response $response, callable $next)
18
    {
19
        if ($this->where == 'before') {
20
            $response->setHeader($this->name, $this->value);
21
        }
22
23
        $resp = $next($request, $response);
24
25
        if ($this->where == 'after') {
26
            $response->setHeader($this->name, $this->value);
27
        }
28
29
        return $resp;
30
    }
31
32
}