Add the event listener for the awards #155

This commit is contained in:
Nabeel Shahzad 2018-03-17 00:55:39 -05:00
parent 31b9195a6e
commit 26f00ccaae
9 changed files with 127 additions and 53 deletions

View File

@ -1,10 +0,0 @@
<?php
namespace App\Awards;
use App\Interfaces\AwardInterface;
class BaseAward implements AwardInterface
{
}

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

View File

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

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

View File

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

View File

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

View File

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

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

View File

@ -11,4 +11,9 @@ use App\Interfaces\AwardInterface;
class SampleAward extends AwardInterface
{
public $name = 'Sample Award';
public function check(): bool
{
return false;
}
}