Completed
Push — master ( ab056d...4cd2d7 )
by Temitope
02:24
created

SlimRouteApp::runEmojiRoute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 166
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 166
rs 8.2857
cc 1
eloc 28
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @author   Temitope Olotin <[email protected]>
4
 * @license  <https://opensource.org/license/MIT> MIT
5
 */
6
7
namespace Laztopaz\EmojiRestfulAPI;
8
9
require 'vendor/autoload.php';
10
11
use \Psr\Http\Message\ResponseInterface as Response;
12
use \Psr\Http\Message\ServerRequestInterface as Request;
13
14
use Illuminate\Database\Capsule\Manager as Capsule;
15
use Laztopaz\EmojiRestfulAPI\DatabaseConnection;
16
use Laztopaz\EmojiRestfulAPI\EmojiController;
17
use Laztopaz\EmojiRestfulAPI\Middleware;
18
use Laztopaz\EmojiRestfulAPI\Oauth;
19
20
class SlimRouteApp
21
{
22
    protected $auth;
23
    protected $emoji;
24
    protected $slimApp;
25
26
    public function __construct(Oauth $auth, EmojiController $emoji)
27
    {
28
        $this->auth = $auth;
29
        $this->emoji = $emoji;
30
        $this->slimApp = new \Slim\App([
31
            'settings' => [
32
            'debug' => true,
33
            'displayErrorDetails' => true,
34
            ],]);
35
        
36
        $this->runEmojiRoute();
37
    }
38
39
    public function setUpSlimApp()
40
    {
41
        return $this->slimApp;
42
    }
43
44
    public function runEmojiRoute()
45
    {  
46
        $auth = $this->auth;
47
        $emoji = $this->emoji;
48
49
       /*
50
        * This verb returns error 404
51
        *
52
        * @param $request
53
        *
54
        * @param $response
55
        *
56
        * @return json $response
57
        *
58
        */
59
        $this->slimApp->get('/', function (Request $request, Response $response) {
60
            return $response->withJson(['status'], 404);
61
62
        });
63
64
        /*
65
        * This verb returns error 404
66
        *
67
        * @param $request
68
        *
69
        * @param $response
70
        *
71
        * @return json $response
72
        *
73
        */
74
        $this->slimApp->post('/', function (Request $request, Response $response) {
75
            return $response->withJson(['status'], 404);
76
77
        });
78
79
        /*
80
        * This endpoint authenticate the user
81
        *
82
        * @param $request
83
        *
84
        * @param $response
85
        *
86
        * @return json $response
87
        *
88
        */
89
        $this->slimApp->post('/auth/login', function (Request $request, Response $response) use ($auth) {
90
            return $auth->loginUser($request, $response);
91
92
        });
93
94
        /*
95
        * This endpoint authenticate the user
96
        *
97
        * @param $request
98
        *
99
        * @param $response
100
        *
101
        * @param $args
102
        *
103
        * @return json $response
104
        *
105
        */
106
107
        $this->slimApp->get('/auth/logout', function (Request $request, Response $response, $args) use ($auth) {
108
            return $auth->logoutUser($request, $response, $args)->withJson(['status'], 200);
109
110
        })->add(new Middleware());
111
112
        /*
113
        * This verb returns all emoji
114
        *
115
        * @param $request
116
        *
117
        * @param $response
118
        *
119
        * @return json $response
120
        *
121
        */
122
        $this->slimApp->get('/emojis', function (Request $request, Response $response) use ($emoji) {
123
            return $emoji->listAllEmoji($response);
124
125
        });
126
127
        /*
128
        * This verb returns a single emoji
129
        *
130
        * @param $response
131
        *
132
        * @param $args
133
        *
134
        * @return json $response
135
        *
136
        */
137
        $this->slimApp->get('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) {
138
            return  $emoji->getSingleEmoji($response, $args)->withJson(['status'], 200);
139
140
        });
141
142
        /*
143
        * This verb creates a new  emoji
144
        *
145
        * @param $request
146
        *
147
        * @param $response
148
        *
149
        * @return json $response
150
        *
151
        */
152
        $this->slimApp->post('/emojis', function (Request $request, Response $response) use ($emoji) {
153
            return $emoji->createEmoji($request, $response)->withJson(['status'], 200);
154
155
        })->add(new Middleware());
156
157
        /*
158
        * This verb updatess an emoji using put verb
159
        *
160
        * @param $request
161
        *
162
        * @param $response
163
        *
164
        * @param $args
165
        *
166
        * @param $emoji
167
        *
168
        * @return json $response
169
        *
170
        */
171
        $this->slimApp->put('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) {
172
            return $emoji->updateEmojiByPutVerb($request, $response, $args);
173
174
        })->add(new Middleware());
175
176
        /*
177
        * This verb updatess an emoji using put verb
178
        *
179
        * @param $request
180
        *
181
        * @param $response
182
        *
183
        * @param $data
184
        *
185
        * @return json $response
186
        *
187
        */
188
        $this->slimApp->patch('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) {
189
            return $emoji->updateEmojiByPatchVerb($request, $response, $args);
190
191
        })->add(new Middleware());
192
193
        /*
194
        * This verb updatess an emoji using put verb
195
        *
196
        * @param $request
197
        *
198
        * @param $response
199
        *
200
        * @param $args
201
        *
202
        * @return json $response
203
        *
204
        */
205
        $this->slimApp->delete('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) {
206
            return $emoji->deleteEmoji($request, $response, $args);
207
208
        })->add(new Middleware());
209
    }
210
211
}