Add ability to pass in parameters to an Award class #155
This commit is contained in:
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
46
app/Awards/PilotFlightAwards.php
Normal file
46
app/Awards/PilotFlightAwards.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,11 +22,12 @@ class CreateAwardsTable extends Migration
|
|||||||
# ref fields are expenses tied to some model object
|
# ref fields are expenses tied to some model object
|
||||||
# EG, the airports has an internal expense for gate costs
|
# EG, the airports has an internal expense for gate costs
|
||||||
$table->string('ref_class')->nullable();
|
$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->timestamps();
|
||||||
|
|
||||||
$table->index(['ref_class', 'ref_class_id']);
|
$table->index(['ref_class']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,24 @@ class Utils extends Facade
|
|||||||
return 'utils';
|
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
|
* Download a URI. If a file is given, it will save the downloaded
|
||||||
* content into that file
|
* content into that file
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Interfaces;
|
namespace App\Interfaces;
|
||||||
|
|
||||||
|
use App\Facades\Utils;
|
||||||
use App\Models\Award;
|
use App\Models\Award;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\UserAward;
|
use App\Models\UserAward;
|
||||||
@@ -13,6 +14,7 @@ use App\Models\UserAward;
|
|||||||
abstract class AwardInterface
|
abstract class AwardInterface
|
||||||
{
|
{
|
||||||
public $name = '';
|
public $name = '';
|
||||||
|
public $param_description = '';
|
||||||
|
|
||||||
protected $award;
|
protected $award;
|
||||||
protected $user;
|
protected $user;
|
||||||
@@ -21,9 +23,10 @@ abstract class AwardInterface
|
|||||||
* Each award class just needs to return true or false if it
|
* 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
|
* should actually be awarded to a user. This is the only method that
|
||||||
* needs to be implemented
|
* needs to be implemented
|
||||||
|
* @param null $params Optional parameters that are passed in from the UI
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
abstract public function check(): bool;
|
abstract public function check($params = null): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AwardInterface constructor.
|
* AwardInterface constructor.
|
||||||
@@ -42,7 +45,13 @@ abstract class AwardInterface
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
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();
|
$this->addAward();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace App\Models;
|
|||||||
* The Award model
|
* The Award model
|
||||||
* @property mixed id
|
* @property mixed id
|
||||||
* @property mixed ref_class
|
* @property mixed ref_class
|
||||||
* @property mixed|null ref_class_id
|
* @property mixed|null ref_class_params
|
||||||
* @package Award\Models
|
* @package Award\Models
|
||||||
*/
|
*/
|
||||||
class Award extends BaseModel
|
class Award extends BaseModel
|
||||||
@@ -18,7 +18,7 @@ class Award extends BaseModel
|
|||||||
'description',
|
'description',
|
||||||
'image_url',
|
'image_url',
|
||||||
'ref_class',
|
'ref_class',
|
||||||
'ref_class_id',
|
'ref_class_params',
|
||||||
];
|
];
|
||||||
|
|
||||||
public static $rules = [
|
public static $rules = [
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
namespace App\Repositories;
|
namespace App\Repositories;
|
||||||
|
|
||||||
use App\Models\Award;
|
use App\Models\Award;
|
||||||
use App\Repositories\Traits\CacheableRepository;
|
|
||||||
use Prettus\Repository\Contracts\CacheableInterface;
|
use Prettus\Repository\Contracts\CacheableInterface;
|
||||||
|
use Prettus\Repository\Traits\CacheableRepository;
|
||||||
|
|
||||||
class AwardRepository extends BaseRepository implements CacheableInterface
|
class AwardRepository extends BaseRepository implements CacheableInterface
|
||||||
{
|
{
|
||||||
@@ -14,7 +14,7 @@ class AwardRepository extends BaseRepository implements CacheableInterface
|
|||||||
'title' => 'like',
|
'title' => 'like',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function model()
|
public function model(): string
|
||||||
{
|
{
|
||||||
return Award::class;
|
return Award::class;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,14 @@ class SampleAward extends AwardInterface
|
|||||||
{
|
{
|
||||||
public $name = 'Sample Award';
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user