Reduce sonar complexity (#992)

This commit is contained in:
Alexis Saettler
2018-03-17 11:12:49 +01:00
committed by GitHub
parent edd3b43eec
commit 9d2c787b58
20 changed files with 811 additions and 940 deletions
@@ -54,31 +54,9 @@ class ApiActivityController extends ApiController
*/
public function store(Request $request)
{
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'summary' => 'required|max:100000',
'description' => 'required|max:1000000',
'date_it_happened' => 'required|date',
'activity_type_id' => 'integer',
'contacts' => 'required|array',
]);
if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}
// Make sure each contact exists and has the right to be associated with
// this account
$attendeesID = $request->get('contacts');
foreach ($attendeesID as $attendeeID) {
try {
$contact = Contact::where('account_id', auth()->user()->account_id)
->where('id', $attendeeID)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
$contact = $this->validateUpdate($request);
if (! $contact instanceof Contact) {
return $contact;
}
try {
@@ -126,31 +104,9 @@ class ApiActivityController extends ApiController
return $this->respondNotFound();
}
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'summary' => 'required|max:100000',
'description' => 'required|max:1000000',
'date_it_happened' => 'required|date',
'activity_type_id' => 'integer',
'contacts' => 'required|array',
]);
if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}
// Make sure each contact exists and has the right to be associated with
// this account
$attendeesID = $request->get('contacts');
foreach ($attendeesID as $attendeeID) {
try {
$contact = Contact::where('account_id', auth()->user()->account_id)
->where('id', $attendeeID)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
$isvalid = $this->validateUpdate($request);
if ($isvalid !== true) {
return $isvalid;
}
// Update the activity itself
@@ -206,6 +162,44 @@ class ApiActivityController extends ApiController
return new ActivityResource($activity);
}
/**
* Validate the request for update.
*
* @param Request $request
* @return mixed
*/
private function validateUpdate(Request $request)
{
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'summary' => 'required|max:100000',
'description' => 'required|max:1000000',
'date_it_happened' => 'required|date',
'activity_type_id' => 'integer',
'contacts' => 'required|array',
]);
if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}
// Make sure each contact exists and has the right to be associated with
// this account
$attendeesID = $request->get('contacts');
foreach ($attendeesID as $attendeeID) {
try {
$contact = Contact::where('account_id', auth()->user()->account_id)
->where('id', $attendeeID)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
}
return true;
}
/**
* Delete an activity.
* @param Request $request