Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions legacy/app/Classes/Transformers/OrganisationTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Legacy\Classes\Transformers;

use League\Fractal\TransformerAbstract;
use App\Legacy\Models\Organisation;

class OrganisationTransformer extends TransformerAbstract
{
/**
* @var bool
*/
private $unpublished = false;

/**
* @param array $configuration
*/
public function __construct($configuration = [])
{
if (isset($configuration['unpublished']) && is_bool($configuration['unpublished'])) {
$this->unpublished = $configuration['unpublished'];
}
}

/**
* Turn this item object into a generic array
*
* @param Organisation $model
* @return array
*/
public function transform(Organisation $model)
{
$response = [
'countryCode' => $model->country_code,
'name' => $model->org_name,
'url' => $model->attribution_url,
'imageUrl' => $model->attribution_file_name ? $model->getAttributionImageUrl() : null,
'translations' => null,
];

if ($model->details->count()) {
$response['translations'] = [];
foreach ($model->details as $detail) {
if ($this->unpublished || $detail->published) {
$response['translations'][$detail->language_code] = [
'languageCode' => $detail->language_code,
'name' => $detail->org_name,
'attributionMessage' => $detail->attribution_message,
'published' => (bool) $detail->published,
];
}
}
}

return $response;
}
}

108 changes: 108 additions & 0 deletions legacy/app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace App\Legacy\Http\Controllers;

use App\Legacy\Classes\Repositories\OrganisationRepositoryInterface;
use App\Legacy\Classes\Transformers\OrganisationTransformer;
use App\Legacy\Models\Organisation;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use League\Fractal\Manager;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;

use App\Http\Controllers\Controller;

class OrganisationController extends Controller
{
/**
* @var OrganisationRepositoryInterface
*/
protected $orgRepo;

/**
* @var Request
*/
protected $request;

/**
* @var Manager
*/
protected $manager;

/**
* @param OrganisationRepositoryInterface $orgRepo
* @param Request $request
* @param Manager $manager
*/
public function __construct(
OrganisationRepositoryInterface $orgRepo,
Request $request,
Manager $manager
) {
$this->orgRepo = $orgRepo;
$this->request = $request;
$this->manager = $manager;
}

/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getAll(Request $request)
{
try {
/** @var Collection $orgs */
$orgs = $this->orgRepo->all();
} catch (\Exception $e) {
Log::error('Could not get Organisations list', ['message' => $e->getMessage()]);
return response()->json([
'status' => 500,
'error_message' => 'Could not get Organisations list',
'errors' => [],
], 500);
}

$orgs->each(function (Organisation $org) {
$org->load('details');
});

$resource = new FractalCollection($orgs, new OrganisationTransformer([
'unpublished' => $request->header('x-api-key') ? false : true,
]));

$response = $this->manager->createData($resource);
return response()->json($response->toArray(), 200);
}

/**
* @param string $code
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getById($code, Request $request)
{
try {
/** @var Organisation $org */
$org = $this->orgRepo->findByCountryCode($code);
} catch (\Exception $e) {
Log::error('Organisation not found', ['message' => $e->getMessage()]);
return response()->json([
'status' => 404,
'error_message' => 'Organisation does not exist',
'errors' => ['No matching organisation for country code'],
], 404);
}

$org->load('details');

$resource = new Item($org, new OrganisationTransformer([
'unpublished' => $request->header('x-api-key') ? false : true,
]));

$response = $this->manager->createData($resource);
return response()->json($response->toArray(), 200);
}
}

3 changes: 2 additions & 1 deletion legacy/app/Models/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Organisation extends Model
'org_name',
'oid',
'attribution_url',
'attribution_file_name'
'attribution_file_name',
'country_code',
];

public function alerts()
Expand Down
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@
});

Route::group(['middleware' => 'ApiAuth', 'prefix' => 'v1'], function () {
Route::get('org/', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getAll');
Route::get('org/{code}/whatnow', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getFeed');
Route::get('org/{code}', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getById');
Route::get('whatnow/{id}', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getPublishedById');

Route::any('{any}', function () {
return response()->json([
Expand Down
Loading