<?php

namespace Kylin\Wechat\Traits;

use EasyWeChat\OpenPlatform\Application;
use Psr\Http\Message\ResponseInterface;
use Kylin\Wechat\Events\OpenPlatform\Authorized;
use Kylin\Wechat\Events\OpenPlatform\AuthorizeUpdated;
use Kylin\Wechat\Events\OpenPlatform\Unauthorized;
use Kylin\Wechat\Events\OpenPlatform\VerifyTicketRefreshed;

/**
 * 处理开放平台服务器事件的特性
 */
Trait HandleOpenPlatformServerEvents
{
    /**
     * 处理开放平台服务器事件
     */
    public function handleServerEvents(Application $application): ResponseInterface
    {
        $server = $application->server;

        $server->on('authorized', fn ($message) => event(new Authorized($message)));

        $server->on('authorize_updated', fn ($message) => event(new AuthorizeUpdated($message)));

        $server->on('unauthorized', fn ($message) => event(new Unauthorized($message)));

        $server->on('component_verify_ticket', fn ($message) => event(new VerifyTicketRefreshed($message)));

        return $server->serve();
    }
}