AbstractProvider::getData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Previewtechs\HTTP\AccessLogger;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
7
/**
8
 * Class AbstractProvider
9
 * @package Previewtechs\API\AccessLogger
10
 */
11
abstract class AbstractProvider
12
{
13
    /**
14
     * @var ServerRequestInterface
15
     */
16
    public $request;
17
18
    /**
19
     * @return string
20
     */
21 5
    public function getIP()
22
    {
23 5
        return $this->request->getServerParams()['REMOTE_ADDR'];
24
    }
25
26
    /**
27
     * @return string
28
     */
29 5
    public function getEndpoint()
30
    {
31 5
        return $this->request->getUri()->getPath();
32
    }
33
34
    /**
35
     * @return string
36
     */
37 5
    public function getMethod()
38
    {
39 5
        return $this->request->getMethod();
40
    }
41
42
    /**
43
     * @return string|null
44
     */
45 5
    public function getVersion()
46
    {
47 5
        preg_match("/\/v[0-9]\//", $this->request->getUri()->getPath(), $match);
48 5
        return str_replace('/', '', $match[0]);
49
    }
50
51
    /**
52
     * @return \DateTime
53
     */
54 5
    public function getCreated()
55
    {
56 5
        return new \DateTime('now');
57
    }
58
59
    /**
60
     * @return array
61
     */
62 5
    public function getData()
63
    {
64
        return [
65 5
            'method' => $this->getMethod(),
66 5
            'endpoint' => $this->getEndpoint(),
67 5
            'version' => $this->getVersion(),
68 5
            'request_ip' => $this->getIP(),
69 5
            'created' => $this->getCreated()
70 5
        ];
71
    }
72
}
73