Test Failed
Push — master ( 169a53...e99e0c )
by Php Easy Api
05:00
created

ResponseProvider::getResponseKind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Response;
4
5
use Closure;
6
use Resta\Support\Utils;
7
use Resta\Support\ClosureDispatcher;
8
use Resta\Foundation\ApplicationProvider;
9
10
class ResponseProvider extends ApplicationProvider
11
{
12
    //get response output
13
    use ResponseOutput;
0 ignored issues
show
introduced by
The trait Resta\Response\ResponseOutput requires some properties which are not provided by Resta\Response\ResponseProvider: $controllerWatch, $log, $loggerService
Loading history...
14
15
    /**
16
     * app response type
17
     *
18
     * @return mixed
19
     */
20
    private function appResponseType()
21
    {
22
        //get controller instance
23
        $controllerInstance = $this->getControllerInstance();
24
25
        //If our endpoint is provided without auto service,
26
        //we get the response object from the existing kernel object without resampling our service base class.
27
        //In this case we need to instantiate the service base class of the existing project for
28
        //the auto service to be called.
29
        return ClosureDispatcher::bind($controllerInstance)->call(function() use($controllerInstance){
30
31
            if(property_exists($controllerInstance,'response')){
32
                return $controllerInstance->response;
33
            }
34
35
            // if the client wishes,
36
            // data can be returned in the supported format.
37
            if(app()->has('clientResponseType')){
38
                return app()->get('clientResponseType');
39
            }
40
41
            return config('app.response');
42
        });
43
    }
44
45
    /**
46
     * resolving event fire for response
47
     *
48
     * @param null $event
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $event is correct as it would always require null to be passed?
Loading history...
49
     * @param bool $return
50
     * @return void
51
     */
52
    protected function fireEvent($event=null,$return=false)
53
    {
54
        // if an array of response-events is registered
55
        // in the container system, the event will fire.
56
        if($this->app->has('response-event.'.$event)){
57
            $event = $this->app->get('response-event.'.$event);
58
59
            // the event to be fired must be
60
            // a closure instance.
61
            if($event instanceof Closure){
62
                $eventResolved = $event($this->app);
63
64
                if($return){
65
                    return $eventResolved;
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * formatter
73
     *
74
     * @return \Resta\Config\ConfigProcess
75
     */
76
    public function formatter()
77
    {
78
        //we get and handle the adapter classes in
79
        //the output array according to the base object.
80
        return config('response.outputter.formatter');
81
    }
82
83
    /**
84
     * get controller instance
85
     *
86
     * @return mixed
87
     */
88
    private function getControllerInstance()
89
    {
90
        //we get the instanceController object from the router.
91
        return core()->instanceController;
92
    }
93
94
    /**
95
     * get response kind
96
     *
97
     * @return mixed
98
     */
99
    private function getResponseKind()
100
    {
101
        //we get the response type by checking the instanceController object from the router.
102
        //Each type of response is in the base class in project directory.
103
        return ($this->getControllerInstance()===null) ? core()->responseType :
104
            $this->appResponseType();
105
    }
106
107
    /**
108
     * response application handle
109
     *
110
     * @return mixed
111
     */
112
    public function handle()
113
    {
114
        //definition for singleton instance
115
        define ('responseApp',true);
116
117
        //get out putter for response
118
        $formatter = $this->formatter();
119
120
        //if out putter is not null
121
        if(Utils::isNamespaceExists($formatter)){
122
123
            //fire event before response output
124
            $this->fireEvent('before');
125
126
            //get outputter for result
127
            $outPutter = $this->getOutPutter();
128
129
            //fire event after response output
130
            $this->fireEvent('after');
131
132
            // we resolve the response via the service container
133
            // and run the handle method.
134
            $result = app()->resolve($formatter)->{$this->getResponseKind()}($outPutter);
135
136
            $this->app->register('result',$result);
137
        }
138
    }
139
140
    /**
141
     * output formatter
142
     *
143
     * @param array $data
144
     * @return array
145
     */
146
    public function outputFormatter($data=array(),$outputter='json')
147
    {
148
        $dataCapsule = config('response.data');
149
150
        return  array_merge(
151
            config('response.meta'),
0 ignored issues
show
Bug introduced by
It seems like config('response.meta') can also be of type null; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

151
            /** @scrutinizer ignore-type */ config('response.meta'),
Loading history...
152
            [$dataCapsule=>$data]
153
        );
154
    }
155
}