PHP code example of ozean12 / googlepubsub

1. Go to this page and download the library: Download ozean12/googlepubsub library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ozean12 / googlepubsub example snippets


// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...
        new JMS\SerializerBundle\JMSSerializerBundle(), // if not already enabled
        new Ozean12\WebTranslateItBundle\Ozean12WebTranslateItBundle(),
    ];
    
    // ...
}

$message = new MyTopicMessage();
$publisher = $this->container->get('ozean12_google_pubsub.publisher.my_topic');
$result = $publisher->publish($message);

// GoogleCloudController.php

use Ozean12\GooglePubSubBundle\DTO\PushMessageRequestDTO;

/**
 * Class GoogleCloudController
 *
 * @Version("v1")
 * @Route("/google-cloud/")
 */
class GoogleCloudController extends FOSRestController
{
    /**
     * @ApiDoc(
     *   resource = true,
     *   description = "Receive a new PubSub message and process it",
     *   statusCodes = {
     *     204 = "Returned when successful",
     *     403 = "Returned when access denied"
     *   }
     * )
     *
     * @ParamConverter("message", converter="fos_rest.request_body")
     *
     * @Route("pub-sub")
     * @Method({"POST"})
     * @View()
     *
     * @param PushMessageRequestDTO $message
     * @return mixed
     */
    public function pubSub(PushMessageRequestDTO $message)
    {
        $this->get('ozean12_google_pubsub.push_subscriber_manager.service')->processMessage($message);
    }
}

// MyPushSubscriber.php

use Ozean12\GooglePubSubBundle\DTO\PushMessageDTO;
use Ozean12\GooglePubSubBundle\Service\Subscriber\PushSubscriberInterface;

/**
 * Class MyPushSubscriber
 */
class MyPushSubscriber implements PushSubscriberInterface
{
    /**
     * {@inheritdoc}
     */
    public function process(PushMessageDTO $message)
    {
        /** @var SendTransactionalEmailDataDTO $emailDTO */
        $data = base64_decode($message->getData())

        // process your data here
    }
}