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.

62 lines
1.6 KiB
PHP

<?php
namespace Isekai\UserPoints\Service;
use MediaWiki\Config\Config;
use MediaWiki\User\User;
use MediaWiki\MediaWikiServices;
class IsekaiUserDailySignFactory {
/** @var MediaWikiServices */
private $services;
/** @var \Config */
private $config;
/** @var array */
private $pointConfig;
/** @var array */
private $dailySignConfig;
private static $instances;
public function __construct(MediaWikiServices $services) {
$this->services = $services;
$this->config = $services->getMainConfig();
$this->pointConfig = $this->config->get('IsekaiUserPointConfig');
$this->dailySignConfig = $this->config->get('IsekaiUserDailySignConfig');
}
/**
* @param User $user
* @return IsekaiUserDailySign|null
*/
public function newFromUser(User $user) {
if (!$this->dailySignConfig) {
throw new \Exception('$wgIsekaiUserDailySignConfig is not configured');
}
if (!$user->isRegistered()) {
return null;
}
$userId = $user->getId();
$cacheKey = $userId;
if (!isset(self::$instances[$cacheKey])) {
self::$instances[$cacheKey] = new IsekaiUserDailySign($user, $this->pointConfig, $this->dailySignConfig, $this->services);
}
return self::$instances[$cacheKey];
}
/**
* @param int $userId
* @return IsekaiUserDailySign|null
*/
public function newFromUserId($userId) {
$user = $this->services->getUserFactory()->newFromId($userId);
if (!$user) {
return null;
}
return $this->newFromUser($user);
}
}