I am building my first Laravel web app and I have created a class to handle API requests for a model called Quote. In what ways can the code be improved or reduced?
<?php
namespace App\Http\Controllers;
use Illuminate\Http;
use Illuminate\Http\Request;
use App\SuperQuote\Transformers\QuoteTransformer;
use App\SuperQuote\Transformers\CustomValidation;
use App\SuperQuote\ClassHelpers\ArrayHelper;
use App\SuperQuote\Repositories\RepositoryInterface;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
class AppQuoteDatabaseController extends ApiController
{
/**
* @var App\SuperQuote\Transformers\QuoteTransformer
*
*/
protected $quoteTransformer;
/**
* @var App\SuperQuote\Transformers\CustomValidation
*
*/
protected $customValidation;
/**
* @var App\SuperQuote\Classhelper\ArrayHelper
*
*/
protected $arrayHelper;
/**
* @var app\SuperQuote\Repositories\RepositoryInterface;
*
*/
protected $appQuoteDatabase;
function __construct(QuoteTransformer $quoteTransformer, CustomValidation $customValidation,ArrayHelper $arrayHelper,RepositoryInterface $appQuoteDatabase)
{
$this->quoteTransformer = $quoteTransformer;
$this->customValidation = $customValidation;
$this->arrayHelper = $arrayHelper;
$this->appQuoteDatabase = $appQuoteDatabase;
}
/**
* @return Http\JsonResponse
*/
public function index()
{
$quotes=$this->appQuoteDatabase->getAll();
if ($quotes != null) {
return $this->successfullRequest($this->quoteTransformer->transformCollection($quotes->toArray()));
} else {
return $this->internalError();
}
}
/**
* @param $name
* @return Http\JsonResponse
*/
public function GetQuoteByName($name)
{
$quotes=$this->appQuoteDatabase->findbyName($name);
if (count($quotes) > 0)
{
return $this->successfullRequest($this->quoteTransformer->transformCollection($quotes->toArray()));
}
else
{
return $this->respondNotFound('database does not contain that author, You can always add their information to the database');
}
}
/**
* @return Http\JsonResponse
*/
public function edit()
{
if (array_key_exists('id', $_GET) &&( array_key_exists('quote', $_GET) || array_key_exists('authorName', $_GET) || array_key_exists('category', $_GET) ))
{
$quote=$this->appQuoteDatabase->find($_GET['id']);
if($quote != null)
{
$this->arrayHelper->updateField($quote,'category',$this->customValidation);
$this->arrayHelper->updateField($quote,'author',$this->customValidation);
$this->arrayHelper->updateField($quote,'quote',$this->customValidation);
$this->arrayHelper->updateField($quote,'quoteOrigin',$this->customValidation);
$this->arrayHelper->updateField($quote,'dateOrigin',$this->customValidation);
$quote->save();
return $this->successfullRequest($this->quoteTransformer->transform($quote));
}
}
return $this->respondNotFound('Check values that were passed');
}
/**
* @return Http\JsonResponse
* @internal param Request $request
* @internal param $date
*/
public function findQuotesBetweenDate()
{
$validator = Validator::make(
array(
'startDate' => Input::get( 'startDate' ),
'endDate' => Input::get( 'endDate' )
),
array(
'startDate' => array( 'required','date' ),
'endDate' => array( 'required','date' ),
)
);
if ($validator->fails())
{
$startDateCheck = $this->customValidation->IsNullOrEmptyString($_GET['startDate']);
$endDateCheck = $this->customValidation->IsNullOrEmptyString($_GET['endDate']);
if ($endDateCheck === false && $startDateCheck === false)
{
$quotes=$this->appQuoteDatabase->findbyDates($_GET['startDate'], $_GET['endDate'] );
if (count($quotes) > 0)
{
return $this->successfullRequest($this->quoteTransformer->transformCollection($quotes->toArray()));
}
else
{
return $this->respondNotFound('database does not contain information related to that date');
}
}
}
return $this->respondNotFound($validator->messages());
}
/**
* @param Request $request
* @return Http\JsonResponse
*/
public function store(Request $request)
{
$validator = Validator::make(
array(
'authorName' => Input::get( 'authorName' ),
'quote' => Input::get( 'quote' ),
'category' => Input::get( 'category' )
),
array(
'name' => array( 'required' ),
'quote' => array( 'required' ),
'category' => array( 'required' )
)
);
if ($validator->fails())
{
$authorNameValidation = $this->customValidation->IsNullOrEmptyString($request->input('authorName'));
$quoteValidation = $this->customValidation->IsNullOrEmptyString($request->input('quote'));
$categoryValidation = $this->customValidation->IsNullOrEmptyString($request->input('category'));
if ($authorNameValidation === false && $quoteValidation === false && $categoryValidation === false) {
$values = array();
$values['authorName'] = $request->input('authorName');
$values['quote']= $request->input('quote');
$values['category'] = $request->input('category');
$values->save();
if ($this->appQuoteDatabase->insert($values) > 0) {
return $this->successfullRequestWithMessage(true);
} else {
return $this->internalError();
}
}
}
return $this->respondNotFound($validator->messages());
}
/**
* @param $value
* @return response()->json
**/
public function show($value)
{
$itemFound=$this->appQuoteDatabase->find($value);
if ($itemFound != null) {
return $this->successfullRequest($this->quoteTransformer->transform($itemFound));
} else {
return $this->respondNotFound('Quote Not Found');
}
}
}
<?php namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class AppQuoteDatabase extends Model {
protected $table = 'app_quote_database';
}