PsrTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 41
dl 0
loc 152
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequestArguments() 0 3 1
A getPsrConfigMiddleware() 0 4 2
A getServerParams() 0 5 1
A getRequest() 0 3 1
A getPsrAjaxMiddleware() 0 3 1
A getPsrFactory() 0 3 1
A registerPsr() 0 49 2
A getPsrRequestHandler() 0 3 1
A getPsr17Factory() 0 3 1
1
<?php
2
3
namespace Jaxon\Di\Traits;
4
5
use Jaxon\App\I18n\Translator;
6
use Jaxon\Di\Container;
7
use Jaxon\Plugin\Response\Psr\PsrPlugin;
8
use Jaxon\Request\Handler\ParameterReader;
9
use Jaxon\Request\Handler\Psr\PsrAjaxMiddleware;
10
use Jaxon\Request\Handler\Psr\PsrConfigMiddleware;
11
use Jaxon\Request\Handler\Psr\PsrFactory;
12
use Jaxon\Request\Handler\Psr\PsrRequestHandler;
13
use Jaxon\Request\Handler\RequestHandler;
14
use Jaxon\Response\ResponseManager;
15
use Nyholm\Psr7\Factory\Psr17Factory;
16
use Nyholm\Psr7Server\ServerRequestCreator;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
use function is_array;
20
21
trait PsrTrait
22
{
23
    /**
24
     * @var string
25
     */
26
    private $sPsrConfig = 'jaxon.psr.config.file';
27
28
    /**
29
     * @var string
30
     */
31
    private $sPsrServerRequest = 'jaxon.psr.server.request';
32
33
    /**
34
     * Register the values into the container
35
     *
36
     * @return void
37
     */
38
    private function registerPsr()
39
    {
40
        // The server request
41
        $this->set(Psr17Factory::class, function() {
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->/** @scrutinizer ignore-call */ 
42
               set(Psr17Factory::class, function() {
Loading history...
42
            return new Psr17Factory();
43
        });
44
        $this->set(ServerRequestCreator::class, function($di) {
45
            $xPsr17Factory = $di->g(Psr17Factory::class);
46
            return new ServerRequestCreator(
47
                $xPsr17Factory, // ServerRequestFactory
48
                $xPsr17Factory, // UriFactory
49
                $xPsr17Factory, // UploadedFileFactory
50
                $xPsr17Factory, // StreamFactory
51
            );
52
        });
53
        $this->set(ServerRequestInterface::class, function($di) {
54
            return $di->g(ServerRequestCreator::class)->fromGlobals();
55
        });
56
        // Server request with the Jaxon request parameter as attribute
57
        $this->set($this->sPsrServerRequest, function($di) {
58
            /** @var ParameterReader */
59
            $xParameterReader = $di->g(ParameterReader::class);
60
            /** @var ServerRequestInterface */
61
            $xRequest = $di->g(ServerRequestInterface::class);
62
            $aRequestParameter = $xParameterReader->getRequestParameter($xRequest);
63
            return !is_array($aRequestParameter) ? $xRequest :
64
                $xRequest->withAttribute('jxncall', $aRequestParameter);
65
        });
66
        // PSR factory
67
        $this->set(PsrFactory::class, function($di) {
68
            return new PsrFactory($di->g(Container::class));
69
        });
70
        // PSR request handler
71
        $this->set(PsrRequestHandler::class, function($di) {
72
            return new PsrRequestHandler($di->g(Container::class), $di->g(RequestHandler::class),
73
                $di->g(ResponseManager::class), $di->g(Translator::class));
74
        });
75
        // PSR config middleware
76
        $this->set(PsrConfigMiddleware::class, function($di) {
77
            return new PsrConfigMiddleware($di->g(Container::class), $di->g($this->sPsrConfig));
78
        });
79
        // PSR ajax middleware
80
        $this->set(PsrAjaxMiddleware::class, function($di) {
81
            return new PsrAjaxMiddleware($di->g(Container::class), $di->g(RequestHandler::class),
82
                $di->g(ResponseManager::class));
83
        });
84
        // The PSR response plugin
85
        $this->set(PsrPlugin::class, function($di) {
86
            return new PsrPlugin($di->g(Psr17Factory::class), $di->g(ServerRequestInterface::class));
87
        });
88
    }
89
90
    /**
91
     * Get the request
92
     *
93
     * @return array
94
     */
95
    public function getServerParams(): array
96
    {
97
        /** @var ServerRequestInterface */
98
        $xRequest = $this->g(ServerRequestInterface::class);
0 ignored issues
show
Bug introduced by
It seems like g() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        /** @scrutinizer ignore-call */ 
99
        $xRequest = $this->g(ServerRequestInterface::class);
Loading history...
99
        return $xRequest->getServerParams();
100
    }
101
102
    /**
103
     * Get the request with Jaxon parameter as attribute
104
     *
105
     * @return ServerRequestInterface
106
     */
107
    public function getRequest(): ServerRequestInterface
108
    {
109
        return $this->g($this->sPsrServerRequest);
110
    }
111
112
    /**
113
     * Return the array of arguments from the GET or POST data
114
     *
115
     * @return array
116
     */
117
    public function getRequestArguments(): array
118
    {
119
        return $this->getRequest()->getAttribute('jxncall')['args'] ?? [];
120
    }
121
122
    /**
123
     * Get the PSR factory
124
     *
125
     * @return PsrFactory
126
     */
127
    public function getPsrFactory(): PsrFactory
128
    {
129
        return $this->g(PsrFactory::class);
130
    }
131
132
    /**
133
     * Get the Psr17 factory
134
     *
135
     * @return Psr17Factory
136
     */
137
    public function getPsr17Factory(): Psr17Factory
138
    {
139
        return $this->g(Psr17Factory::class);
140
    }
141
142
    /**
143
     * Get the PSR request handler
144
     *
145
     * @return PsrRequestHandler
146
     */
147
    public function getPsrRequestHandler(): PsrRequestHandler
148
    {
149
        return $this->g(PsrRequestHandler::class);
150
    }
151
152
    /**
153
     * Get the PSR config middleware
154
     *
155
     * @param string $sConfigFile
156
     *
157
     * @return PsrConfigMiddleware
158
     */
159
    public function getPsrConfigMiddleware(string $sConfigFile): PsrConfigMiddleware
160
    {
161
        !$this->h($this->sPsrConfig) && $this->val($this->sPsrConfig, $sConfigFile);
0 ignored issues
show
Bug introduced by
It seems like h() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
        !$this->/** @scrutinizer ignore-call */ h($this->sPsrConfig) && $this->val($this->sPsrConfig, $sConfigFile);
Loading history...
Bug introduced by
It seems like val() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
        !$this->h($this->sPsrConfig) && $this->/** @scrutinizer ignore-call */ val($this->sPsrConfig, $sConfigFile);
Loading history...
162
        return $this->g(PsrConfigMiddleware::class);
163
    }
164
165
    /**
166
     * Get the PSR ajax request middleware
167
     *
168
     * @return PsrAjaxMiddleware
169
     */
170
    public function getPsrAjaxMiddleware(): PsrAjaxMiddleware
171
    {
172
        return $this->g(PsrAjaxMiddleware::class);
173
    }
174
}
175