SlackImportCommandController.php 4.73 KB
Newer Older
1
2
3
4
<?php
namespace T3o\T3oSlack\Command;

use T3o\T3oSlack\Domain\Model\SlackUser;
5
use T3o\T3oSlack\Domain\Service\SlackApi;
6
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
7
8
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

/***************************************************************
 *  Copyright notice
 *
 *  (c) 2017
 *  All rights reserved
 *
 * @author     Stefan Busemann <stefan@in2code.de>
 * @package    TYPO3
 * @subpackage t3o_slack
 *
 *  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!
 ***************************************************************/
36
class SlackImportCommandController extends CommandController implements \Psr\Log\LoggerAwareInterface
37
{
38
39
    use \Psr\Log\LoggerAwareTrait;

40
41
42
43
    /**
     * @var \T3o\T3oSlack\Domain\Repository\SlackUserRepository
     * @inject
     */
44
    protected $slackUserRepository;
45
46
47
48

    /**
     * @var array
     */
49
    protected $settings;
50
51

    /**
52
53
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
     * @inject
54
     */
55
    protected $configurationManager;
56
57
58
59
60
61
62
63
64

    /**
     * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
     * @inject
     */
    protected $persistenceManager;

    /**
     * Import all Slack Users
65
     *
66
67
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
68
69
70
     */
    public function importCommand()
    {
71
72
        $this->getTSConfiguration();

73
        $slackApi = GeneralUtility::makeInstance(SlackApi::class);
74
        $cursor = '';
75
        $limiter = 0;
76
        $counter = 0;
77
        do {
78
            $response = $slackApi->getUsers($cursor);
79
            if (!$response['ok']) {
80
                throw new \RuntimeException('Slack API error . Reason: ' . $response['error']);
81
            }
82

83
            foreach ($response['members'] as $member) {
84
85
86
87
88
89
90
                if ($member['is_bot'] === false) {
                    $existingSlackUser = $this->slackUserRepository->findOneBySlackId($member['id']);
                    if ($existingSlackUser) {
                        $slackUser = $existingSlackUser;
                    } else {
                        $slackUser = GeneralUtility::makeInstance(SlackUser::class);
                    }
91
92
93
94
95
                    $slackUser->setEmail($member['profile']['email']);
                    $slackUser->setRealName($member['real_name']);
                    $slackUser->setSlackId($member['id']);
                    $slackUser->setSlackUserName($member['name']);
                    $counter++;
96
97
98
99
100
                    if ($existingSlackUser) {
                        $this->slackUserRepository->update($slackUser);
                    } else {
                        $this->slackUserRepository->add($slackUser);
                    }
101
102
                }
            }
103
104
105
106
107

            if ($response['response_metadata']['next_cursor']) {
                $cursor = $response['response_metadata']['next_cursor'];
            } else {
                $cursor = '';
108
109
            }

110
111
112
113
            $this->persistenceManager->persistAll();
            $limiter++;
            if ($limiter > 999) {
                // break loop, if we have more then 999 loops
114
                throw new \RuntimeException('Slack import stopped, due to many loops.');
115
            }
116
        } while ($cursor);
117

Thomas Löffler's avatar
Thomas Löffler committed
118
        $this->logger = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__);
119
        $this->logger->info('Import of Slack User sucessful - imported ' . $counter . ' users');
120
    }
121

122
123
124
125
    protected function getTSConfiguration()
    {
        // load complete ts
        $this->settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
126

127
128
129
130
131
        // remove dots from configuration
        $typoscriptService = GeneralUtility::makeInstance(TypoScriptService::class);

        $this->settings = $typoscriptService->convertTypoScriptArrayToPlainArray($this->settings);
        $this->settings = $this->settings['plugin']['tx_t3oslack'];
132
133
    }
}