Passed
Push — master ( 6f7a23...2cf817 )
by Thierry
02:29
created

PsrTrait::getPsrConfigMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
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\Di\Container;
6
use Jaxon\Request\Factory\Psr\PsrFactory;
7
use Jaxon\Request\Handler\Psr\PsrAjaxMiddleware;
8
use Jaxon\Request\Handler\Psr\PsrConfigMiddleware;
9
use Jaxon\Request\Handler\Psr\PsrRequestHandler;
10
use Jaxon\Request\Handler\RequestHandler;
11
use Jaxon\Response\Manager\ResponseManager;
12
use Jaxon\Utils\Translation\Translator;
13
use Nyholm\Psr7\Factory\Psr17Factory;
14
use Nyholm\Psr7Server\ServerRequestCreator;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
trait PsrTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait PsrTrait
Loading history...
18
{
19
    /**
20
     * @var string
21
     */
22
    private $sPsrConfig = 'jaxon.psr.config.file';
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
23
24
    /**
25
     * Register the values into the container
26
     *
27
     * @return void
28
     */
29
    private function registerPsr()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
30
    {
31
        // The server request
32
        $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

32
        $this->/** @scrutinizer ignore-call */ 
33
               set(Psr17Factory::class, function() {
Loading history...
33
            return new Psr17Factory();
34
        });
35
        $this->set(ServerRequestCreator::class, function($c) {
36
            $xRequestFactory = $c->g(Psr17Factory::class);
37
            return new ServerRequestCreator(
38
                $xRequestFactory, // ServerRequestFactory
39
                $xRequestFactory, // UriFactory
40
                $xRequestFactory, // UploadedFileFactory
41
                $xRequestFactory  // StreamFactory
42
            );
43
        });
44
        $this->set(ServerRequestInterface::class, function($c) {
45
            return $c->g(ServerRequestCreator::class)->fromGlobals();
46
        });
47
        // PSR factory
48
        $this->set(PsrFactory::class, function($c) {
49
            return new PsrFactory($c->g(Container::class));
50
        });
51
        // PSR request handler
52
        $this->set(PsrRequestHandler::class, function($c) {
53
            return new PsrRequestHandler($c->g(Container::class), $c->g(RequestHandler::class),
54
                $c->g(ResponseManager::class), $c->g(Translator::class));
55
        });
56
        // PSR config middleware
57
        $this->set(PsrConfigMiddleware::class, function($c) {
58
            return new PsrConfigMiddleware($c->g(Container::class), $c->g($this->sPsrConfig));
59
        });
60
        // PSR ajax middleware
61
        $this->set(PsrAjaxMiddleware::class, function($c) {
62
            return new PsrAjaxMiddleware($c->g(Container::class), $c->g(RequestHandler::class),
63
                $c->g(ResponseManager::class));
64
        });
65
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
66
67
    /**
68
     * Get the request
69
     *
70
     * @return ServerRequestInterface
71
     */
72
    public function getRequest(): ServerRequestInterface
73
    {
74
        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

74
        return $this->/** @scrutinizer ignore-call */ g(ServerRequestInterface::class);
Loading history...
75
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
76
77
    /**
78
     * Get the PSR factory
79
     *
80
     * @return PsrFactory
81
     */
82
    public function getPsrFactory(): PsrFactory
83
    {
84
        return $this->g(PsrFactory::class);
85
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
86
87
    /**
88
     * Get the PSR request handler
89
     *
90
     * @return PsrRequestHandler
91
     */
92
    public function getPsrRequestHandler(): PsrRequestHandler
93
    {
94
        return $this->g(PsrRequestHandler::class);
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
96
97
    /**
98
     * Get the PSR config middleware
99
     *
100
     * @param string $sConfigFile
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
101
     *
102
     * @return PsrConfigMiddleware
103
     */
104
    public function getPsrConfigMiddleware(string $sConfigFile): PsrConfigMiddleware
105
    {
106
        $this->val($this->sPsrConfig, $sConfigFile);
0 ignored issues
show
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

106
        $this->/** @scrutinizer ignore-call */ 
107
               val($this->sPsrConfig, $sConfigFile);
Loading history...
107
        return $this->g(PsrConfigMiddleware::class);
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Get the PSR ajax request middleware
112
     *
113
     * @return PsrAjaxMiddleware
114
     */
115
    public function getPsrAjaxMiddleware(): PsrAjaxMiddleware
116
    {
117
        return $this->g(PsrAjaxMiddleware::class);
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
119
}
120