Commit ff5f19d1 authored by Thomas Löffler's avatar Thomas Löffler

Merge branch '276-add-simplified-karma-issuer-action-trigger-api' into 'develop'

Resolve "Add simplified karma issuer action trigger API"

Closes #276

See merge request !134
parents c2ef1337 46e68f2f
Pipeline #6190 passed with stages
in 3 minutes and 59 seconds
......@@ -6,6 +6,7 @@ use T3o\Karma\Domain\Model\FrontendUser;
use T3o\Karma\Domain\Model\LedgerEntry;
use T3o\Karma\Domain\Repository\FrontendUserRepository;
use T3o\Karma\Domain\Repository\LedgerEntryRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
/***************************************************************
......@@ -43,6 +44,25 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
*/
protected $frontendUserRepository;
/**
* @var array
*/
protected $settings;
/**
* UserProfileChangeKarmaIssuerUtility constructor.
*/
public function __construct()
{
$objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
/** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager */
$configurationManager = $objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
$this->settings = $configurationManager->getConfiguration(
\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'karma'
);
}
/**
* @param LedgerEntryRepository $ledgerEntryRepository
*/
......@@ -185,6 +205,44 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
$this->frontendUserRepository->update($frontendUser);
}
/**
* Trigger a karma-earning action for a user
*
* @param string $issuerCode The issuer code for the karma
* @param string $issuerActionCode The issuer action code for the karma
* @param ExtbaseFrontendUser $frontendUser The frontend user earning the karma
* @throws \Exception If the $issuerCode, $issuerActionCode, or karma source code is not defined in TypoScript.
*/
public function triggerKarmaIssuerActionForUser($issuerCode, $issuerActionCode, ExtbaseFrontendUser $frontendUser)
{
if (!array_key_exists($issuerCode, $this->settings['issuers'])) {
throw new \Exception('Issuer code "' . $issuerCode . '" does not exist.', 1549968187);
}
if (!array_key_exists($issuerActionCode, $this->settings['issuers'][$issuerCode])) {
throw new \Exception('Issuer action code "' . $issuerActionCode . '" does not exist.', 1549968249);
}
$issuerActionSettings = $this->settings['issuers'][$issuerCode][$issuerActionCode];
$karmaSourceCode = $issuerActionSettings['sourceCode'];
if ($karmaSourceCode === '') {
throw new \Exception('Empty karma source code supplied. Please configure it in TypoScript.', 1541508560);
}
if (!array_key_exists($karmaSourceCode, $this->settings['sourceCodes'])) {
throw new \Exception('Karma source code "' . $karmaSourceCode . '" does not exist.', 1549968362);
}
$this->addKarmaToUser(
$issuerActionSettings['valueEarned'],
$frontendUser,
$karmaSourceCode,
$issuerCode,
$issuerActionCode
);
}
/**
* Will take any FrontendUser object and return a karma extension FrontendUser subclass with the data we need
*
......
......@@ -3,11 +3,8 @@
namespace T3o\Karma\Utility;
use In2code\Femanager\Controller\AbstractController as In2CodeAbstractController;
use T3o\Karma\Domain\Repository\FrontendUserRepository;
use T3o\Karma\Service\KarmaService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/***************************************************************
* Copyright notice
......@@ -41,35 +38,6 @@ class UserProfileChangeKarmaIssuerUtility
*/
protected $karmaService;
/**
* @var \T3o\Karma\Domain\Repository\FrontendUserRepository
*/
protected $frontendUserRepository;
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
/**
* @var array
*/
protected $settings;
/**
* UserProfileChangeKarmaIssuerUtility constructor.
*/
public function __construct()
{
$objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
/** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager */
$configurationManager = $objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
$this->settings = $configuration = $configurationManager->getConfiguration(
\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'karma'
);
}
/**
* @param \T3o\Karma\Service\KarmaService $karmaService
*/
......@@ -79,41 +47,18 @@ class UserProfileChangeKarmaIssuerUtility
}
/**
* @param \T3o\Karma\Domain\Repository\FrontendUserRepository $frontendUserRepository
*/
public function injectFrontendUserRepository(FrontendUserRepository $frontendUserRepository)
{
$this->frontendUserRepository = $frontendUserRepository;
}
/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
*/
public function injectObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* Triggered when a new user is created
*
* @param ExtbaseFrontendUser $frontendUser
* @param string $action
* @param In2CodeAbstractController $object
*/
public function newUserWasCreated(ExtbaseFrontendUser $frontendUser)
{
$issuerActionSettings = $this->settings['issuers'][self::ISSUER_CODE][__FUNCTION__];
$karmaSourceCode = $issuerActionSettings['sourceCode'];
if ($karmaSourceCode === '') {
throw new \Exception('Empty karma source code supplied. Please configure it in TypoScript.', 1541508560);
}
$this->karmaService->addKarmaToUser(
$issuerActionSettings['valueEarned'],
$frontendUser,
$issuerActionSettings['sourceCode'],
$this->karmaService->triggerKarmaIssuerActionForUser(
self::ISSUER_CODE,
__FUNCTION__
__FUNCTION__,
$frontendUser
);
}
}
Markdown is supported
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