CheckRequest::isPutRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Jasny\Controller\Traits;
5
6
use Psr\Http\Message\ServerRequestInterface;
7
8
/**
9
 * Controller methods to check the request
10
 */
11
trait CheckRequest
12
{
13
    abstract protected function getRequest(): ServerRequestInterface;
14
    
15
    
16
    /**
17
     * Check if request is GET request
18
     */
19
    protected function isGetRequest(): bool
20
    {
21
        return $this->getRequest()->getMethod() === 'GET' || $this->getRequest()->getMethod() === '';
22
    }
23
24
    /**
25
     * Check if request is POST request
26
     */
27
    protected function isPostRequest(): bool
28
    {
29
        return $this->getRequest()->getMethod() === 'POST';
30
    }
31
32
    /**
33
     * Check if request is PUT request
34
     */
35
    protected function isPutRequest(): bool
36
    {
37
        return $this->getRequest()->getMethod() === 'PUT';
38
    }
39
40
    /**
41
     * Check if request is DELETE request
42
     */
43
    protected function isDeleteRequest(): bool
44
    {
45
        return $this->getRequest()->getMethod() === 'DELETE';
46
    }
47
    
48
    /**
49
     * Check if request is HEAD request
50
     */
51
    protected function isHeadRequest(): bool
52
    {
53
        return $this->getRequest()->getMethod() === 'HEAD';
54
    }
55
    
56
57
    /**
58
     * Returns the HTTP referer if it is on the current host
59
     */
60
    protected function getLocalReferer(): ?string
61
    {
62
        $request = $this->getRequest();
63
        $referer = $request->getHeaderLine('Referer');
64
        $host = $request->getHeaderLine('Host');
65
66
        return $referer !== '' && parse_url($referer, PHP_URL_HOST) === $host ? $referer : null;
67
    }
68
}
69