From 4f57905bcc47dd8bbab4cc8cab1db9f07ba54fd3 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 1 Mar 2017 20:15:35 +0000 Subject: [PATCH] Refactor to deal with request and extract data to pass to service classes --- .../Controllers/Admin/NotesController.php | 14 +++- .../Controllers/Admin/PlacesController.php | 15 +++- app/Http/Controllers/MicropubController.php | 60 +++++++++++++++- app/Services/NoteService.php | 68 +++++-------------- app/Services/PlaceService.php | 35 ++++------ 5 files changed, 115 insertions(+), 77 deletions(-) diff --git a/app/Http/Controllers/Admin/NotesController.php b/app/Http/Controllers/Admin/NotesController.php index 0ddbd977..bc5ca5b7 100644 --- a/app/Http/Controllers/Admin/NotesController.php +++ b/app/Http/Controllers/Admin/NotesController.php @@ -87,7 +87,19 @@ class NotesAdminController extends Controller ->withInput(); } - $note = $this->noteService->createNote($request); + $data = []; + $data['content'] = $request->input('content'); + $data['in-reply-to'] = $request->input('in-reply-to'); + $data['location'] = $request->input('location'); + $data['syndicate'] = []; + if ($request->input('twitter')) { + $data['syndicate'][] = 'twitter'; + } + if ($request->input('facebook')) { + $data['syndicate'][] = 'facebook'; + } + + $note = $this->noteService->createNote($data); return view('admin.newnotesuccess', [ 'id' => $note->id, diff --git a/app/Http/Controllers/Admin/PlacesController.php b/app/Http/Controllers/Admin/PlacesController.php index 0a93ad81..0513b9a5 100644 --- a/app/Http/Controllers/Admin/PlacesController.php +++ b/app/Http/Controllers/Admin/PlacesController.php @@ -4,11 +4,19 @@ namespace App\Http\Controllers\Admin; use App\Place; use Illuminate\Http\Request; +use App\Services\PlaceService; use App\Http\Controllers\Controller; use Phaza\LaravelPostgis\Geometries\Point; class PlacesAdminController extends Controller { + protected $placeService; + + public function __construct(PlaceService $placeService = null) + { + $this->placeService = $placeService ?? new PlaceService(); + } + /** * List the places that can be edited. * @@ -61,7 +69,12 @@ class PlacesAdminController extends Controller */ public function store(Request $request) { - $this->placeService->createPlace($request); + $data = []; + $data['name'] = $request->name; + $data['description'] = $request->description; + $data['latitude'] = $request->latitude; + $data['longitude'] = $request->longitude; + $place = $this->placeService->createPlace($data); return view('admin.newplacesuccess'); } diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index e934970e..a110bc65 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -57,8 +57,46 @@ class MicropubController extends Controller if (array_search('post', $scopes) !== false) { $clientId = $tokenData->getClaim('client_id'); if (($request->input('h') == 'entry') || ($request->input('type')[0] == 'h-entry')) { + $data = []; + $data['client-id'] = $clientId; + if ($request->header('Content-Type') == 'application/json') { + $data['content'] = $request->input('properties.content')[0]; + $data['in-reply-to'] = $request->input('properties.in-reply-to')[0]; + $data['location'] = $request->input('properties.location'); + //flatten location if array + if (is_array($data['location'])) { + $data['location'] = $data['location'][0]; + } + } else { + $data['content'] = $request->input('content'); + $data['in-reply-to'] = $request->input('in-reply-to'); + $data['location'] = $request->input('location'); + } + $data['syndicate'] = []; + $targets = array_pluck(config('syndication.targets'), 'uid', 'service.name'); + if (is_string($request->input('mp-syndicate-to'))) { + $service = array_search($request->input('mp-syndicate-to')); + if ($service == 'Twitter') { + $data['syndicate'][] = 'twitter'; + } + if ($service == 'Facebook') { + $data['syndicate'][] = 'facebook'; + } + } + if (is_array($request->input('mp-syndicate-to'))) { + foreach ($targets as $service => $target) { + if (in_array($target, $request->input('mp-syndicate-to'))) { + if ($service == 'Twitter') { + $data['syndicate'][] = 'twitter'; + } + if ($service == 'Facebook') { + $data['syndicate'][] = 'facebook'; + } + } + } + } try { - $note = $this->noteService->createNote($request, $clientId); + $note = $this->noteService->createNote($data); } catch (Exception $exception) { return response()->json(['error' => true], 400); } @@ -69,8 +107,26 @@ class MicropubController extends Controller ], 201)->header('Location', $note->longurl); } if ($request->input('h') == 'card' || $request->input('type')[0] == 'h-card') { + $data = []; + if ($request->header('Content-Type') == 'application/json') { + $data['name'] = $request->input('properties.name'); + $data['description'] = $request->input('properties.description') ?? null; + if ($request->has('properties.geo')) { + $data['geo'] = $request->input('properties.geo'); + } + } else { + $data['name'] = $request->input('name'); + $data['description'] = $request->input('description'); + if ($request->has('geo')) { + $data['geo'] = $request->input('geo'); + } + if ($request->has('latitude')) { + $data['latitude'] = $request->input('latitude'); + $data['longitude'] = $request->input('longitude'); + } + } try { - $place = $this->placeService->createPlace($request); + $place = $this->placeService->createPlace($data); } catch (Exception $exception) { return response()->json(['error' => true], 400); } diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index 80e5cdb1..9da3fcc8 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.php @@ -1,5 +1,7 @@ header('Content-Type') == 'application/json') { - $content = $request->input('properties.content')[0]; - $inReplyTo = $request->input('properties.in-reply-to')[0]; - $location = $request->input('properties.location'); - if (is_array($location)) { - $location = $location[0]; - } - } else { - $content = $request->input('content'); - $inReplyTo = $request->input('in-reply-to'); - $location = $request->input('location'); - } - $note = Note::create( [ - 'note' => $content, - 'in_reply_to' => $inReplyTo, - 'client_id' => $clientId, + 'note' => $data['content'], + 'in_reply_to' => $data['in-reply-to'], + 'client_id' => $data['client-id'], ] ); - if ($location !== null && $location !== 'no-location') { - if (substr($location, 0, strlen(config('app.url'))) == config('app.url')) { + if (array_key_exists('location', $data) && $data['location'] !== null && $data['location'] !== 'no-location') { + if (substr($data['location'], 0, strlen(config('app.url'))) == config('app.url')) { //uri of form http://host/places/slug, we want slug so chop off start //that’s the app’s url plus `/places/` $slug = mb_substr($location, mb_strlen(config('app.url')) + 8); $place = Place::where('slug', '=', $slug)->first(); $note->place()->associate($place); - $note->save(); } - if (substr($location, 0, 4) == 'geo:') { + if (substr($data['location'], 0, 4) == 'geo:') { preg_match_all( '/([0-9\.\-]+)/', - $location, + $data['location'], $matches ); $note->location = $matches[0][0] . ', ' . $matches[0][1]; - $note->save(); } } + /* drop image support for now //add images to media library if ($request->hasFile('photo')) { $files = $request->file('photo'); @@ -68,40 +55,19 @@ class NoteService $note->addMedia($file)->toCollectionOnDisk('images', 's3'); } } + */ + + $note->save(); dispatch(new SendWebMentions($note)); //syndication targets - //from admin CP - if ($request->input('twitter')) { + if (in_array('twitter', $data['syndicate'])) { dispatch(new SyndicateToTwitter($note)); } - if ($request->input('facebook')) { + if (in_arraY('facebook', $data['syndicate'])) { dispatch(new SyndicateToFacebook($note)); } - //from a micropub request - $targets = array_pluck(config('syndication.targets'), 'uid', 'service.name'); - if (is_string($request->input('mp-syndicate-to'))) { - $service = array_search($request->input('mp-syndicate-to')); - if ($service == 'Twitter') { - dispatch(new SyndicateToTwitter($note)); - } - if ($service == 'Facebook') { - dispatch(new SyndicateToFacebook($note)); - } - } - if (is_array($request->input('mp-syndicate-to'))) { - foreach ($targets as $service => $target) { - if (in_array($target, $request->input('mp-syndicate-to'))) { - if ($service == 'Twitter') { - dispatch(new SyndicateToTwitter($note)); - } - if ($service == 'Facebook') { - dispatch(new SyndicateToFacebook($note)); - } - } - } - } return $note; } diff --git a/app/Services/PlaceService.php b/app/Services/PlaceService.php index 602e097b..1da83c15 100644 --- a/app/Services/PlaceService.php +++ b/app/Services/PlaceService.php @@ -1,5 +1,7 @@ header('Content-Type') == 'application/json') { - $name = $request->input('properties.name'); - $description = $request->input('properties.description') ?? null; - $geo = $request->input('properties.geo'); - } else { - $name = $request->input('name'); - $description = $request->input('description'); - $geo = $request->input('geo'); - } - if ($geo) { + //obviously a place needs a lat/lng, but this could be sent in a geo-url + //if no geo array key, we assume the array already has lat/lng values + if (array_key_exists('geo', $data)) { preg_match_all( '/([0-9\.\-]+)/', - $geo, + $data['geo'], $matches ); - $latitude = $matches[0][0]; - $longitude = $matches[0][1]; - } - if ($request->input('latitude') !== null) { - $latitude = $request->input('latitude'); - $longitude = $request->input('longitude'); + $data['latitude'] = $matches[0][0]; + $data['longitude'] = $matches[0][1]; } $place = new Place(); - $place->name = $name; - $place->description = $description; - $place->location = new Point((float) $latitude, (float) $longitude); + $place->name = $data['name']; + $place->description = $data['description']; + $place->location = new Point((float) $data['latitude'], (float) $data['longitude']); $place->save(); return $place;