phpvms/app/Services/UserService.php

370 lines
9.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
2019-07-16 03:51:35 +08:00
use App\Contracts\Service;
use App\Events\UserRegistered;
2017-12-03 00:55:17 +08:00
use App\Events\UserStateChanged;
use App\Events\UserStatsChanged;
use App\Exceptions\UserPilotIdExists;
2018-08-20 22:42:54 +08:00
use App\Models\Enums\PirepState;
2017-12-31 10:40:32 +08:00
use App\Models\Enums\UserState;
2018-08-20 22:42:54 +08:00
use App\Models\Pirep;
2018-02-21 12:33:09 +08:00
use App\Models\Rank;
use App\Models\Role;
use App\Models\User;
use App\Repositories\AircraftRepository;
use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Support\Units\Time;
2018-02-21 12:33:09 +08:00
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use function is_array;
class UserService extends Service
{
2018-08-27 00:40:04 +08:00
private $aircraftRepo;
private $subfleetRepo;
private $userRepo;
/**
* UserService constructor.
2018-08-27 00:40:04 +08:00
*
* @param AircraftRepository $aircraftRepo
* @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/
public function __construct(
AircraftRepository $aircraftRepo,
SubfleetRepository $subfleetRepo,
UserRepository $userRepo
) {
$this->aircraftRepo = $aircraftRepo;
$this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo;
}
2017-12-15 13:51:57 +08:00
/**
* Register a pilot. Also attaches the initial roles
* required, and then triggers the UserRegistered event
2018-08-27 00:40:04 +08:00
*
* @param User $user User model
* @param array $groups Additional groups to assign
2018-08-27 00:40:04 +08:00
*
* @throws \Exception
2018-08-27 00:40:04 +08:00
*
* @return mixed
2017-12-15 13:51:57 +08:00
*/
public function createUser(User $user, array $groups = null)
2017-12-15 13:51:57 +08:00
{
2018-08-27 00:40:04 +08:00
// Determine if we want to auto accept
if (setting('pilots.auto_accept') === true) {
2017-12-31 10:40:32 +08:00
$user->state = UserState::ACTIVE;
2017-12-15 13:51:57 +08:00
} else {
2017-12-31 10:40:32 +08:00
$user->state = UserState::PENDING;
2017-12-23 03:54:25 +08:00
}
2017-12-15 13:51:57 +08:00
$user->save();
2017-12-15 13:51:57 +08:00
2018-08-27 00:40:04 +08:00
// Attach the user roles
// $role = Role::where('name', 'user')->first();
// $user->attachRole($role);
2017-12-15 13:51:57 +08:00
// Attach any additional roles
if (!empty($groups) && is_array($groups)) {
2017-12-30 06:56:46 +08:00
foreach ($groups as $group) {
$role = Role::where('name', $group)->first();
$user->attachRole($role);
}
}
2018-08-27 00:40:04 +08:00
// Let's check their rank and where they should start
2017-12-15 13:51:57 +08:00
$this->calculatePilotRank($user);
$user->refresh();
2017-12-15 13:51:57 +08:00
event(new UserRegistered($user));
return $user;
}
/**
* Find the next available pilot ID and set the current user's pilot_id to that +1
* Called from UserObserver right now after a record is created
*
* @param User $user
*
* @return User
*/
public function findAndSetPilotId(User $user): User
{
if ($user->pilot_id !== null && $user->pilot_id > 0) {
return $user;
}
$max = (int) User::max('pilot_id');
$user->pilot_id = $max + 1;
$user->save();
2019-07-17 02:06:13 +08:00
Log::info('Set pilot ID for user '.$user->id.' to '.$user->pilot_id);
return $user;
}
public function isPilotIdAlreadyUsed(int $pilot_id): bool
{
return User::where('pilot_id', '=', $pilot_id)->exists();
}
/**
* Change a user's pilot ID
*
* @param User $user
2019-07-17 02:06:13 +08:00
* @param int $pilot_id
*
* @throws UserPilotIdExists
*
* @return User
*/
public function changePilotId(User $user, int $pilot_id): User
{
if ($user->pilot_id === $pilot_id) {
return $user;
}
if ($this->isPilotIdAlreadyUsed($pilot_id)) {
Log::error('User with id '.$pilot_id.' already exists');
2019-07-17 02:06:13 +08:00
throw new UserPilotIdExists($user);
}
$old_id = $user->pilot_id;
$user->pilot_id = $pilot_id;
$user->save();
Log::info('Changed pilot ID for user '.$user->id.' from '.$old_id.' to '.$user->pilot_id);
return $user;
}
/**
* Return the subfleets this user is allowed access to,
* based on their current rank
2018-08-27 00:40:04 +08:00
*
* @param $user
2018-08-27 00:40:04 +08:00
*
* @return Collection
*/
public function getAllowableSubfleets($user)
{
if ($user === null || setting('pireps.restrict_aircraft_to_rank') === false) {
return $this->subfleetRepo->with('aircraft')->all();
}
$subfleets = $user->rank->subfleets();
return $subfleets->with('aircraft')->get();
}
/**
* Return a bool if a user is allowed to fly the current aircraft
2018-08-27 00:40:04 +08:00
*
* @param $user
* @param $aircraft_id
2018-08-27 00:40:04 +08:00
*
* @return bool
*/
public function aircraftAllowed($user, $aircraft_id)
{
$aircraft = $this->aircraftRepo->find($aircraft_id, ['subfleet_id']);
$subfleets = $this->getAllowableSubfleets($user);
$subfleet_ids = $subfleets->pluck('id')->toArray();
return \in_array($aircraft->subfleet_id, $subfleet_ids, true);
}
/**
* Change the user's state. PENDING to ACCEPTED, etc
* Send out an email
2018-08-27 00:40:04 +08:00
*
* @param User $user
* @param $old_state
2018-08-27 00:40:04 +08:00
*
* @return User
*/
public function changeUserState(User $user, $old_state): User
{
if ($user->state === $old_state) {
return $user;
}
Log::info('User '.$user->ident.' state changing from '
.UserState::label($old_state).' to '
.UserState::label($user->state));
event(new UserStateChanged($user, $old_state));
return $user;
}
/**
* Adjust the number of flights a user has. Triggers
* UserStatsChanged event
2018-08-27 00:40:04 +08:00
*
* @param User $user
* @param int $count
2018-08-27 00:40:04 +08:00
*
* @return User
*/
public function adjustFlightCount(User $user, int $count): User
{
$user->refresh();
$old_value = $user->flights;
$user->flights += $count;
$user->save();
event(new UserStatsChanged($user, 'flights', $old_value));
2017-12-03 00:55:17 +08:00
return $user;
}
/**
* Update a user's flight times
2018-08-27 00:40:04 +08:00
*
* @param User $user
* @param int $minutes
2018-08-27 00:40:04 +08:00
*
* @return User
*/
public function adjustFlightTime(User $user, int $minutes): User
{
$user->refresh();
$user->flight_time += $minutes;
$user->save();
return $user;
}
/**
* See if a pilot's rank has change. Triggers the UserStatsChanged event
2018-08-27 00:40:04 +08:00
*
* @param User $user
2018-08-27 00:40:04 +08:00
*
* @return User
*/
public function calculatePilotRank(User $user): User
{
$user->refresh();
2018-08-27 00:40:04 +08:00
// If their current rank is one they were assigned, then
// don't change away from it automatically.
if ($user->rank && $user->rank->auto_promote === false) {
return $user;
}
// If we should count their transfer hours?
if (setting('pilots.count_transfer_hours', false) === true) {
$pilot_hours = new Time($user->flight_time + $user->transfer_time);
} else {
$pilot_hours = new Time($user->flight_time);
}
2018-08-27 00:40:04 +08:00
// The current rank's hours are over the pilot's current hours,
// so assume that they were "placed" here by an admin so don't
// bother with updating it
if ($user->rank && $user->rank->hours > $pilot_hours->hours) {
return $user;
}
$old_rank = $user->rank;
$original_rank_id = $user->rank_id;
$ranks = Rank::where('auto_promote', true)
->orderBy('hours', 'asc')->get();
foreach ($ranks as $rank) {
if ($rank->hours > $pilot_hours->hours) {
break;
}
2018-08-20 22:48:39 +08:00
$user->rank_id = $rank->id;
}
// Only trigger the event/update if there's been a change
if ($user->rank_id !== $original_rank_id) {
$user->save();
$user->refresh();
event(new UserStatsChanged($user, 'rank', $old_rank));
}
return $user;
}
2017-12-03 00:55:17 +08:00
/**
* Set the user's status to being on leave
2018-08-27 00:40:04 +08:00
*
* @param User $user
2018-08-27 00:40:04 +08:00
*
* @return User
*/
2018-03-31 11:36:01 +08:00
public function setStatusOnLeave(User $user): User
{
$user->refresh();
$user->state = UserState::ON_LEAVE;
$user->save();
event(new UserStateChanged($user, UserState::ACTIVE));
$user->refresh();
return $user;
}
/**
* Recalculate the stats for all active users
*/
public function recalculateAllUserStats(): void
{
$w = [
['state', '!=', UserState::REJECTED],
];
$this->userRepo
->findWhere($w, ['id', 'name', 'airline_id'])
->each(function ($user, $_) {
return $this->recalculateStats($user);
});
}
/**
* Recount/update all of the stats for a user
2018-08-27 00:40:04 +08:00
*
* @param User $user
2018-08-27 00:40:04 +08:00
*
* @return User
*/
public function recalculateStats(User $user): User
{
2018-08-27 00:40:04 +08:00
// Recalc their hours
2018-08-20 22:42:54 +08:00
$w = [
'user_id' => $user->id,
2018-08-27 00:40:04 +08:00
'state' => PirepState::ACCEPTED,
2018-08-20 22:42:54 +08:00
];
$flight_count = Pirep::where($w)->count();
$user->flights = $flight_count;
2018-08-20 22:42:54 +08:00
$flight_time = Pirep::where($w)->sum('flight_time');
$user->flight_time = $flight_time;
$user->save();
2018-08-27 00:40:04 +08:00
// Recalc the rank
2018-08-20 22:42:54 +08:00
$this->calculatePilotRank($user);
Log::info('User '.$user->ident.' updated; flight count='.$flight_count
.', rank='.$user->rank->name
.', flight_time='.$user->flight_time.' minutes');
2018-08-20 22:42:54 +08:00
$user->save();
return $user;
}
}