|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Controllers; |
| 6 | + |
| 7 | +use App\Generators\CatGenerator; |
| 8 | +use App\Generators\DogeGenerator; |
| 9 | +use App\Generators\GeneratorInterface; |
| 10 | +use App\Generators\MultiGenerator; |
| 11 | +use App\Generators\ValidatingGenerator; |
| 12 | +use Illuminate\Contracts\Container\Container; |
| 13 | +use Illuminate\Contracts\View\Factory; |
| 14 | +use Illuminate\Http\JsonResponse; |
| 15 | +use Illuminate\Http\Request; |
| 16 | +use Illuminate\Http\Response; |
| 17 | +use Laravel\Lumen\Routing\Controller; |
| 18 | + |
| 19 | +/** |
| 20 | + * This is the main controller class. |
| 21 | + * |
| 22 | + * @author Graham Campbell <[email protected]> |
| 23 | + */ |
| 24 | +class MainController extends Controller |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Show the welcome message. |
| 28 | + * |
| 29 | + * @return \Illuminate\Http\JsonResponse |
| 30 | + */ |
| 31 | + public function show() |
| 32 | + { |
| 33 | + return new JsonResponse([ |
| 34 | + 'meta' => ['message' => 'Very image. Much server.'], |
| 35 | + ]); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Generate cat memes. |
| 40 | + * |
| 41 | + * @param \Illuminate\Contracts\Container\Container $container |
| 42 | + * @param \Illuminate\Http\Request $request |
| 43 | + * |
| 44 | + * @return \Illuminate\Http\JsonResponse |
| 45 | + */ |
| 46 | + public function cat(Container $container, Request $request) |
| 47 | + { |
| 48 | + $inner = $container->make(CatGenerator::class); |
| 49 | + $text = $request->get('text'); |
| 50 | + $quantity = $request->get('quantity', 1); |
| 51 | + |
| 52 | + return $this->generate($inner, (string) $text, (int) $quantity); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Generate dodge memes. |
| 57 | + * |
| 58 | + * @param \Illuminate\Contracts\Container\Container $container |
| 59 | + * @param \Illuminate\Http\Request $request |
| 60 | + * |
| 61 | + * @return \Illuminate\Http\JsonResponse |
| 62 | + */ |
| 63 | + public function dodge(Container $container, Request $request) |
| 64 | + { |
| 65 | + $inner = $container->make(DogeGenerator::class); |
| 66 | + $text = $request->get('text'); |
| 67 | + $quantity = $request->get('quantity', 1); |
| 68 | + |
| 69 | + return $this->generate($inner, (string) $text, (int) $quantity); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Generate the memes. |
| 74 | + * |
| 75 | + * @param \App\Generators\GeneratorInterface $inner |
| 76 | + * @param string $text |
| 77 | + * @param int $quantity |
| 78 | + * |
| 79 | + * @return \Illuminate\Http\JsonResponse |
| 80 | + */ |
| 81 | + protected function generate(GeneratorInterface $inner, string $text, int $quantity = 1) |
| 82 | + { |
| 83 | + $generator = new ValidatingGenerator(new MultiGenerator($inner, $quantity)); |
| 84 | + |
| 85 | + $images = []; |
| 86 | + |
| 87 | + foreach ($generator->start($text)->wait() as $image) { |
| 88 | + $images[] = "https://api.memetrash.co.uk/result/{$image}.jpg"; |
| 89 | + } |
| 90 | + |
| 91 | + return new JsonResponse([ |
| 92 | + 'success' => ['message' => 'Here are your memes!'], |
| 93 | + 'data' => ['images' => $images], |
| 94 | + ]); |
| 95 | + } |
| 96 | +} |
0 commit comments