Add ability to pass in parameters to an Award class #155

This commit is contained in:
Nabeel Shahzad
2018-03-17 11:35:34 -05:00
parent 26f00ccaae
commit 7feecb507d
8 changed files with 90 additions and 33 deletions

View File

@@ -1,24 +0,0 @@
<?php
namespace App\Awards;
use App\Interfaces\AwardInterface;
/**
* Simple example of an awards class, where you can apply an award when a user
* has 100 flights. All award classes need to implement the AwardInterface
* @package App\Awards
*/
class Pilot100Flights extends AwardInterface
{
public $name = 'Pilot 100 Flights';
/**
* If the user has over 100 flights, then we can give them this award
* @return bool
*/
public function check(): bool
{
return $this->user->flights >= 100;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Awards;
use App\Interfaces\AwardInterface;
/**
* Simple example of an awards class, where you can apply an award when a user
* has 100 flights. All award classes need to extend the AwardInterface
* @package App\Awards
*/
class PilotFlightAwards extends AwardInterface
{
/**
* Set the name of this award class to make it easier to see when
* assigning to a specific award
* @var string
*/
public $name = 'Pilot Flights';
/**
* The description to show under the parameters field, so the admin knows
* what the parameter actually controls. You can leave this blank if there
* isn't a parameter.
* @var string
*/
public $param_description = 'The number of flights at which to give this award';
/**
* If the user has over N flights, then we can give them this award. This method
* only needs to return a true or false of whether it should be awarded or not.
*
* If no parameter is passed in, just default it to 100. You should check if there
* is a parameter or not
* @param null $params The parameters passed in from the UI
* @return bool
*/
public function check($params = null): bool
{
if(!$params) {
$params = 100;
}
return $this->user->flights >= $params;
}
}

View File

@@ -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']);
});
}

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -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 = [

View File

@@ -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;
}

View File

@@ -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;
}