Request   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 27
c 2
b 0
f 1
dl 0
loc 96
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 13 4
A toUri() 0 5 1
A addQuery() 0 5 1
A linkNew() 0 5 1
A linkCrawl() 0 5 1
A toUriWithMethod() 0 5 1
A withQuery() 0 5 1
A linkSelf() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use OutOfRangeException;
8
9
use function array_merge;
10
use function in_array;
11
12
/**
13
 * @property $this $lazy
14
 * @property $this $eager
15
 * @psalm-suppress PropertyNotSetInConstructor for DSL
16
 */
17
final class Request extends AbstractRequest
18
{
19
    public const GET = 'get';
20
    public const POST = 'post';
21
    public const PUT = 'put';
22
    public const PATCH = 'patch';
23
    public const DELETE = 'delete';
24
    public const HEAD = 'head';
25
    public const OPTIONS = 'options';
26
27
    /** @psalm-suppress ImplementedReturnTypeMismatch */
28
    public function __get(string $name): mixed
29
    {
30
        if ($name === 'eager' || $name === 'lazy') {
31
            $this->in = $name;
32
33
            return $this;
34
        }
35
36
        if (in_array($name, ['code', 'headers', 'body'], true)) {
37
            return parent::__get($name);
38
        }
39
40
        throw new OutOfRangeException($name);
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     *
46
     * @param array<string, mixed> $query
47
     */
48
    public function withQuery(array $query): RequestInterface
49
    {
50
        $this->query = $query;
51
52
        return $this;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function addQuery(array $query): RequestInterface
59
    {
60
        $this->query = array_merge($this->query, $query);
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function toUriWithMethod(): string
69
    {
70
        $uri = $this->toUri();
71
72
        return "{$this->method} {$uri}";
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function toUri(): string
79
    {
80
        $this->resourceObject->uri->query = $this->query;
81
82
        return (string) $this->resourceObject->uri;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function linkSelf(string $linkKey): RequestInterface
89
    {
90
        $this->links[] = new LinkType($linkKey, LinkType::SELF_LINK);
91
92
        return $this;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function linkNew(string $linkKey): RequestInterface
99
    {
100
        $this->links[] = new LinkType($linkKey, LinkType::NEW_LINK);
101
102
        return $this;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function linkCrawl(string $linkKey): RequestInterface
109
    {
110
        $this->links[] = new LinkType($linkKey, LinkType::CRAWL_LINK);
111
112
        return $this;
113
    }
114
}
115