|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of JoliCode's Slack PHP API project. |
| 7 | + * |
| 8 | + * (c) JoliCode <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace JoliCode\Slack; |
| 15 | + |
| 16 | +use JoliCode\Slack\Api\Client as ApiClient; |
| 17 | + |
| 18 | +class Client extends ApiClient |
| 19 | +{ |
| 20 | + const CURSOR_PAGINATION = [ |
| 21 | + 'channelsList' => 'channels', |
| 22 | + 'conversationsHistory' => 'messages', |
| 23 | + 'conversationsList' => 'channels', |
| 24 | + 'conversationsMembers' => 'members', |
| 25 | + 'conversationsReplies' => 'messages', |
| 26 | + 'filesInfo' => 'comments', |
| 27 | + 'groupsList' => 'groups', |
| 28 | + 'imList' => 'ims', |
| 29 | + 'mpimList' => 'groups', |
| 30 | + 'reactionsList' => 'items', |
| 31 | + 'starsList' => 'items', |
| 32 | + 'usersList' => 'members', |
| 33 | + ]; |
| 34 | + |
| 35 | + public function __call(string $name, $arguments) |
| 36 | + { |
| 37 | + $method = lcfirst(str_replace('iterate', '', $name)); |
| 38 | + |
| 39 | + if (isset(self::CURSOR_PAGINATION[$method])) { |
| 40 | + return $this->iterate($method, $arguments); |
| 41 | + } |
| 42 | + |
| 43 | + throw new \BadMethodCallException(sprintf('Unknown method %s::%s()', self::class, $name)); |
| 44 | + } |
| 45 | + |
| 46 | + public function iterate(string $method, array $arguments): iterable |
| 47 | + { |
| 48 | + $getter = 'get'.self::CURSOR_PAGINATION[$method]; |
| 49 | + |
| 50 | + $arguments[0] = $arguments[0] ?? []; |
| 51 | + $arguments[0]['limit'] = $arguments[0]['limit'] ?? 1000; |
| 52 | + |
| 53 | + $cursor = ''; |
| 54 | + |
| 55 | + do { |
| 56 | + $arguments[0]['cursor'] = $cursor; |
| 57 | + |
| 58 | + $response = $this->{$method}(...$arguments); |
| 59 | + |
| 60 | + foreach ($response->{$getter}() as $item) { |
| 61 | + yield $item; |
| 62 | + } |
| 63 | + |
| 64 | + $cursor = $response->getResponseMetadata() ? $response->getResponseMetadata()->getNextCursor() : ''; |
| 65 | + } while (!empty($cursor)); |
| 66 | + } |
| 67 | +} |
0 commit comments