|
106 | 106 | return new Response('', 400);
|
107 | 107 | });
|
108 | 108 |
|
| 109 | +$app->post('/create_topic_and_subscription', function () use ($app) { |
| 110 | + /** @var Google_Client $client */ |
| 111 | + $client = $app['google_client']; |
| 112 | + $projectName = sprintf('projects/%s', $app['project_id']); |
| 113 | + $pubsub = new Google_Service_Pubsub($client); |
| 114 | + $topic = new Google_Service_Pubsub_Topic(); |
| 115 | + $topic->setName(sprintf('%s/topics/%s', $projectName, $app['topic'])); |
| 116 | + try { |
| 117 | + $pubsub->projects_topics->create($topic->getName(), $topic); |
| 118 | + } catch (Google_Service_Exception $e) { |
| 119 | + // 409 is ok. The topic already exists. |
| 120 | + if ($e->getCode() != 409) { |
| 121 | + throw $e; |
| 122 | + } |
| 123 | + } |
| 124 | + $subscription = new Google_Service_Pubsub_Subscription(); |
| 125 | + $subscription->setName(sprintf( |
| 126 | + '%s/subscriptions/%s', |
| 127 | + $projectName, |
| 128 | + $app['topic'] |
| 129 | + )); |
| 130 | + $subscription->setTopic($topic->getName()); |
| 131 | + $config = new Google_Service_Pubsub_PushConfig(); |
| 132 | + $project_id = $app['project_id']; |
| 133 | + $config->setPushEndpoint("https://$project_id.appspot.com/receive_message"); |
| 134 | + $subscription->setPushConfig($config); |
| 135 | + try { |
| 136 | + $pubsub->projects_subscriptions->create( |
| 137 | + $subscription->getName(), |
| 138 | + $subscription |
| 139 | + ); |
| 140 | + } catch (Google_Service_Exception $e) { |
| 141 | + // 409 is ok. The subscription already exists. |
| 142 | + if ($e->getCode() != 409) { |
| 143 | + throw $e; |
| 144 | + } |
| 145 | + } |
| 146 | + return 'OK'; |
| 147 | +}); |
| 148 | + |
109 | 149 | return $app;
|
0 commit comments