Add the event listener for the awards #155
This commit is contained in:
@@ -1,10 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Awards;
|
|
||||||
|
|
||||||
use App\Interfaces\AwardInterface;
|
|
||||||
|
|
||||||
class BaseAward implements AwardInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
24
app/Awards/Pilot100Flights.php
Normal file
24
app/Awards/Pilot100Flights.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,39 +10,48 @@ use App\Models\UserAward;
|
|||||||
* Base class for the Awards, they need to extend this
|
* Base class for the Awards, they need to extend this
|
||||||
* @package App\Interfaces
|
* @package App\Interfaces
|
||||||
*/
|
*/
|
||||||
class AwardInterface
|
abstract class AwardInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var string The name of the award class, needs to be set
|
|
||||||
*/
|
|
||||||
public $name = '';
|
public $name = '';
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Award
|
|
||||||
*/
|
|
||||||
protected $award;
|
protected $award;
|
||||||
|
protected $user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var 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
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected $user;
|
abstract public function check(): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AwardInterface constructor.
|
* AwardInterface constructor.
|
||||||
* @param Award $award
|
* @param Award $award
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
public function __construct(Award $award, User $user)
|
public function __construct(Award $award = null, User $user = null)
|
||||||
{
|
{
|
||||||
$this->award = $award;
|
$this->award = $award;
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the main handler for this award class to determine if
|
||||||
|
* it should be awarded or not
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
if($this->check()) {
|
||||||
|
$this->addAward();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the award to this user, if they don't already have it
|
* Add the award to this user, if they don't already have it
|
||||||
* @return bool|UserAward
|
* @return bool|UserAward
|
||||||
*/
|
*/
|
||||||
public function addAward()
|
protected function addAward()
|
||||||
{
|
{
|
||||||
$w = [
|
$w = [
|
||||||
'user_id' => $this->user->id,
|
'user_id' => $this->user->id,
|
||||||
@@ -60,14 +69,4 @@ class AwardInterface
|
|||||||
|
|
||||||
return $award;
|
return $award;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Each award class just needs to return true or false
|
|
||||||
* if it should actually be awarded to a user
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function check()
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
28
app/Listeners/AwardListener.php
Normal file
28
app/Listeners/AwardListener.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use App\Events\UserStateChanged;
|
||||||
|
use App\Models\Award;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look for and run any of the award classes
|
||||||
|
* @package App\Listeners
|
||||||
|
*/
|
||||||
|
class AwardListener
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Call all of the awards
|
||||||
|
* @param UserStateChanged $event
|
||||||
|
*/
|
||||||
|
public function handle(UserStateChanged $event)
|
||||||
|
{
|
||||||
|
$awards = Award::all();
|
||||||
|
foreach($awards as $award) {
|
||||||
|
$klass = $award->getReference($award, $event->user);
|
||||||
|
if($klass) {
|
||||||
|
$klass->handle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,15 +29,18 @@ class Award extends BaseModel
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the referring object
|
* Get the referring object
|
||||||
|
* @param Award|null $award
|
||||||
|
* @param User|null $user
|
||||||
|
* @return null
|
||||||
*/
|
*/
|
||||||
public function getReference()
|
public function getReference(Award $award=null, User $user=null)
|
||||||
{
|
{
|
||||||
if (!$this->ref_class) {
|
if (!$this->ref_class) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new $this->ref_class;
|
return new $this->ref_class($award, $user);
|
||||||
# return $klass;
|
# return $klass;
|
||||||
# return $klass->find($this->ref_class_id);
|
# return $klass->find($this->ref_class_id);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ use App\Events\CronMonthly;
|
|||||||
use App\Events\CronNightly;
|
use App\Events\CronNightly;
|
||||||
use App\Events\CronWeekly;
|
use App\Events\CronWeekly;
|
||||||
use App\Events\Expenses;
|
use App\Events\Expenses;
|
||||||
|
use App\Events\UserStateChanged;
|
||||||
|
use App\Listeners\AwardListener;
|
||||||
use App\Listeners\Cron\Nightly\RecalculateBalances;
|
use App\Listeners\Cron\Nightly\RecalculateBalances;
|
||||||
use App\Listeners\ExpenseListener;
|
use App\Listeners\ExpenseListener;
|
||||||
use App\Listeners\FinanceEvents;
|
use App\Listeners\FinanceEvents;
|
||||||
@@ -21,6 +23,7 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $listen = [
|
protected $listen = [
|
||||||
|
|
||||||
Expenses::class => [
|
Expenses::class => [
|
||||||
ExpenseListener::class
|
ExpenseListener::class
|
||||||
],
|
],
|
||||||
@@ -37,20 +40,14 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
CronMonthly::class => [
|
CronMonthly::class => [
|
||||||
\App\Listeners\Cron\Monthly\ApplyExpenses::class
|
\App\Listeners\Cron\Monthly\ApplyExpenses::class
|
||||||
],
|
],
|
||||||
|
|
||||||
|
UserStateChanged::class => [
|
||||||
|
AwardListener::class,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $subscribe = [
|
protected $subscribe = [
|
||||||
FinanceEvents::class,
|
FinanceEvents::class,
|
||||||
NotificationEvents::class,
|
NotificationEvents::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* Register any events for your application.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function boot()
|
|
||||||
{
|
|
||||||
parent::boot();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Support\ClassLoader;
|
||||||
use Module;
|
use Module;
|
||||||
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
|
||||||
|
|
||||||
class AwardsService
|
class AwardsService
|
||||||
{
|
{
|
||||||
@@ -13,17 +13,16 @@ class AwardsService
|
|||||||
public function findAllAwardClasses()
|
public function findAllAwardClasses()
|
||||||
{
|
{
|
||||||
$awards = [];
|
$awards = [];
|
||||||
|
|
||||||
|
# Find the awards in the app/Awards directory
|
||||||
|
$classes = ClassLoader::getClassesInPath(app_path('/Awards'));
|
||||||
|
$awards = array_merge($awards, $classes);
|
||||||
|
|
||||||
|
# Look throughout all the other modules
|
||||||
foreach (Module::all() as $module) {
|
foreach (Module::all() as $module) {
|
||||||
$path = $module->getExtraPath('Awards');
|
$path = $module->getExtraPath('Awards');
|
||||||
if(!file_exists($path)) {
|
$classes = ClassLoader::getClassesInPath($path);
|
||||||
continue;
|
$awards = array_merge($awards, $classes);
|
||||||
}
|
|
||||||
|
|
||||||
$classes = array_keys(ClassMapGenerator::createMap($path));
|
|
||||||
foreach($classes as $cl) {
|
|
||||||
$klass = new $cl;
|
|
||||||
$awards[] = $klass;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $awards;
|
return $awards;
|
||||||
|
|||||||
29
app/Support/ClassLoader.php
Normal file
29
app/Support/ClassLoader.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
||||||
|
|
||||||
|
class ClassLoader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getClassesInPath($path)
|
||||||
|
{
|
||||||
|
$classes = [];
|
||||||
|
|
||||||
|
if (!file_exists($path)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$all_classes = array_keys(ClassMapGenerator::createMap($path));
|
||||||
|
foreach ($all_classes as $cl) {
|
||||||
|
$klass = new $cl;
|
||||||
|
$classes[] = $klass;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $classes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,4 +11,9 @@ use App\Interfaces\AwardInterface;
|
|||||||
class SampleAward extends AwardInterface
|
class SampleAward extends AwardInterface
|
||||||
{
|
{
|
||||||
public $name = 'Sample Award';
|
public $name = 'Sample Award';
|
||||||
|
|
||||||
|
public function check(): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user