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

[TASK] Moved karma source fetch into KarmaService

parent 93d01f96
......@@ -7,6 +7,7 @@ use T3o\Karma\Domain\Model\FrontendUser;
use T3o\Karma\Domain\Model\KarmaSource;
use T3o\Karma\Domain\Model\LedgerEntry;
use T3o\Karma\Domain\Repository\FrontendUserRepository;
use T3o\Karma\Domain\Repository\KarmaSourceRepository;
use T3o\Karma\Domain\Repository\LedgerEntryRepository;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
......@@ -45,6 +46,11 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
*/
protected $frontendUserRepository;
/**
* @var \T3o\Karma\Domain\Repository\KarmaSourceRepository
*/
protected $karmaSourceRepository;
/**
* @param LedgerEntryRepository $ledgerEntryRepository
*/
......@@ -61,6 +67,13 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
$this->frontendUserRepository = $frontendUserRepository;
}
/**
* @param KarmaSourceRepository $karmaSourceRepository
*/
public function injectKarmaSourceRepository(KarmaSourceRepository $karmaSourceRepository) {
$this->karmaSourceRepository = $karmaSourceRepository;
}
/**
* Get the imimmutable karma total from the user
*
......@@ -153,6 +166,34 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
$this->frontendUserRepository->update($frontendUser);
}
/**
* Get a Karma Source object. Will generate a new object with code and label if none exists.
*
* @param string $sourceCode
* @param string $defaultLabel
* @return KarmaSource
* @throws \Exception when $sourceCode is an empty string
*/
public function getKarmaSourceByCode(string $sourceCode, $defaultLabel='[No Label Set]')
{
if ($sourceCode == '') {
throw new \Exception('Empty karma source code supplied.', 1541508560);
}
$karmaSource = $this->karmaSourceRepository->findByCode($sourceCode)->getFirst();
if ($karmaSource === null) {
/** @var KarmaSource $karmaSource */
$karmaSource = new KarmaSource();
$karmaSource->setCode($karmaSourceCode);
$karmaSource->setTitle($defaultLabel);
$this->karmaSourceRepository->add($karmaSource);
}
return $karmaSource;
}
/**
* Will take any FrontendUser object and return a karma extension FrontendUser subclass with the data we need
*
......
......@@ -46,11 +46,6 @@ class UserProfileChangeKarmaIssuer
*/
protected $frontendUserRepository;
/**
* @var \T3o\Karma\Domain\Repository\KarmaSourceRepository
*/
protected $karmaSourceRepository;
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
......@@ -91,14 +86,6 @@ class UserProfileChangeKarmaIssuer
$this->frontendUserRepository = $frontendUserRepository;
}
/**
* @param \T3o\Karma\Domain\Repository\KarmaSourceRepository $karmaSourceRepository
*/
public function injectKarmaSourceRepository(KarmaSourceRepository $karmaSourceRepository)
{
$this->karmaSourceRepository = $karmaSourceRepository;
}
/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
*/
......@@ -113,22 +100,10 @@ class UserProfileChangeKarmaIssuer
*/
public function newUserWasCreated(ExtbaseFrontendUser $frontendUser)
{
$karmaSourceCode = $this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['sourceCode'];
if ($karmaSourceCode == '') {
throw new \Exception('Empty karma source code supplied. Please configure it in TypoScript.', 1541508560);
}
$karmaSource = $this->karmaSourceRepository->findByCode($karmaSourceCode)->getFirst();
if ($karmaSource === null) {
/** @var KarmaSource $karmaSource */
$karmaSource = new KarmaSource();
$karmaSource->setCode($karmaSourceCode);
$karmaSource->setTitle($this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['defaultSourceLabel']);
$this->karmaSourceRepository->add($karmaSource);
}
$karmaSource = $this->karmaService->getKarmaSourceByCode(
$this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['sourceCode'],
$this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['defaultSourceLabel']
);
$this->karmaService->addKarmaToUser(
$this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['valueEarned'],
......
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