Google\Cloud\PubSub\Subscription::create PHP Method

create() public method

The suggested way of creating a subscription is by calling through {@see \Google\Cloud\PubSub\Topic::subscribe()} or {@see \Google\Cloud\PubSub\Topic::subscription()}. Returns subscription info in the format detailed in the documentation for a subscription. **NOTE: Some methods of instantiation of a Subscription do not supply a topic name. The topic name is required to create a subscription.** Example: $topic = $pubsub->topic('my-new-topic'); $subscription = $topic->subscription('my-new-subscription'); $result = $subscription->create();
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/create Create Subscription
public create ( array $options = [] ) : array
$options array [optional] { Configuration Options @type int $ackDeadlineSeconds This value is the maximum time after a subscriber receives a message before the subscriber should acknowledge the message. **Defaults to** `10`. @type array $pushConfig See {@see \Google\Cloud\PubSub\Subscription::modifyPushConfig()} or [PushConfig](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions#PushConfig) for usage. }
return array An array of subscription info
    public function create(array $options = [])
    {
        // If a subscription is created via PubSubClient::subscription(),
        // it may or may not have a topic name. This is fine for most API
        // interactions, but a topic name is required to create a subscription.
        if (!$this->topicName) {
            throw new InvalidArgumentException('A topic name is required to
                create a subscription.');
        }
        $this->info = $this->connection->createSubscription($options + ['name' => $this->name, 'topic' => $this->topicName]);
        return $this->info;
    }

Usage Example

コード例 #1
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testCreateWithoutTopicName()
 {
     $subscription = new Subscription($this->connection->reveal(), 'project-id', 'subscription-name', null, true);
     $sub = $subscription->create();
 }