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

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