Refactor to deal with request and extract data to pass to service classes

This commit is contained in:
Jonny Barnes 2017-03-01 20:15:35 +00:00
parent 8ac32f5783
commit 4f57905bcc
5 changed files with 115 additions and 77 deletions

View file

@ -87,7 +87,19 @@ class NotesAdminController extends Controller
->withInput(); ->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', [ return view('admin.newnotesuccess', [
'id' => $note->id, 'id' => $note->id,

View file

@ -4,11 +4,19 @@ namespace App\Http\Controllers\Admin;
use App\Place; use App\Place;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Services\PlaceService;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Phaza\LaravelPostgis\Geometries\Point; use Phaza\LaravelPostgis\Geometries\Point;
class PlacesAdminController extends Controller class PlacesAdminController extends Controller
{ {
protected $placeService;
public function __construct(PlaceService $placeService = null)
{
$this->placeService = $placeService ?? new PlaceService();
}
/** /**
* List the places that can be edited. * List the places that can be edited.
* *
@ -61,7 +69,12 @@ class PlacesAdminController extends Controller
*/ */
public function store(Request $request) 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'); return view('admin.newplacesuccess');
} }

View file

@ -57,8 +57,46 @@ class MicropubController extends Controller
if (array_search('post', $scopes) !== false) { if (array_search('post', $scopes) !== false) {
$clientId = $tokenData->getClaim('client_id'); $clientId = $tokenData->getClaim('client_id');
if (($request->input('h') == 'entry') || ($request->input('type')[0] == 'h-entry')) { 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 { try {
$note = $this->noteService->createNote($request, $clientId); $note = $this->noteService->createNote($data);
} catch (Exception $exception) { } catch (Exception $exception) {
return response()->json(['error' => true], 400); return response()->json(['error' => true], 400);
} }
@ -69,8 +107,26 @@ class MicropubController extends Controller
], 201)->header('Location', $note->longurl); ], 201)->header('Location', $note->longurl);
} }
if ($request->input('h') == 'card' || $request->input('type')[0] == 'h-card') { 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 { try {
$place = $this->placeService->createPlace($request); $place = $this->placeService->createPlace($data);
} catch (Exception $exception) { } catch (Exception $exception) {
return response()->json(['error' => true], 400); return response()->json(['error' => true], 400);
} }

View file

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace App\Services; namespace App\Services;
use App\Note; use App\Note;
@ -14,53 +16,38 @@ class NoteService
/** /**
* Create a new note. * Create a new note.
* *
* @param \Illuminate\Http\Request $request * @param array $data
* @param string $clientId
* @return \App\Note $note * @return \App\Note $note
*/ */
public function createNote(Request $request, $clientId = null) public function createNote(array $data): Note
{ {
if ($request->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 = Note::create(
[ [
'note' => $content, 'note' => $data['content'],
'in_reply_to' => $inReplyTo, 'in_reply_to' => $data['in-reply-to'],
'client_id' => $clientId, 'client_id' => $data['client-id'],
] ]
); );
if ($location !== null && $location !== 'no-location') { if (array_key_exists('location', $data) && $data['location'] !== null && $data['location'] !== 'no-location') {
if (substr($location, 0, strlen(config('app.url'))) == config('app.url')) { 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 //uri of form http://host/places/slug, we want slug so chop off start
//thats the apps url plus `/places/` //thats the apps url plus `/places/`
$slug = mb_substr($location, mb_strlen(config('app.url')) + 8); $slug = mb_substr($location, mb_strlen(config('app.url')) + 8);
$place = Place::where('slug', '=', $slug)->first(); $place = Place::where('slug', '=', $slug)->first();
$note->place()->associate($place); $note->place()->associate($place);
$note->save();
} }
if (substr($location, 0, 4) == 'geo:') { if (substr($data['location'], 0, 4) == 'geo:') {
preg_match_all( preg_match_all(
'/([0-9\.\-]+)/', '/([0-9\.\-]+)/',
$location, $data['location'],
$matches $matches
); );
$note->location = $matches[0][0] . ', ' . $matches[0][1]; $note->location = $matches[0][0] . ', ' . $matches[0][1];
$note->save();
} }
} }
/* drop image support for now
//add images to media library //add images to media library
if ($request->hasFile('photo')) { if ($request->hasFile('photo')) {
$files = $request->file('photo'); $files = $request->file('photo');
@ -68,40 +55,19 @@ class NoteService
$note->addMedia($file)->toCollectionOnDisk('images', 's3'); $note->addMedia($file)->toCollectionOnDisk('images', 's3');
} }
} }
*/
$note->save();
dispatch(new SendWebMentions($note)); dispatch(new SendWebMentions($note));
//syndication targets //syndication targets
//from admin CP if (in_array('twitter', $data['syndicate'])) {
if ($request->input('twitter')) {
dispatch(new SyndicateToTwitter($note)); dispatch(new SyndicateToTwitter($note));
} }
if ($request->input('facebook')) { if (in_arraY('facebook', $data['syndicate'])) {
dispatch(new SyndicateToFacebook($note)); 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; return $note;
} }

View file

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace App\Services; namespace App\Services;
use App\Place; use App\Place;
@ -11,37 +13,26 @@ class PlaceService
/** /**
* Create a place. * Create a place.
* *
* @param \Illuminate\Http\Request $request * @param array $data
* @return \App\Place * @return \App\Place
*/ */
public function createPlace(Request $request) public function createPlace(array $data): Place
{ {
if ($request->header('Content-Type') == 'application/json') { //obviously a place needs a lat/lng, but this could be sent in a geo-url
$name = $request->input('properties.name'); //if no geo array key, we assume the array already has lat/lng values
$description = $request->input('properties.description') ?? null; if (array_key_exists('geo', $data)) {
$geo = $request->input('properties.geo');
} else {
$name = $request->input('name');
$description = $request->input('description');
$geo = $request->input('geo');
}
if ($geo) {
preg_match_all( preg_match_all(
'/([0-9\.\-]+)/', '/([0-9\.\-]+)/',
$geo, $data['geo'],
$matches $matches
); );
$latitude = $matches[0][0]; $data['latitude'] = $matches[0][0];
$longitude = $matches[0][1]; $data['longitude'] = $matches[0][1];
}
if ($request->input('latitude') !== null) {
$latitude = $request->input('latitude');
$longitude = $request->input('longitude');
} }
$place = new Place(); $place = new Place();
$place->name = $name; $place->name = $data['name'];
$place->description = $description; $place->description = $data['description'];
$place->location = new Point((float) $latitude, (float) $longitude); $place->location = new Point((float) $data['latitude'], (float) $data['longitude']);
$place->save(); $place->save();
return $place; return $place;