andela-tolotin /
Emoji-REST-API
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @author Temitope Olotin <[email protected]> |
||
| 4 | * @license <https://opensource.org/license/MIT> MIT |
||
| 5 | */ |
||
| 6 | require 'vendor/autoload.php'; |
||
| 7 | |||
| 8 | use \Psr\Http\Message\ResponseInterface as Response; |
||
|
0 ignored issues
–
show
|
|||
| 9 | use \Psr\Http\Message\ServerRequestInterface as Request; |
||
| 10 | |||
| 11 | use Illuminate\Database\Capsule\Manager as Capsule; |
||
| 12 | use Laztopaz\EmojiRestfulAPI\DatabaseConnection; |
||
| 13 | use Laztopaz\EmojiRestfulAPI\EmojiController; |
||
| 14 | use Laztopaz\EmojiRestfulAPI\Middleware; |
||
| 15 | use Laztopaz\EmojiRestfulAPI\Oauth; |
||
| 16 | |||
| 17 | $app = new Slim\App([ |
||
| 18 | 'settings' => [ |
||
| 19 | 'debug' => true, |
||
| 20 | 'displayErrorDetails' => true, |
||
| 21 | ], |
||
| 22 | ]); |
||
| 23 | |||
| 24 | $capsule = new Capsule(); |
||
| 25 | |||
| 26 | new DatabaseConnection($capsule); |
||
| 27 | |||
| 28 | $auth = new Oauth(); |
||
| 29 | |||
| 30 | $emoji = new EmojiController($auth); |
||
| 31 | |||
| 32 | |||
| 33 | /* |
||
| 34 | * This verb returns error 404 |
||
| 35 | * |
||
| 36 | * @param $request |
||
| 37 | * |
||
| 38 | * @param $response |
||
| 39 | * |
||
| 40 | * @return json $response |
||
| 41 | * |
||
| 42 | */ |
||
| 43 | $app->get('/', function (Request $request, Response $response) use ($auth) { |
||
| 44 | return $response->withJson(['status'], 404); |
||
| 45 | |||
| 46 | }); |
||
| 47 | |||
| 48 | /* |
||
| 49 | * This verb returns error 404 |
||
| 50 | * |
||
| 51 | * @param $request |
||
| 52 | * |
||
| 53 | * @param $response |
||
| 54 | * |
||
| 55 | * @return json $response |
||
| 56 | * |
||
| 57 | */ |
||
| 58 | $app->post('/', function (Request $request, Response $response) { |
||
| 59 | return $response->withJson(['status'], 404); |
||
| 60 | |||
| 61 | }); |
||
| 62 | |||
| 63 | /* |
||
| 64 | * This endpoint authenticate the user |
||
| 65 | * |
||
| 66 | * @param $request |
||
| 67 | * |
||
| 68 | * @param $response |
||
| 69 | * |
||
| 70 | * @return json $response |
||
| 71 | * |
||
| 72 | */ |
||
| 73 | $app->post('/auth/login', function (Request $request, Response $response) use ($auth) { |
||
| 74 | return $auth->loginUser($request, $response); |
||
| 75 | |||
| 76 | }); |
||
| 77 | |||
| 78 | /* |
||
| 79 | * This endpoint authenticate the user |
||
| 80 | * |
||
| 81 | * @param $request |
||
| 82 | * |
||
| 83 | * @param $response |
||
| 84 | * |
||
| 85 | * @param $args |
||
| 86 | * |
||
| 87 | * @return json $response |
||
| 88 | * |
||
| 89 | */ |
||
| 90 | |||
| 91 | $app->get('/auth/logout', function (Request $request, Response $response, $args) use ($auth) { |
||
| 92 | return $auth->logoutUser($request, $response, $args)->withJson(['status'], 200); |
||
| 93 | |||
| 94 | })->add(new Middleware()); |
||
| 95 | |||
| 96 | /* |
||
| 97 | * This verb returns all emoji |
||
| 98 | * |
||
| 99 | * @param $request |
||
| 100 | * |
||
| 101 | * @param $response |
||
| 102 | * |
||
| 103 | * @return json $response |
||
| 104 | * |
||
| 105 | */ |
||
| 106 | $app->get('/emojis', function (Request $request, Response $response) use ($emoji) { |
||
| 107 | return $emoji->listAllEmoji($response); |
||
| 108 | |||
| 109 | }); |
||
| 110 | |||
| 111 | /* |
||
| 112 | * This verb returns a single emoji |
||
| 113 | * |
||
| 114 | * @param $response |
||
| 115 | * |
||
| 116 | * @param $args |
||
| 117 | * |
||
| 118 | * @return json $response |
||
| 119 | * |
||
| 120 | */ |
||
| 121 | $app->get('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) { |
||
| 122 | return $emoji->getSingleEmoji($response, $args); |
||
| 123 | |||
| 124 | }); |
||
| 125 | |||
| 126 | /* |
||
| 127 | * This verb creates a new emoji |
||
| 128 | * |
||
| 129 | * @param $request |
||
| 130 | * |
||
| 131 | * @param $response |
||
| 132 | * |
||
| 133 | * @return json $response |
||
| 134 | * |
||
| 135 | */ |
||
| 136 | $app->post('/emojis', function (Request $request, Response $response) use ($emoji) { |
||
| 137 | return $emoji->createEmoji($request, $response); |
||
| 138 | |||
| 139 | })->add(new Middleware()); |
||
| 140 | |||
| 141 | /* |
||
| 142 | * This verb updatess an emoji using put verb |
||
| 143 | * |
||
| 144 | * @param $request |
||
| 145 | * |
||
| 146 | * @param $response |
||
| 147 | * |
||
| 148 | * @param $args |
||
| 149 | * |
||
| 150 | * @param $emoji |
||
| 151 | * |
||
| 152 | * @return json $response |
||
| 153 | * |
||
| 154 | */ |
||
| 155 | $app->put('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) { |
||
| 156 | return $emoji->updateEmojiByPutVerb($request, $response, $args); |
||
| 157 | |||
| 158 | })->add(new Middleware()); |
||
| 159 | |||
| 160 | /* |
||
| 161 | * This verb updatess an emoji using put verb |
||
| 162 | * |
||
| 163 | * @param $request |
||
| 164 | * |
||
| 165 | * @param $response |
||
| 166 | * |
||
| 167 | * @param $data |
||
| 168 | * |
||
| 169 | * @return json $response |
||
| 170 | * |
||
| 171 | */ |
||
| 172 | $app->patch('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) { |
||
| 173 | return $emoji->updateEmojiByPatchVerb($request, $response, $args); |
||
| 174 | |||
| 175 | })->add(new Middleware()); |
||
| 176 | |||
| 177 | /* |
||
| 178 | * This verb updatess an emoji using put verb |
||
| 179 | * |
||
| 180 | * @param $request |
||
| 181 | * |
||
| 182 | * @param $response |
||
| 183 | * |
||
| 184 | * @param $args |
||
| 185 | * |
||
| 186 | * @return json $response |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | $app->delete('/emojis/{id}', function (Request $request, Response $response, $args) use ($emoji) { |
||
| 190 | return $emoji->deleteEmoji($request, $response, $args); |
||
| 191 | |||
| 192 | })->add(new Middleware()); |
||
| 193 | |||
| 194 | $app->run(); |
||
| 195 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: