|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Legacy\Http\Controllers; |
| 4 | + |
| 5 | +use App\Legacy\Classes\Repositories\OrganisationRepositoryInterface; |
| 6 | +use App\Legacy\Classes\Transformers\OrganisationTransformer; |
| 7 | +use App\Legacy\Models\Organisation; |
| 8 | +use Illuminate\Database\Eloquent\Collection; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Support\Facades\Log; |
| 11 | +use League\Fractal\Manager; |
| 12 | +use League\Fractal\Resource\Collection as FractalCollection; |
| 13 | +use League\Fractal\Resource\Item; |
| 14 | + |
| 15 | +use App\Http\Controllers\Controller; |
| 16 | + |
| 17 | +class OrganisationController extends Controller |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var OrganisationRepositoryInterface |
| 21 | + */ |
| 22 | + protected $orgRepo; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Request |
| 26 | + */ |
| 27 | + protected $request; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Manager |
| 31 | + */ |
| 32 | + protected $manager; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param OrganisationRepositoryInterface $orgRepo |
| 36 | + * @param Request $request |
| 37 | + * @param Manager $manager |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + OrganisationRepositoryInterface $orgRepo, |
| 41 | + Request $request, |
| 42 | + Manager $manager |
| 43 | + ) { |
| 44 | + $this->orgRepo = $orgRepo; |
| 45 | + $this->request = $request; |
| 46 | + $this->manager = $manager; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param Request $request |
| 51 | + * @return \Symfony\Component\HttpFoundation\Response |
| 52 | + */ |
| 53 | + public function getAll(Request $request) |
| 54 | + { |
| 55 | + try { |
| 56 | + /** @var Collection $orgs */ |
| 57 | + $orgs = $this->orgRepo->all(); |
| 58 | + } catch (\Exception $e) { |
| 59 | + Log::error('Could not get Organisations list', ['message' => $e->getMessage()]); |
| 60 | + return response()->json([ |
| 61 | + 'status' => 500, |
| 62 | + 'error_message' => 'Could not get Organisations list', |
| 63 | + 'errors' => [], |
| 64 | + ], 500); |
| 65 | + } |
| 66 | + |
| 67 | + $orgs->each(function (Organisation $org) { |
| 68 | + $org->load('details'); |
| 69 | + }); |
| 70 | + |
| 71 | + $resource = new FractalCollection($orgs, new OrganisationTransformer([ |
| 72 | + 'unpublished' => $request->header('x-api-key') ? false : true, |
| 73 | + ])); |
| 74 | + |
| 75 | + $response = $this->manager->createData($resource); |
| 76 | + return response()->json($response->toArray(), 200); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param string $code |
| 81 | + * @param Request $request |
| 82 | + * @return \Symfony\Component\HttpFoundation\Response |
| 83 | + */ |
| 84 | + public function getById($code, Request $request) |
| 85 | + { |
| 86 | + try { |
| 87 | + /** @var Organisation $org */ |
| 88 | + $org = $this->orgRepo->findByCountryCode($code); |
| 89 | + } catch (\Exception $e) { |
| 90 | + Log::error('Organisation not found', ['message' => $e->getMessage()]); |
| 91 | + return response()->json([ |
| 92 | + 'status' => 404, |
| 93 | + 'error_message' => 'Organisation does not exist', |
| 94 | + 'errors' => ['No matching organisation for country code'], |
| 95 | + ], 404); |
| 96 | + } |
| 97 | + |
| 98 | + $org->load('details'); |
| 99 | + |
| 100 | + $resource = new Item($org, new OrganisationTransformer([ |
| 101 | + 'unpublished' => $request->header('x-api-key') ? false : true, |
| 102 | + ])); |
| 103 | + |
| 104 | + $response = $this->manager->createData($resource); |
| 105 | + return response()->json($response->toArray(), 200); |
| 106 | + } |
| 107 | +} |
| 108 | + |
0 commit comments