From 7feecb507d50a3bca0a996ff4a66f7a40a45c83c Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 17 Mar 2018 11:35:34 -0500 Subject: [PATCH] Add ability to pass in parameters to an Award class #155 --- app/Awards/Pilot100Flights.php | 24 ---------- app/Awards/PilotFlightAwards.php | 46 +++++++++++++++++++ .../2018_01_28_180522_create_awards_table.php | 5 +- app/Facades/Utils.php | 18 ++++++++ app/Interfaces/AwardInterface.php | 13 +++++- app/Models/Award.php | 4 +- app/Repositories/AwardRepository.php | 4 +- modules/Sample/Awards/SampleAward.php | 9 +++- 8 files changed, 90 insertions(+), 33 deletions(-) delete mode 100644 app/Awards/Pilot100Flights.php create mode 100644 app/Awards/PilotFlightAwards.php diff --git a/app/Awards/Pilot100Flights.php b/app/Awards/Pilot100Flights.php deleted file mode 100644 index a8357cff..00000000 --- a/app/Awards/Pilot100Flights.php +++ /dev/null @@ -1,24 +0,0 @@ -user->flights >= 100; - } -} diff --git a/app/Awards/PilotFlightAwards.php b/app/Awards/PilotFlightAwards.php new file mode 100644 index 00000000..34655236 --- /dev/null +++ b/app/Awards/PilotFlightAwards.php @@ -0,0 +1,46 @@ +user->flights >= $params; + } +} diff --git a/app/Database/migrations/2018_01_28_180522_create_awards_table.php b/app/Database/migrations/2018_01_28_180522_create_awards_table.php index 0f3b5803..c35b2689 100644 --- a/app/Database/migrations/2018_01_28_180522_create_awards_table.php +++ b/app/Database/migrations/2018_01_28_180522_create_awards_table.php @@ -22,11 +22,12 @@ class CreateAwardsTable extends Migration # ref fields are expenses tied to some model object # EG, the airports has an internal expense for gate costs $table->string('ref_class')->nullable(); - $table->string('ref_class_id', 36)->nullable(); + $table->text('ref_class_params')->nullable(); + #$table->string('ref_class_id', 36)->nullable(); $table->timestamps(); - $table->index(['ref_class', 'ref_class_id']); + $table->index(['ref_class']); }); } diff --git a/app/Facades/Utils.php b/app/Facades/Utils.php index ff14eedd..c9717e5c 100644 --- a/app/Facades/Utils.php +++ b/app/Facades/Utils.php @@ -12,6 +12,24 @@ class Utils extends Facade return 'utils'; } + /** + * Simple check on the first character if it's an object or not + * @param $obj + * @return bool + */ + public static function isObject($obj) + { + if(!$obj) { + return false; + } + + if($obj[0] === '{' || $obj[0] === '[') { + return true; + } + + return false; + } + /** * Download a URI. If a file is given, it will save the downloaded * content into that file diff --git a/app/Interfaces/AwardInterface.php b/app/Interfaces/AwardInterface.php index 4399a5e1..809e3764 100644 --- a/app/Interfaces/AwardInterface.php +++ b/app/Interfaces/AwardInterface.php @@ -2,6 +2,7 @@ namespace App\Interfaces; +use App\Facades\Utils; use App\Models\Award; use App\Models\User; use App\Models\UserAward; @@ -13,6 +14,7 @@ use App\Models\UserAward; abstract class AwardInterface { public $name = ''; + public $param_description = ''; protected $award; protected $user; @@ -21,9 +23,10 @@ abstract class AwardInterface * 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 + * @param null $params Optional parameters that are passed in from the UI * @return bool */ - abstract public function check(): bool; + abstract public function check($params = null): bool; /** * AwardInterface constructor. @@ -42,7 +45,13 @@ abstract class AwardInterface */ public function handle() { - if($this->check()) { + # 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)) { + $param = json_decode($this->award->ref_class_params); + } + + if ($this->check($param)) { $this->addAward(); } } diff --git a/app/Models/Award.php b/app/Models/Award.php index 2810fad6..b0c0071f 100755 --- a/app/Models/Award.php +++ b/app/Models/Award.php @@ -6,7 +6,7 @@ namespace App\Models; * The Award model * @property mixed id * @property mixed ref_class - * @property mixed|null ref_class_id + * @property mixed|null ref_class_params * @package Award\Models */ class Award extends BaseModel @@ -18,7 +18,7 @@ class Award extends BaseModel 'description', 'image_url', 'ref_class', - 'ref_class_id', + 'ref_class_params', ]; public static $rules = [ diff --git a/app/Repositories/AwardRepository.php b/app/Repositories/AwardRepository.php index 5842982d..3f8b4c5c 100755 --- a/app/Repositories/AwardRepository.php +++ b/app/Repositories/AwardRepository.php @@ -3,8 +3,8 @@ namespace App\Repositories; use App\Models\Award; -use App\Repositories\Traits\CacheableRepository; use Prettus\Repository\Contracts\CacheableInterface; +use Prettus\Repository\Traits\CacheableRepository; class AwardRepository extends BaseRepository implements CacheableInterface { @@ -14,7 +14,7 @@ class AwardRepository extends BaseRepository implements CacheableInterface 'title' => 'like', ]; - public function model() + public function model(): string { return Award::class; } diff --git a/modules/Sample/Awards/SampleAward.php b/modules/Sample/Awards/SampleAward.php index fae307c4..a1efe624 100644 --- a/modules/Sample/Awards/SampleAward.php +++ b/modules/Sample/Awards/SampleAward.php @@ -12,7 +12,14 @@ class SampleAward extends AwardInterface { public $name = 'Sample Award'; - public function check(): bool + /** + * This is the method that needs to be implemented. + * You have access to $this->user, which holds the current + * user the award is being checked against + * @param null $params Parameters passed in from the UI + * @return bool + */ + public function check($params = null): bool { return false; }