You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.2 KiB
PHP
114 lines
3.2 KiB
PHP
<?php
|
|
namespace Isekai\UserPoints\Service;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
use User;
|
|
|
|
class IsekaiUserPointsFactory {
|
|
/** @var MediaWikiServices */
|
|
private $services;
|
|
|
|
/** @var \Config */
|
|
private $config;
|
|
|
|
/** @var array */
|
|
private $pointConfig;
|
|
|
|
private static $instances;
|
|
|
|
public function __construct(MediaWikiServices $services) {
|
|
$this->services = $services;
|
|
$this->config = $services->getMainConfig();
|
|
$this->pointConfig = $this->config->get('IsekaiUserPointConfig');
|
|
}
|
|
|
|
/**
|
|
* @param int $userId
|
|
* @param string $pointType
|
|
* @return IsekaiUserPoints|null
|
|
*/
|
|
public function newFromUserId(int $userId, string $pointType) {
|
|
$user = $this->services->getUserFactory()->newFromId($userId);
|
|
return $this->newFromUser($user, $pointType);
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @return IsekaiUserPoints|null
|
|
*/
|
|
public function newFromUser(User $user, string $pointType) {
|
|
if (!isset($this->pointConfig[$pointType])) {
|
|
return null;
|
|
}
|
|
|
|
if (!$user->isRegistered()) {
|
|
return null;
|
|
}
|
|
|
|
$userId = $user->getId();
|
|
$cacheKey = $userId . ':' . $pointType;
|
|
if (!isset(self::$instances[$cacheKey])) {
|
|
self::$instances[$cacheKey] = new IsekaiUserPoints($user, $pointType, $this->pointConfig, $this->services);
|
|
}
|
|
return self::$instances[$cacheKey];
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @param string $pointType
|
|
* @param stdClass $data
|
|
*/
|
|
public function newFromData(User $user, string $pointType, $data) {
|
|
$instance = $this->newFromUser($user, $pointType);
|
|
if ($instance) {
|
|
$instance->setData($data);
|
|
}
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
* @return array{0: User, 1: string, 2: IsekaiUserPoints} (user, instance)
|
|
*/
|
|
public function newFromUsers(array $users, array $pointTypes) {
|
|
$userids = [];
|
|
$usermap = [];
|
|
foreach ($users as $user) {
|
|
$userId = $user->getId();
|
|
$userids[] = $userId;
|
|
$usermap[$userId] = $user;
|
|
}
|
|
|
|
$dbr = $this->services->getDBLoadBalancer()->getConnection(DB_REPLICA);
|
|
$pointDataRows = $dbr->select(
|
|
'isekai_user_points',
|
|
'*',
|
|
[ 'user_id' => $userids, 'point_type' => $pointTypes ],
|
|
__METHOD__
|
|
);
|
|
|
|
$result = [];
|
|
$initedUsers = [];
|
|
foreach ($pointDataRows as $pointData) {
|
|
$user = $usermap[$pointData->user_id];
|
|
$pointType = $pointData->type;
|
|
$instance = $this->newFromUser($user, $pointType);
|
|
if ($instance) {
|
|
$instance->setData($pointData);
|
|
}
|
|
|
|
$result[] = [$user, $instance];
|
|
$initedUsers[] = $user;
|
|
}
|
|
foreach ($userids as $userId) {
|
|
if (!in_array($userId, $initedUsers)) {
|
|
$user = $usermap[$userId];
|
|
$pointType = $pointData->type;
|
|
$instance = $this->newFromUser($user, $pointType);
|
|
|
|
$result[] = [$user, $pointType, $instance];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
} |