Completed
Push — master ( f92be8...f4002a )
by Sebastian
09:58 queued 06:29
created

RobotsMiddleware::handle()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 3
nop 2
1
<?php
2
3
namespace Spatie\RobotsMiddleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class RobotsMiddleware
9
{
10
    protected $response;
11
12
    public function handle(Request $request, Closure $next)
13
    {
14
        $this->response = $next($request);
15
16
        $shouldIndex = $this->shouldIndex($request);
17
18
        if (is_bool($shouldIndex)) {
19
            return $this->responseWithRobots($shouldIndex ? 'all' : 'none');
20
        }
21
22
        if (is_string($shouldIndex)) {
23
            return $this->responseWithRobots($shouldIndex);
24
        }
25
26
        throw new InvalidIndexRule('An indexing rule needs to return a boolean or a string');
27
    }
28
29
    protected function responseWithRobots(string $contents)
30
    {
31
        $this->response->headers->set('x-robots-tag', $contents, false);
32
33
        return $this->response;
34
    }
35
36
    /**
37
     * @return string|bool
38
     */
39
    protected function shouldIndex(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        return true;
42
    }
43
}
44