1
<?php
2

3
declare(strict_types=1);
4

5
namespace Kreait\Firebase\Messaging;
6

7
use GuzzleHttp\ClientInterface;
8
use Kreait\Firebase\Exception\FirebaseException;
9
use Kreait\Firebase\Exception\MessagingApiExceptionConverter;
10
use Kreait\Firebase\Exception\MessagingException;
11
use Psr\Http\Message\ResponseInterface;
12
use Throwable;
13

14
/**
15
 * @internal
16
 */
17
class AppInstanceApiClient
18
{
19
    /** @var ClientInterface */
20
    private $client;
21

22
    /** @var MessagingApiExceptionConverter */
23
    private $errorHandler;
24

25
    /**
26
     * @internal
27
     */
28 12
    public function __construct(ClientInterface $client)
29
    {
30 12
        $this->client = $client;
31 12
        $this->errorHandler = new MessagingApiExceptionConverter();
32
    }
33

34
    /**
35
     * @param Topic|string $topic
36
     * @param RegistrationToken[]|string[] $tokens
37
     *
38
     * @throws FirebaseException
39
     * @throws MessagingException
40
     */
41 12
    public function subscribeToTopic($topic, array $tokens): ResponseInterface
42
    {
43 12
        return $this->requestApi('POST', '/iid/v1:batchAdd', [
44
            'json' => [
45 12
                'to' => '/topics/'.$topic,
46 12
                'registration_tokens' => $tokens,
47
            ],
48
        ]);
49
    }
50

51
    /**
52
     * @param Topic|string $topic
53
     * @param RegistrationToken[]|string[] $tokens
54
     *
55
     * @throws FirebaseException
56
     * @throws MessagingException
57
     */
58 3
    public function unsubscribeFromTopic($topic, array $tokens): ResponseInterface
59
    {
60 3
        return $this->requestApi('POST', '/iid/v1:batchRemove', [
61
            'json' => [
62 3
                'to' => '/topics/'.$topic,
63 3
                'registration_tokens' => $tokens,
64
            ],
65
        ]);
66
    }
67

68
    /**
69
     * @throws FirebaseException
70
     * @throws MessagingException
71
     */
72 3
    public function getAppInstance(string $registrationToken): ResponseInterface
73
    {
74 3
        return $this->requestApi('GET', '/iid/'.$registrationToken.'?details=true');
75
    }
76

77
    /**
78
     * @param array<string, mixed> $options
79
     *
80
     * @throws FirebaseException
81
     * @throws MessagingException
82
     */
83 12
    private function requestApi(string $method, string $endpoint, ?array $options = null): ResponseInterface
84
    {
85
        try {
86 12
            return $this->client->request($method, $endpoint, $options ?? []);
87 12
        } catch (Throwable $e) {
88 12
            throw $this->errorHandler->convertException($e);
89
        }
90
    }
91
}

Read our documentation on viewing source code .

Loading