Awards base class and scaffolding #155

This commit is contained in:
Nabeel Shahzad 2018-03-16 23:59:53 -05:00
parent 067fb0f9f0
commit 7dd6a7e7f3
7 changed files with 161 additions and 12 deletions

10
app/Awards/BaseAward.php Normal file
View File

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

View File

@ -7,6 +7,7 @@ use App\Models\Acars;
use App\Models\Airline;
use App\Models\Pirep;
use App\Models\User;
use App\Services\AwardsService;
use DB;
use PDO;
use Symfony\Component\Yaml\Yaml;
@ -29,11 +30,12 @@ class DevCommands extends BaseCommand
}
$commands = [
'clear-acars' => '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
*/

View File

@ -0,0 +1,64 @@
<?php
namespace App\Interfaces;
use App\Models\User;
use App\Models\UserAward;
/**
* Base class for the Awards, they need to extend this
* @package App\Interfaces
*/
class AwardInterface
{
/**
* @var string The name of the award class, needs to be set
*/
public $name = '';
/**
* @var Award
*/
protected $award;
/**
* AwardInterface constructor.
* @param $award
*/
public function __construct($award)
{
$this->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;
}
}

17
app/Models/UserAward.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace App\Models;
/**
* Class UserAward
* @package App\Models
*/
class UserAward extends BaseModel
{
public $table = 'user_awards';
public $fillable = [
'user_id',
'award_id'
];
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Services;
use Module;
use Symfony\Component\ClassLoader\ClassMapGenerator;
class AwardsService
{
/**
* Find any of the award classes
*/
public function findAllAwardClasses()
{
$awards = [];
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;
}
}
return $awards;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Modules\Sample\Awards;
use App\Interfaces\AwardInterface;
/**
* Class SampleAward
* @package Modules\Sample\Awards
*/
class SampleAward extends AwardInterface
{
public $name = 'Sample Award';
}

View File

@ -1,13 +1,7 @@
{
"name": "/sample-module",
"name": "phpvms/sample-module",
"type": "laravel-module",
"description": "",
"authors": [
{
"name": "",
"email": ""
}
],
"require": {
"joshbrw/laravel-module-installer": "0.1.x"
},