Skip to content

Instantly share code, notes, and snippets.

@makoru-hikage
Created March 16, 2017 00:02
Show Gist options
  • Save makoru-hikage/bb141724ac4a3cbe92cbee58435c5980 to your computer and use it in GitHub Desktop.
Save makoru-hikage/bb141724ac4a3cbe92cbee58435c5980 to your computer and use it in GitHub Desktop.
Still needs to be improved...
<?php
namespace DeltaX\Crud\MenuService;
use DeltaX\ContentAccess\ContentAccessInterface;
/**
* MenuService
*/
abstract class MenuService {
/**
* Set of permissions
* @var array
*/
protected $permissions;
/**
* Response: The main content
* of the response body
* @var mixed
*/
protected $outputData;
/**
* Response: The Status Code
* @var int
*/
protected $code = 0;
/**
* Response: The Success Indicator
* @var bool
*/
protected $success = false;
/**
* Response: Response Message
* @var string
*/
protected $message;
/**
* Request: inputData taken from forms
* or JSON request body
* @var array
*/
protected $inputData;
/**
* An object to authenticate an authorized user
* @var DeltaX\ContentAccess\ContentAccessInterface;
*/
protected $authenticator;
/**
* An object to validate input for queries
* @var \DeltaX\Crud\ModelService\ValidatorInterface $validator
*/
protected $validator;
/**
* Only this method, validate(), and
* authenticate() set the following
* properties: $success, $code,
* $message, and $outputData.
*
* Any response bodies resulted
* from database queries are done here
*
* If there are required input data,
* use $inputData property
*
* @return array
*/
abstract protected function runCommand();
/**
* runQuery
* @return self
*/
protected function processQuery(){
if($this->code !== 0){
return $this;
}
$this->runQuery();
return $this;
}
/**
* setInput
* @param array $input
* @return self
*/
public function setInput(array $input) {
$this->inputData = $input;
return $this;
}
/**
* setAuthenticator
* @param \DeltaX\ContentAccess\ContentAccessInterface $authenticator
* @return self
*/
public function setAuthenticator(ContentAccessInterface $authenticator) {
$this->authenticator = $authenticator;
return $this;
}
/**
* setAuthenticator
* @param \DeltaX\Crud\ModelService\ValidatorInterface $validator
* @return self
*/
public function setValidator(ValidatorInterface $validator) {
$this->validator = $validator;
return $this;
}
/**
* authenticate
* @return self
*/
protected function authenticate() {
if($this->code !== 0){
return $this;
}
if ( ! $this->authenticator->authorize($this->permissions)){
$this->code = 401;
$this->message = 'User is not authorized to do action on content';
}
return $this;
}
/**
* validate
* @return self
*/
protected function validate() {
if($this->code !== 0){
return $this;
}
//If validate() yields 'true', all inputs are valid
$validation = $this->validator->validate($this->inputData);
//If validate() returns an array, it means that there are errors
if (is_array($validation)) {
$this->code = 400;
$this->message = 'Some inputs are not valid';
$this->outputData = $validation;
}
return $this;
}
/**
* prepareResponse
* @return array
*/
protected function prepareResponse() {
return [
'success' => $this->success,
'code' => $this->code,
'message' => $this->message,
'data' => $this->outputData
];
}
/**
* execute
* @return mixed
*/
public function execute() {
return $this->authenticate()
->validate()
->prepareInput()
->runQuery()
->prepareResponse();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment