Passed
Pull Request — master (#10)
by
unknown
11:27 queued 10s
created

EtagConditionals::defaultGetEtag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Werk365\EtagConditionals;
4
5
use Closure;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class EtagConditionals
11
{
12
    /**
13
     * The callback used to generate the ETag.
14
     *
15
     * @var \Closure|null
16
     */
17
    private static $etagGenerateCallback;
18
19
    /**
20
     * Set a callback that should be used when generating the ETag.
21
     *
22
     * @param  \Closure|null  $callback
23
     * @return void
24
     */
25
    public static function etagGenerateUsing(?Closure $callback): void
26
    {
27
        static::$etagGenerateCallback = $callback;
0 ignored issues
show
Bug introduced by
Since $etagGenerateCallback is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $etagGenerateCallback to at least protected.
Loading history...
28
    }
29
30
    /**
31
     * Get ETag value for this request and response.
32
     *
33
     * @param  \Illuminate\Http\Request  $request
34
     * @param  \Symfony\Component\HttpFoundation\Response  $response
35
     * @return string
36
     */
37
    public static function getEtag(Request $request, Response $response): string
38
    {
39
        if (static::$etagGenerateCallback) {
0 ignored issues
show
Bug introduced by
Since $etagGenerateCallback is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $etagGenerateCallback to at least protected.
Loading history...
40
            $etag = call_user_func(static::$etagGenerateCallback, $request, $response);
41
        } else {
42
            $etag = static::defaultGetEtag($request, $response);
43
        }
44
45
        return (string) Str::of($etag)->start('"')->finish('"');
46
    }
47
48
    /**
49
     * Get default ETag value.
50
     *
51
     * @param  \Illuminate\Http\Request  $request
52
     * @param  \Symfony\Component\HttpFoundation\Response  $response
53
     * @return string
54
     */
55
    private static function defaultGetEtag(Request $request, Response $response): string
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    private static function defaultGetEtag(/** @scrutinizer ignore-unused */ Request $request, Response $response): string

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

Loading history...
56
    {
57
        return md5($response->getContent());
58
    }
59
}
60