Completed
Push — master ( 5b7eeb...45b4b0 )
by claudio
04:42
created

BaseMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace plunner\Http\Middleware;
4
5
use Tymon\JWTAuth\JWTAuth;
6
use Illuminate\Events\Dispatcher;
7
use Illuminate\Routing\ResponseFactory;
8
9
abstract class BaseMiddleware
10
{
11
    /**
12
     * @var \Illuminate\Routing\ResponseFactory
13
     */
14
    protected $response;
15
16
    /**
17
     * @var \Illuminate\Events\Dispatcher
18
     */
19
    protected $events;
20
21
    /**
22
     * @var \Tymon\JWTAuth\JWTAuth
23
     */
24
    protected $auth;
25
26
    /**
27
     * Create a new BaseMiddleware instance
28
     *
29
     * @param \Illuminate\Routing\ResponseFactory  $response
30
     * @param \Illuminate\Events\Dispatcher  $events
31
     * @param \Tymon\JWTAuth\JWTAuth  $auth
32
     */
33 2
    public function __construct(ResponseFactory $response, Dispatcher $events, JWTAuth $auth)
34
    {
35 2
        $this->response = $response;
36 2
        $this->events = $events;
37 2
        $this->auth = $auth;
38 2
    }
39
40
    /**
41
     * Fire event and return the response
42
     *
43
     * @param  string   $event
44
     * @param  string   $error
45
     * @param  integer  $status
46
     * @param  array    $payload
47
     * @return mixed
48
     */
49 1
    protected function respond($event, $error, $status, $payload = [])
50
    {
51 1
        $response = $this->events->fire($event, $payload, true);
52
53 1
        return $response ?: $this->response->json(['error' => $error], $status);
54
    }
55
56
57
    /**
58
     * Convert to array a string passed as argument of a middleware in this way key1-ele1;key2-ele2
59
     * @param String $str
60
     * @return array
61
     */
62 2
    protected function convertToArray($str){
63 2
        $ret = [];
64 2
        $str = explode(';', $str);
65 2
        foreach($str as $value) {
66 2
            $tmp = explode('-', $value);
67 2
            if(count($tmp) != 2)
68 2
                return [];
69 2
            $ret[$tmp[0]] = $tmp[1];
70 2
        }
71 2
        return $ret;
72
    }
73
}
74