2016-10-23 11:41:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Repositories;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Player;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
|
|
|
class UserRepository extends Repository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if a user exists in the repository.
|
|
|
|
*
|
|
|
|
* @param string $identification
|
|
|
|
* @param string $type Must be one of properties defined in User class
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has($identification, $type = 'uid')
|
|
|
|
{
|
|
|
|
if ($type == "uid") {
|
|
|
|
return Arr::has($this->items, $identification);
|
|
|
|
} else {
|
2017-01-08 10:40:22 +08:00
|
|
|
return (bool) Arr::where((array) $this->items, function($key, $value) use ($identification, $type) {
|
2016-11-17 17:32:12 +08:00
|
|
|
if (property_exists($value, $type))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ($value->$type == $identification);
|
2016-10-23 11:41:52 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a user from repository and cache it.
|
|
|
|
*
|
|
|
|
* @param string $identification
|
|
|
|
* @param string $type
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($identification, $type = 'uid')
|
|
|
|
{
|
|
|
|
if (!$this->has($identification, $type)) {
|
|
|
|
if ($type == "username") {
|
|
|
|
$player = Player::where('player_name', $identification)->first();
|
|
|
|
|
|
|
|
if ($player) {
|
|
|
|
$identification = $player->uid;
|
|
|
|
$type = "uid";
|
2016-11-05 21:08:10 +08:00
|
|
|
} else {
|
|
|
|
return null;
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::where($type, $identification)->first();
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
$this->set($user->uid, $user);
|
|
|
|
return $user;
|
|
|
|
}
|
2017-01-08 10:40:22 +08:00
|
|
|
|
|
|
|
return null;
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|
|
|
|
|
2017-01-08 10:40:22 +08:00
|
|
|
$result = Arr::where((array) $this->items, function($key, $value) use ($identification, $type) {
|
|
|
|
if (property_exists($value, $type))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ($value->$type == $identification);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return first element
|
|
|
|
reset($result);
|
|
|
|
return current($result);
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|
2016-11-17 17:32:12 +08:00
|
|
|
|
|
|
|
public function getCurrentUser()
|
|
|
|
{
|
|
|
|
return $this->get(session('uid'));
|
|
|
|
}
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|