Commit 46d255b7 authored by Thomas Löffler's avatar Thomas Löffler
Browse files

If Slack user exists, do an update

* Prevents DB bloating in user table
* Fix some CGL
* Better readable code
parent 1e00e463
Pipeline #5235 passed with stages
in 5 minutes and 54 seconds
......@@ -39,17 +39,12 @@ class SlackImportCommandController extends CommandController
* @var \T3o\T3oSlack\Domain\Repository\SlackUserRepository
* @inject
*/
public $slackUserRepository;
protected $slackUserRepository;
/**
* @var array
*/
public $settings;
/**
* @var \T3o\T3oSlack\Domain\Service\SlackApi;
*/
public $slackApi;
protected $settings;
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
......@@ -66,37 +61,41 @@ class SlackImportCommandController extends CommandController
/**
* Import all Slack Users
*
* @return string '1' for success, '0' for error
* @throws \Exception
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/
public function importCommand()
{
$this->getTSConfiguration();
// delete all uses before fresh import
$this->slackUserRepository->removeAll();
$this->persistenceManager->persistAll();
$this->slackApi = GeneralUtility::makeInstance(SlackApi::class);
$slackApi = GeneralUtility::makeInstance(SlackApi::class);
$cursor = '';
$limiter = 0;
$counter = 0;
do {
$response = $this->slackApi->getUsers($cursor);
$response = $slackApi->getUsers($cursor);
if (!$response['ok']) {
throw new \Exception('Slack API error . Reason: ' . $response['error']);
throw new \RuntimeException('Slack API error . Reason: ' . $response['error']);
}
foreach ($response['members'] as $member) {
if ($member['is_bot'] == false) {
$slackUser = GeneralUtility::makeInstance(SlackUser::class);
if ($member['is_bot'] === false) {
$existingSlackUser = $this->slackUserRepository->findOneBySlackId($member['id']);
if ($existingSlackUser) {
$slackUser = $existingSlackUser;
} else {
$slackUser = GeneralUtility::makeInstance(SlackUser::class);
}
$slackUser->setEmail($member['profile']['email']);
$slackUser->setRealName($member['real_name']);
$slackUser->setSlackId($member['id']);
$slackUser->setSlackUserName($member['name']);
$counter++;
$this->slackUserRepository->add($slackUser);
if ($existingSlackUser) {
$this->slackUserRepository->update($slackUser);
} else {
$this->slackUserRepository->add($slackUser);
}
}
}
......@@ -107,12 +106,10 @@ class SlackImportCommandController extends CommandController
}
$this->persistenceManager->persistAll();
$limiter++;
if ($limiter > 999) {
// break loop, if we have more then 999 loops
throw new \Exception('Slack import stopped, due to many loops.');
throw new \RuntimeException('Slack import stopped, due to many loops.');
}
} while ($cursor);
......@@ -120,8 +117,6 @@ class SlackImportCommandController extends CommandController
'Import of Slack User sucessful - imported ' . $counter . ' users',
't3o_slack'
);
return '1';
}
protected function getTSConfiguration()
......
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