Completed
Push — master ( 08386a...7d12ab )
by Freek
01:17
created

AddCspHeaders::getProfiles()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 5
nop 2
1
<?php
2
3
namespace Spatie\Csp;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
9
class AddCspHeaders
10
{
11
    public function handle(Request $request, Closure $next, $customProfileClass = null)
12
    {
13
        $response = $next($request);
14
15
        $this
16
            ->getProfiles($customProfileClass, $response)
17
            ->filter->shouldBeApplied($request, $response)
18
            ->each->applyTo($response);
19
20
        return $response;
21
    }
22
23
    protected function getProfiles(string $customProfileClass = null, $response): Collection
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
24
    {
25
        $profiles = collect();
26
27
        if ($customProfileClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $customProfileClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            $profiles->push(ProfileFactory::create($customProfileClass));
29
30
            return $profiles;
31
        }
32
33
        $profileClass = config('csp.profile');
34
35
        if (!empty($profileClass)) {
36
37
            $profiles->push(ProfileFactory::create($profileClass));
38
39
        }
40
41
        $reportOnlyProfileClass = config('csp.report_only_profile');
42
43
        if (!empty($reportOnlyProfileClass)) {
44
            $profile = ProfileFactory::create($reportOnlyProfileClass);
45
46
            $profile->reportOnly();
47
48
            $profiles->push($profile);
49
        }
50
51
        return $profiles;
52
    }
53
}
54