Passed
Push — master ( 5198fb...6eb156 )
by Thierry
02:34
created

RequestTrait.php$0 ➔ getParameterReader()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Di\Traits;
4
5
use Jaxon\Config\ConfigManager;
6
use Jaxon\Di\Container;
7
use Jaxon\Plugin\Manager\PluginManager;
8
use Jaxon\Request\Factory;
9
use Jaxon\Request\Factory\ParameterFactory;
10
use Jaxon\Request\Factory\RequestFactory;
11
use Jaxon\Request\Handler\CallbackManager;
12
use Jaxon\Request\Handler\ParameterReader;
13
use Jaxon\Request\Handler\RequestHandler;
14
use Jaxon\Request\Handler\UploadHandler;
15
use Jaxon\Request\Plugin\CallableClass\CallableRegistry;
16
use Jaxon\Request\Upload\NameGeneratorInterface;
17
use Jaxon\Request\Upload\UploadManager;
18
use Jaxon\Request\Validator;
19
use Jaxon\Response\Manager\ResponseManager;
20
use Jaxon\Response\Plugin\DataBag\DataBagPlugin;
21
use Jaxon\Ui\Dialogs\DialogFacade;
22
use Jaxon\Ui\Pagination\Paginator;
23
use Jaxon\Utils\Http\UriDetector;
24
use Jaxon\Utils\Translation\Translator;
25
use Nyholm\Psr7\Factory\Psr17Factory;
26
use Nyholm\Psr7Server\ServerRequestCreator;
27
use Psr\Http\Message\ServerRequestInterface;
28
29
use function bin2hex;
30
use function random_bytes;
31
32
trait RequestTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait RequestTrait
Loading history...
33
{
34
    /**
35
     * Register the values into the container
36
     *
37
     * @return void
38
     */
39
    private function registerRequests()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
40
    {
41
        // The server request
42
        $this->set(ServerRequestCreator::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

42
        $this->/** @scrutinizer ignore-call */ 
43
               set(ServerRequestCreator::class, function() {
Loading history...
43
            $xRequestFactory = new Psr17Factory();
44
            return new ServerRequestCreator(
45
                $xRequestFactory, // ServerRequestFactory
46
                $xRequestFactory, // UriFactory
47
                $xRequestFactory, // UploadedFileFactory
48
                $xRequestFactory  // StreamFactory
49
            );
50
        });
51
        $this->set(ServerRequestInterface::class, function($c) {
52
            return $c->g(ServerRequestCreator::class)->fromGlobals();
53
        });
54
        // The parameter reader
55
        $this->set(ParameterReader::class, function($c) {
56
            return new ParameterReader($c->g(Container::class), $c->g(ConfigManager::class),
57
                $c->g(Translator::class), $c->g(UriDetector::class));
58
        });
59
        // Callback Manager
60
        $this->set(CallbackManager::class, function() {
61
            return new CallbackManager();
62
        });
63
        // Request Handler
64
        $this->set(RequestHandler::class, function($c) {
65
            return new RequestHandler($c->g(Container::class), $c->g(PluginManager::class),
66
                $c->g(ResponseManager::class), $c->g(CallbackManager::class),
67
                $c->g(UploadHandler::class), $c->g(DataBagPlugin::class));
68
        });
69
        // Upload file and dir name generator
70
        $this->set(NameGeneratorInterface::class, function() {
71
            return new class implements NameGeneratorInterface
72
            {
73
                public function random(int $nLength): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function random()
Loading history...
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
74
                {
75
                    return bin2hex(random_bytes((int)($nLength / 2)));
76
                    /*try
0 ignored issues
show
Coding Style introduced by
Block comment text must start on a new line
Loading history...
77
                    {
78
                        return bin2hex(random_bytes((int)($nLength / 2)));
79
                    }
80
                    catch(Exception $e){}
81
                    // Generate the name
82
                    $sChars = '0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz';
83
                    return substr(str_shuffle($sChars), 0, $nLength);*/
84
                }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
85
            };
86
        });
87
        // File upload manager
88
        $this->set(UploadManager::class, function($c) {
89
            return new UploadManager($c->g(NameGeneratorInterface::class), $c->g(ConfigManager::class),
90
                $c->g(Validator::class), $c->g(Translator::class));
91
        });
92
        // File upload plugin
93
        $this->set(UploadHandler::class, function($c) {
94
            return !$c->g(ConfigManager::class)->getOption('core.upload.enabled') ? null :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
95
                new UploadHandler($c->g(Container::class), $c->g(ResponseManager::class), $c->g(Translator::class));
96
        });
97
        // Request Factory
98
        $this->set(Factory::class, function($c) {
99
            return new Factory($c->g(CallableRegistry::class), $c->g(RequestFactory::class),
100
                $c->g(ParameterFactory::class));
101
        });
102
        // Factory for requests to functions
103
        $this->set(RequestFactory::class, function($c) {
104
            $sPrefix = $c->g(ConfigManager::class)->getOption('core.prefix.function');
105
            return new RequestFactory($sPrefix, $c->g(DialogFacade::class), $c->g(Paginator::class));
106
        });
107
        // Parameter Factory
108
        $this->set(ParameterFactory::class, function() {
109
            return new ParameterFactory();
110
        });
111
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
112
113
    /**
114
     * Get the request
115
     *
116
     * @return ServerRequestInterface
117
     */
118
    public function getRequest(): ServerRequestInterface
119
    {
120
        return $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

120
        return $this->/** @scrutinizer ignore-call */ g(ServerRequestInterface::class);
Loading history...
121
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
122
123
    /**
124
     * Get the request handler
125
     *
126
     * @return RequestHandler
127
     */
128
    public function getRequestHandler(): RequestHandler
129
    {
130
        return $this->g(RequestHandler::class);
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * Get the upload handler
135
     *
136
     * @return UploadHandler|null
137
     */
138
    public function getUploadHandler(): ?UploadHandler
139
    {
140
        return $this->g(UploadHandler::class);
141
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
142
143
    /**
144
     * Get the upload manager
145
     *
146
     * @return UploadManager
147
     */
148
    public function getUploadManager(): UploadManager
149
    {
150
        return $this->g(UploadManager::class);
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
152
153
    /**
154
     * Get the callback manager
155
     *
156
     * @return CallbackManager
157
     */
158
    public function getCallbackManager(): CallbackManager
159
    {
160
        return $this->g(CallbackManager::class);
161
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
162
163
    /**
164
     * Get the parameter reader
165
     *
166
     * @return ParameterReader
167
     */
168
    public function getParameterReader(): ParameterReader
169
    {
170
        return $this->g(ParameterReader::class);
171
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
172
}
173