Fix formatting and interfaces in nearly every file

This commit is contained in:
Nabeel Shahzad
2018-03-19 20:50:40 -05:00
parent 04c5b9e7bf
commit ccf56ddec1
331 changed files with 3282 additions and 2492 deletions

View File

@@ -3,22 +3,25 @@
namespace App\Interfaces;
use App\Facades\Utils;
use App\Models\Award;
use App\Models\Award as AwardModel;
use App\Models\User;
use App\Models\UserAward;
use Log;
/**
* Base class for the Awards, they need to extend this
* Base class for the Awards, you need to extend this, and implement:
* $name
* $param_description (optional)
* public function check($parameter=null)
*
* See: http://docs.phpvms.net/customizing/awards
* @package App\Interfaces
*/
abstract class AwardInterface
abstract class Award
{
public $name = '';
public $param_description = '';
protected $award;
protected $user;
/**
* Each award class just needs to return true or false if it should actually
* be awarded to a user. This is the only method that needs to be implemented
@@ -27,12 +30,19 @@ abstract class AwardInterface
*/
abstract public function check($parameter = null): bool;
/*
* You don't really need to mess with anything below here
*/
protected $award,
$user;
/**
* AwardInterface constructor.
* @param Award $award
* @param User $user
* @param AwardModel $award
* @param User $user
*/
public function __construct(Award $award = null, User $user = null)
public function __construct(AwardModel $award = null, User $user = null)
{
$this->award = $award;
$this->user = $user;
@@ -40,13 +50,14 @@ abstract class AwardInterface
/**
* Run the main handler for this award class to determine if
* it should be awarded or not
* it should be awarded or not. Declared as final to prevent a child
* from accidentally overriding and breaking something
*/
public function handle()
final public function handle(): void
{
# Check if the params are a JSON object or array
$param = $this->award->ref_class_params;
if ($this->award->ref_class_params && Utils::isObject($this->award->ref_class_params)) {
if (Utils::isObject($this->award->ref_class_params)) {
$param = json_decode($this->award->ref_class_params);
}
@@ -59,11 +70,11 @@ abstract class AwardInterface
* Add the award to this user, if they don't already have it
* @return bool|UserAward
*/
protected function addAward()
final protected function addAward()
{
$w = [
'user_id' => $this->user->id,
'award_id' => $this->award->id
'user_id' => $this->user->id,
'award_id' => $this->award->id,
];
$found = UserAward::where($w)->count('id');
@@ -73,7 +84,17 @@ abstract class AwardInterface
// Associate this award to the user now
$award = new UserAward($w);
$award->save();
try {
$award->save();
} catch(\Exception $e) {
Log::error(
'Error saving award: '.$e->getMessage(),
$e->getTrace()
);
return null;
}
return $award;
}

103
app/Interfaces/Controller.php Executable file
View File

@@ -0,0 +1,103 @@
<?php
namespace App\Interfaces;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Class Controller
* @package App\Http\Controllers
*/
abstract class Controller extends \Illuminate\Routing\Controller
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* Shortcut function to get the attributes from a request while running the validations
* @param Request $request
* @param array $attrs_or_validations
* @param array $addtl_fields
* @return array
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
public function getFromReq($request, $attrs_or_validations, $addtl_fields = null)
{
# See if a list of values is passed in, or if a validation list is passed in
$is_validation = false;
if (\count(array_filter(array_keys($attrs_or_validations), '\is_string')) > 0) {
$is_validation = true;
}
if ($is_validation) {
$this->validate($request, $attrs_or_validations);
}
$fields = [];
foreach ($attrs_or_validations as $idx => $field) {
if ($is_validation) {
$field = $idx;
}
if ($request instanceof Request) {
if ($request->filled($field)) {
$fields[$field] = $request->input($field);
}
} else {
if (array_key_exists($field, $request)) {
$fields[$field] = $request[$field];
}
}
}
if (!empty($addtl_fields) && \is_array($addtl_fields)) {
$fields = array_merge($fields, $addtl_fields);
}
return $fields;
}
/**
* Run a validation
* @param $request
* @param $rules
* @return bool
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
/*public function validate($request, $rules)
{
if ($request instanceof Request) {
$validator = Validator::make($request->all(), $rules);
} else {
$validator = Validator::make($request, $rules);
}
if (!$validator->passes()) {
throw new BadRequestHttpException($validator->errors(), null, 400);
}
return true;
}*/
/**
* Simple normalized method for forming the JSON responses
* @param $message
* @return \Illuminate\Http\JsonResponse
*/
public function message($message, $count = null)
{
$attrs = [
'message' => $message
];
if ($count !== null) {
$attrs['count'] = $count;
}
return response()->json($attrs);
}
}

View File

@@ -4,20 +4,42 @@ namespace App\Interfaces;
/**
* Class EnumBase
* Borrowing some features from https://github.com/myclabs/php-enum
* after this was created :)
* @package App\Models\Enums
*/
abstract class Enum
{
protected static $labels = [];
protected static $cache = [];
protected $value;
/**
* Create an instance of this Enum
* @param $val
*/
public function __construct($val)
{
$this->value = $val;
}
/**
* Return the value that's been set if this is an instance
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* Return the label, try to return the translated version as well
* @param $value
* @return mixed
*/
public static function label($value) {
if(isset(static::$labels[$value])) {
public static function label($value)
{
if (isset(static::$labels[$value])) {
return trans(static::$labels[$value]);
}
}
@@ -28,7 +50,7 @@ abstract class Enum
public static function labels()
{
$labels = [];
foreach(static::$labels as $key => $label) {
foreach (static::$labels as $key => $label) {
$labels[$key] = trans($label);
}
@@ -38,10 +60,10 @@ abstract class Enum
/**
* Select box
*/
public static function select($add_blank=false)
public static function select($add_blank = false)
{
$labels = [];
if($add_blank) {
if ($add_blank) {
$labels[] = '';
}
@@ -59,7 +81,7 @@ abstract class Enum
*/
public static function toArray()
{
$class = get_called_class();
$class = static::class;
if (!array_key_exists($class, static::$cache)) {
$reflection = new \ReflectionClass($class);
static::$cache[$class] = $reflection->getConstants();
@@ -69,9 +91,19 @@ abstract class Enum
}
/**
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
* @param Enum $enum
* @return bool
*/
protected function equals(Enum $enum): bool
{
return $this->getValue() === $enum->getValue() && get_called_class() == get_class($enum);
}
/**
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a
* class constant
* @param string $name
* @param array $arguments
* @param array $arguments
* @return static
* @throws \BadMethodCallException
* @throws \ReflectionException

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Interfaces;
/**
* Class Listener
* @package App\Interfaces
*/
abstract class Listener
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace App\Interfaces;
abstract class Migration extends \Illuminate\Database\Migrations\Migration
{
}

8
app/Interfaces/Model.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
namespace App\Interfaces;
abstract class Model extends \Illuminate\Database\Eloquent\Model
{
}

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Interfaces;
use Illuminate\Validation\Validator;
abstract class Repository extends \Prettus\Repository\Eloquent\BaseRepository
{
/**
* @param $id
* @param array $columns
* @return mixed|null
*/
public function findWithoutFail($id, $columns = ['*'])
{
try {
return $this->find($id, $columns);
} catch (\Exception $e) {
return null;
}
}
/**
* @param $values
* @return bool
*/
public function validate($values)
{
$validator = Validator::make(
$values,
$this->model()->rules
);
if ($validator->fails()) {
return $validator->messages();
}
return true;
}
/**
* Return N most recent items, sorted by created_at
* @param int $count
* @param string $sort_by created_at (default) or updated_at
* @return mixed
*/
public function recent($count = null, $sort_by = 'created_at')
{
return $this->orderBy($sort_by, 'desc')->paginate($count);
}
/**
* Find records with a WHERE clause but also sort them
* @param $where
* @param $sort_by
* @param $order_by
* @return $this
*/
public function whereOrder($where, $sort_by, $order_by = 'asc')
{
return $this->scopeQuery(function ($query) use ($where, $sort_by, $order_by) {
$q = $query->where($where);
# See if there are multi-column sorts
if (\is_array($sort_by)) {
foreach ($sort_by as $key => $sort) {
$q = $q->orderBy($key, $sort);
}
} else {
$q = $q->orderBy($sort_by, $order_by);
}
return $q;
});
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace App\Interfaces;
abstract class Service
{
}

25
app/Interfaces/Widget.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
namespace App\Interfaces;
use Arrilot\Widgets\AbstractWidget;
/**
* Class Widget
* @package App\Widgets
*/
abstract class Widget extends AbstractWidget
{
public $cacheTime = 0;
/**
* Render the template
* @param string $template
* @param array $vars
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function view(string $template, array $vars = [])
{
return view($template, $vars);
}
}