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();
}
$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,

View file

@ -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');
}

View file

@ -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);
}

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Note;
@ -14,53 +16,38 @@ class NoteService
/**
* Create a new note.
*
* @param \Illuminate\Http\Request $request
* @param string $clientId
* @param array $data
* @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' => $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
//thats the apps 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;
}

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Place;
@ -11,37 +13,26 @@ class PlaceService
/**
* Create a place.
*
* @param \Illuminate\Http\Request $request
* @param array $data
* @return \App\Place
*/
public function createPlace(Request $request)
public function createPlace(array $data): Place
{
if ($request->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;