wearesho-team/wearesho-notifications-repository

1.1.0 2018-10-16 15:30 UTC

This package is auto-updated.

Last update: 2024-09-26 05:11:44 UTC


README

Build Status codecov

此库代表了您的项目和Wearesho Notifications之间灵活的适配器。您可以创建用户授权令牌,用于通过套接字连接到通知服务器,或将通知推送到它。

设置

composer require wearesho-team/wearesho-notifications-repository

用法

配置

要处理它,您应该创建ConfigInterface。有两个内部实现,但您也可以自行实现。

<?php

/**
 * @var string $requestsUrl         URL to your notification server.
 * @var string|null $serviceKey     Access key to notification server. Optional (depends on server requirements). 
 */

$config = new Wearesho\Notifications\Config($requestsUrl, $serviceKey);

如果您喜欢环境配置,可以使用EnvironmentConfig

WEARESHO_NOTIFICATIONS_URL=https://your.notification.server/
WEARESHO_NOTIFICATIONS_SERVICE_KEY='your personal service key, optional'
<?php

$config = new Wearesho\Notifications\EnvironmentConfig($dotenvPrefix = 'WEARESHO_NOTIFICATIONS_');

创建仓库实例

<?php

/**
 * @var Wearesho\Notifications\ConfigInterface $config
 * @var GuzzleHttp\ClientInterface $guzzleClient
 */

$repository = new Wearesho\Notifications\Repository($config, $guzzleClient);

授权

此方法接受用户ID,并返回用于连接的授权令牌

<?php

/**
 * @var Wearesho\Notifications\Repository $repository
 * @var int $userId 
 */

try {
    $authorizationToken = $repository->authorize($userId);
} catch (Wearesho\Notifications\Exceptions\Credentials\Missed $exception) {
    // Your server requires service key, but you have not passed it in config
} catch (Wearesho\Notifications\Exceptions\Credentials\Invalid $exception) {
    // Your service key is invalid
} catch (Wearesho\Notifications\Exceptions\InvalidResponse $exception) {
    // Unexpected service response.
    // You can receive response instance using $exception->getResponse()
}

推送通知

首先您需要创建一个通知实体

<?php

/**
 * @var int $userId             Notification's owner
 * @var string $message         Notification's content
 * @var array|null $context     Special params for message.
 * F.e. if message is like 'Hello, {person}', you can pass params like [ 'person' => 'Jonh', ]
 * This params can be applied in front-end
 * 
 * @var string|null $type       Notification type.
 * Can be any string. but we recommend to use Wearesho\Notifications\Notification\Type constants
 * to avoid unexpected situations.
 *
 * @var \DateTime|null $time    Notification's creation date
 * @var bool|null $isRead       Mark if the notification is read.
 */

$notification = new Wearesho\Notifications\Notification(
    $userId,
    $message,
    $context,
    $type,
    $time,
    $isRead
);

然后将它传递给仓库

<?php

/**
 * @var Wearesho\Notifications\Notification $notification
 * @var Wearesho\Notifications\Repository $repository
 */

try {
    $repository->push($notification);
} catch (Wearesho\Notifications\Exceptions\Credentials\Missed $exception) {
    // Your server requires service key, but you have not passed it in config
} catch (Wearesho\Notifications\Exceptions\Credentials\Invalid $exception) {
    // Your service key is invalid
} catch (Wearesho\Notifications\Exceptions\InvalidNotification $exception) {
    // You have been formed invalid notification
    // You can receive it using $exception->getNotification()
}

推送链

您可以使用Push\Chain将通知推送到不同的通知服务器。它与Push\Filter一起使用非常方便。

<?php

use Wearesho\Notifications;

/** @var Notifications\Repository $repoFirst */
/** @var Notifications\Repository $repoSecond */

$chain = new Notifications\Push\Chain([
    new Notifications\Push\Filter(
        $repoFirst,
        $types = [
            'primary',
        ]
    ),
    $repoSecond
]);

/** @var Notifications\Notification $notification */

$chain->push($notification);

在此示例中,如果通知具有primary类型,则将通知推送到第一个和第二个仓库;如果通知具有其他类型,则仅推送到第二个仓库。