From 7dd6a7e7f33226ad9f23e893d409ff261d64b489 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 16 Mar 2018 23:59:53 -0500 Subject: [PATCH] Awards base class and scaffolding #155 --- app/Awards/BaseAward.php | 10 +++++ app/Console/Commands/DevCommands.php | 29 +++++++++--- app/Interfaces/AwardInterface.php | 64 +++++++++++++++++++++++++++ app/Models/UserAward.php | 17 +++++++ app/Services/AwardsService.php | 31 +++++++++++++ modules/Sample/Awards/SampleAward.php | 14 ++++++ modules/Sample/composer.json | 8 +--- 7 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 app/Awards/BaseAward.php create mode 100644 app/Interfaces/AwardInterface.php create mode 100644 app/Models/UserAward.php create mode 100644 app/Services/AwardsService.php create mode 100644 modules/Sample/Awards/SampleAward.php diff --git a/app/Awards/BaseAward.php b/app/Awards/BaseAward.php new file mode 100644 index 00000000..51dbee4d --- /dev/null +++ b/app/Awards/BaseAward.php @@ -0,0 +1,10 @@ + 'clearAcars', - 'clear-users' => 'clearUsers', - 'compile-assets' => 'compileAssets', - 'db-attrs' => 'dbAttrs', - 'xml-to-yaml' => 'xmlToYaml', + 'list-awards' => 'listAwardClasses', + 'clear-acars' => 'clearAcars', + 'clear-users' => 'clearUsers', + 'compile-assets' => 'compileAssets', + 'db-attrs' => 'dbAttrs', + 'xml-to-yaml' => 'xmlToYaml', ]; if(!array_key_exists($command, $commands)) { @@ -44,6 +46,23 @@ class DevCommands extends BaseCommand $this->{$commands[$command]}(); } + /** + * List all award classes + */ + protected function listAwardClasses() + { + $awardSvc = app(AwardsService::class); + $awards = $awardSvc->findAllAwardClasses(); + + $headers = ['Award Name', 'Class']; + $formatted_awards = []; + foreach($awards as $award) { + $formatted_awards[] = [$award->name, \get_class($award)]; + } + + $this->table($headers, $formatted_awards); + } + /** * Delete all the data from the ACARS and PIREP tables */ diff --git a/app/Interfaces/AwardInterface.php b/app/Interfaces/AwardInterface.php new file mode 100644 index 00000000..9ad942e8 --- /dev/null +++ b/app/Interfaces/AwardInterface.php @@ -0,0 +1,64 @@ +award = $award; + } + + /** + * Add the award to this user, if they don't already have it + * @param User $user + * @return bool + */ + public function addAward(User $user) + { + $w = [ + 'user_id' => $user->id, + 'award_id' => $this->award->id + ]; + + $found = UserAward::where($w)->count('id'); + if($found > 0) { + return true; + } + + // Associate this award to the user now + $award = new UserAward($w); + $award->save(); + } + + /** + * Each award class just needs to award + * @param User $user + * @return mixed + */ + public function check(User $user) + { + return false; + } +} diff --git a/app/Models/UserAward.php b/app/Models/UserAward.php new file mode 100644 index 00000000..2dcae944 --- /dev/null +++ b/app/Models/UserAward.php @@ -0,0 +1,17 @@ +getExtraPath('Awards'); + if(!file_exists($path)) { + continue; + } + + $classes = array_keys(ClassMapGenerator::createMap($path)); + foreach($classes as $cl) { + $klass = new $cl; + $awards[] = $klass; + } + } + + return $awards; + } +} diff --git a/modules/Sample/Awards/SampleAward.php b/modules/Sample/Awards/SampleAward.php new file mode 100644 index 00000000..e2ed96ba --- /dev/null +++ b/modules/Sample/Awards/SampleAward.php @@ -0,0 +1,14 @@ +