Resource   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 38
c 5
b 1
f 1
dl 0
loc 124
rs 10
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setQuery() 0 7 2
A getResponse() 0 3 1
A getArgument() 0 3 1
A __construct() 0 11 1
A setData() 0 8 3
A setArgument() 0 7 2
A getData() 0 3 1
A getQuery() 0 3 1
A setResponse() 0 9 1
1
<?php
2
3
namespace Erykai\Request;
4
5
/**
6
 * Resource Request
7
 */
8
abstract class Resource
9
{
10
    /**
11
     * @var object
12
     */
13
    private object $data;
14
    /**
15
     * @var object|null
16
     */
17
    private ?object $query = null;
18
    /**
19
     * @var object|null
20
     */
21
    private ?object $argument = null;
22
23
    /**
24
     * @var object
25
     */
26
    private object $response;
27
28
    /**
29
     * checks if there are requests and converts it to an object
30
     */
31
    public function __construct(?array $data = null)
32
    {
33
        $this->setData();
34
        $this->setQuery($data);
35
        $this->setArgument($data);
36
        $request = (object) [
37
            'request' => $this->getData(),
38
            'query' => $this->getQuery(),
39
            'argument' => $this->getArgument()
40
        ];
41
        $this->setResponse(200, "success","return data", $request);
42
    }
43
44
    /**
45
     * @return object
46
     */
47
    protected function getData(): object
48
    {
49
        return $this->data;
50
    }
51
52
    /**
53
     * checks if there are requests and converts it to an object
54
     */
55
    private function setData(): void
56
    {
57
        $this->data = (object)filter_input_array(INPUT_POST, FILTER_DEFAULT);
58
        if (count(get_object_vars($this->data)) === 0) {
59
            try {
60
                $this->data = json_decode(file_get_contents('php://input'), false, 512, JSON_THROW_ON_ERROR);
61
            } catch (\JsonException $e) {
62
                $this->setResponse(400,'error', "json php://input {$e->getMessage()} - {$e->getFile()} - {$e->getLine()}");
63
            }
64
        }
65
    }
66
    /**
67
     * @return object|null
68
     */
69
    protected function getQuery(): ?object
70
    {
71
        return $this->query;
72
    }
73
74
    /**
75
     * @param array|null $data
76
     * @return void
77
     */
78
    private function setQuery(?array $data): void
79
    {
80
        if (!empty($data['query'])) {
81
            $this->query = (object)filter_var_array($data['query'], FILTER_DEFAULT);
82
            return;
83
        }
84
        $this->query = null;
85
    }
86
87
    /**
88
     * @return object|null
89
     */
90
    protected function getArgument(): ?object
91
    {
92
        return $this->argument;
93
    }
94
95
    /**
96
     * @param array|null $data
97
     * @return void
98
     */
99
    private function setArgument(?array $data): void
100
    {
101
        if (!empty($data['argument'])) {
102
            $this->argument = (object)filter_var_array($data['argument'], FILTER_DEFAULT);
103
            return;
104
        }
105
        $this->argument = null;
106
    }
107
108
    /**
109
     * @return object
110
     */
111
    protected function getResponse(): object
112
    {
113
        return $this->response;
114
    }
115
116
    /**
117
     * @param int $code
118
     * @param string $type
119
     * @param string $message
120
     * @param object|null $data
121
     * @param string|null $dynamic
122
     */
123
    protected function setResponse(int $code, string $type, string $message, ?object $data = null, ?string $dynamic = null): void
124
    {
125
        http_response_code($code);
126
        $this->response = (object)[
127
            "code" => $code,
128
            "type" => $type,
129
            "message" => $message,
130
            "data" => $data,
131
            "dynamic" => $dynamic
132
        ];
133
    }
134
}