AbstractApi   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 15
c 2
b 0
f 2
dl 0
loc 94
ccs 16
cts 16
cp 1
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 3 1
A getRequest() 0 3 1
A preflight() 0 4 1
A getService() 0 3 1
A getContainer() 0 3 1
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Reuse\Controllers;
6
7
use App\Interfaces\Controllers\IApi;
8
use Nymfonya\Component\Container;
9
use Nymfonya\Component\Http\Request;
10
use Nymfonya\Component\Http\Response;
11
use Monolog\Logger;
12
13
abstract class AbstractApi implements IApi
14
{
15
16
    /**
17
     * request
18
     *
19
     * @var Request
20
     */
21
    protected $request;
22
23
    /**
24
     * response
25
     *
26
     * @var Response
27
     */
28
    protected $response;
29
30
    /**
31
     * logger
32
     *
33
     * @var Logger
34
     */
35
    protected $logger;
36
37
    /**
38
     * di container
39
     *
40
     * @var Container
41
     */
42
    private $container;
43
44
    /**
45
     * instanciate
46
     *
47
     */
48 6
    public function __construct(Container $container)
49
    {
50 6
        $this->container = $container;
51 6
        $this->request = $this->getService(Request::class);
52 6
        $this->response = $this->getService(Response::class);
53 6
        $this->logger = $this->getService(Logger::class);
54
    }
55
56
    /**
57
     * preflight CORS with OPTIONS method
58
     *
59
     * @Methods OPTIONS
60
     * @return IApi
61
     */
62 1
    public function preflight(): IApi
63
    {
64 1
        $this->response->setCode(200)->setContent([]);
65 1
        return $this;
66
    }
67
68
    /**
69
     * returns container service from a service name
70
     *
71
     * @param string $serviceName
72
     * @return object
73
     */
74 1
    protected function getService(string $serviceName)
75
    {
76 1
        return $this->container->getService($serviceName);
77
    }
78
79
    /**
80
     * return container instance
81
     *
82
     * @return Container
83
     */
84 1
    protected function getContainer(): Container
85
    {
86 1
        return $this->container;
87
    }
88
89
    /**
90
     * get request instance
91
     *
92
     * @return Request
93
     */
94 1
    protected function getRequest(): Request
95
    {
96 1
        return $this->request;
97
    }
98
99
    /**
100
     * get request params
101
     *
102
     * @return array
103
     */
104 1
    protected function getParams(): array
105
    {
106 1
        return $this->getRequest()->getParams();
107
    }
108
}
109