diff --git a/app/Awards/BaseAward.php b/app/Awards/BaseAward.php deleted file mode 100644 index 51dbee4d..00000000 --- a/app/Awards/BaseAward.php +++ /dev/null @@ -1,10 +0,0 @@ -user->flights >= 100; + } +} diff --git a/app/Interfaces/AwardInterface.php b/app/Interfaces/AwardInterface.php index ab732477..4399a5e1 100644 --- a/app/Interfaces/AwardInterface.php +++ b/app/Interfaces/AwardInterface.php @@ -10,39 +10,48 @@ use App\Models\UserAward; * Base class for the Awards, they need to extend this * @package App\Interfaces */ -class AwardInterface +abstract class AwardInterface { - /** - * @var string The name of the award class, needs to be set - */ public $name = ''; - /** - * @var 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. * @param Award $award * @param User $user */ - public function __construct(Award $award, User $user) + public function __construct(Award $award = null, User $user = null) { $this->award = $award; $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 * @return bool|UserAward */ - public function addAward() + protected function addAward() { $w = [ 'user_id' => $this->user->id, @@ -60,14 +69,4 @@ class AwardInterface 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; - } } diff --git a/app/Listeners/AwardListener.php b/app/Listeners/AwardListener.php new file mode 100644 index 00000000..5e0f73f4 --- /dev/null +++ b/app/Listeners/AwardListener.php @@ -0,0 +1,28 @@ +getReference($award, $event->user); + if($klass) { + $klass->handle(); + } + } + } +} diff --git a/app/Models/Award.php b/app/Models/Award.php index ec44d341..2810fad6 100755 --- a/app/Models/Award.php +++ b/app/Models/Award.php @@ -29,15 +29,18 @@ class Award extends BaseModel /** * 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) { return null; } try { - return new $this->ref_class; + return new $this->ref_class($award, $user); # return $klass; # return $klass->find($this->ref_class_id); } catch (\Exception $e) { diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 75012654..6273d505 100755 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -6,6 +6,8 @@ use App\Events\CronMonthly; use App\Events\CronNightly; use App\Events\CronWeekly; use App\Events\Expenses; +use App\Events\UserStateChanged; +use App\Listeners\AwardListener; use App\Listeners\Cron\Nightly\RecalculateBalances; use App\Listeners\ExpenseListener; use App\Listeners\FinanceEvents; @@ -21,6 +23,7 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ + Expenses::class => [ ExpenseListener::class ], @@ -37,20 +40,14 @@ class EventServiceProvider extends ServiceProvider CronMonthly::class => [ \App\Listeners\Cron\Monthly\ApplyExpenses::class ], + + UserStateChanged::class => [ + AwardListener::class, + ], ]; protected $subscribe = [ FinanceEvents::class, NotificationEvents::class, ]; - - /** - * Register any events for your application. - * - * @return void - */ - public function boot() - { - parent::boot(); - } } diff --git a/app/Services/AwardsService.php b/app/Services/AwardsService.php index 9eb8fe58..0c877989 100644 --- a/app/Services/AwardsService.php +++ b/app/Services/AwardsService.php @@ -2,8 +2,8 @@ namespace App\Services; +use App\Support\ClassLoader; use Module; -use Symfony\Component\ClassLoader\ClassMapGenerator; class AwardsService { @@ -13,17 +13,16 @@ class AwardsService public function findAllAwardClasses() { $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) { $path = $module->getExtraPath('Awards'); - if(!file_exists($path)) { - continue; - } - - $classes = array_keys(ClassMapGenerator::createMap($path)); - foreach($classes as $cl) { - $klass = new $cl; - $awards[] = $klass; - } + $classes = ClassLoader::getClassesInPath($path); + $awards = array_merge($awards, $classes); } return $awards; diff --git a/app/Support/ClassLoader.php b/app/Support/ClassLoader.php new file mode 100644 index 00000000..a8655200 --- /dev/null +++ b/app/Support/ClassLoader.php @@ -0,0 +1,29 @@ +