Commit 21406232 authored by mabolek's avatar mabolek Committed by Thomas Löffler
Browse files

[TASK] Karma Service class created with basic functions

parent a5fb894f
<?php
namespace T3o\Karma\Service;
use T3o\Karma\Domain\Model\FrontendUser;
use T3o\Karma\Domain\Repository\FrontendUserRepository;
use T3o\Karma\Domain\Repository\LedgerEntryRepository;
/***************************************************************
* Copyright notice
*
* (c) 2018
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
{
/**
* @var LedgerEntryRepository
*/
protected $ledgerEntryRepository;
/**
* @var FrontendUserRepository
*/
protected $frontendUserRepository;
/**
* @param LedgerEntryRepository $ledgerEntryRepository
*/
public function injectLedgerEntryRepository(LedgerEntryRepository $ledgerEntryRepository)
{
$this->ledgerEntryRepository = $ledgerEntryRepository;
}
/**
* @param FrontendUserRepository $frontendUserRepository
*/
public function injectFrontendUserRepository(FrontendUserRepository $frontendUserRepository)
{
$this->frontendUserRepository = $frontendUserRepository;
}
/**
* Get the imimmutable karma total from the user
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser
* @param bool $ignoreCache Fetch value directly from ledger
* @param bool $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update immutable cache.
* @return int Immutable Karma Total
*/
public function getImmutableKarmaForUser(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser, $ignoreCache = false, $updateCacheEntry = true)
{
$frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser);
if (!$ignoreCache) {
return $frontendUser->getKarmaImmutableValue();
}
$immutableKarma = $this->ledgerEntryRepository->calculateImmutableKarmaTotalForUser($frontendUser);
if($updateCacheEntry) {
$immutableKarma = $this->getMutableKarmaForUser($frontendUser, true, false);
$frontendUser->setKarmaImmutableValue($immutableKarma);
$frontendUser->setKarmaImmutableValue($immutableKarma);
$frontendUser->setKarmaCacheTimestamp(time());
$this->frontendUserRepository->update($frontendUser);
}
return $immutableKarma;
}
/**
* Get the mutable karma total from the user
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser
* @param bool $ignoreCache Fetch value directly from ledger
* @param bool $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update mutable cache.
* @return int Mutable Karma Total
*/
public function getMutableKarmaForUser(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser, $ignoreCache = false, $updateCacheEntry = true)
{
$frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser);
if (!$ignoreCache) {
return $frontendUser->getKarmaMutableValue();
}
$mutableKarma = $this->ledgerEntryRepository->calculateMutableKarmaTotalForUser($frontendUser);
if($updateCacheEntry) {
$mutableKarma = $this->getImmutableKarmaForUser($frontendUser, true, false);
$frontendUser->setKarmaMutableValue($mutableKarma);
$frontendUser->setKarmaMutableValue($mutableKarma);
$frontendUser->setKarmaCacheTimestamp(time());
$this->frontendUserRepository->update($frontendUser);
}
return $mutableKarma;
}
/**
* Will take any FrontendUser object and return a karma extension FrontendUser subclass with the data we need
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser
* @return FrontendUser
* @internal
*/
public function ensureCorrectFrontendUserSubclass(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser) {
//If user is not an instance of our own frontend user class we have to fetch it again
if (!($frontendUser instanceof FrontendUser)) {
$frontendUser = $this->frontendUserRepository->findByUid($frontendUser->getUid());
}
return $frontendUser;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment