1: <?php
2: /**
3: * Copyright 2012-2014 Rackspace US, Inc.
4: *
5: * Licensed under the Apache License, Version 2.0 (the "License");
6: * you may not use this file except in compliance with the License.
7: * You may obtain a copy of the License at
8: *
9: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: namespace OpenCloud\Queues\Resource;
19:
20: use Guzzle\Http\Url;
21: use OpenCloud\Common\PersistentObject;
22: use OpenCloud\Queues\Exception\DeleteMessageException;
23:
24: /**
25: * A message is a task, a notification, or any meaningful data that gets posted
26: * to the queue. A message exists until it is deleted by a recipient or
27: * automatically by the system based on a TTL (time-to-live) value.
28: */
29: class Message extends PersistentObject
30: {
31:
32: /**
33: * @var string
34: */
35: private $id;
36:
37: /**
38: * The number of seconds since ts, relative to the server's clock.
39: *
40: * @var int
41: */
42: private $age;
43:
44: /**
45: * Defines how long a message will be accessible. The message expires after
46: * ($ttl - $age) seconds.
47: *
48: * @var int
49: */
50: private $ttl = 600;
51:
52: /**
53: * The arbitrary document submitted along with the original request to post
54: * the message.
55: *
56: * @var mixed
57: */
58: private $body;
59:
60: /**
61: * An opaque relative URI that the client can use to uniquely identify a
62: * message resource, and interact with it.
63: *
64: * @var string
65: */
66: private $href;
67:
68: protected static $url_resource = 'messages';
69: protected static $json_collection_name = 'messages';
70: protected static $json_name = '';
71:
72: /**
73: * Set href (and ID).
74: *
75: * @param string $href
76: * @return self
77: */
78: public function setHref($href)
79: {
80: // We have to extract the ID out of the Href. Nice...
81: preg_match('#.+/([\w\d]+)\?claim_id\=.+$#', $href, $match);
82: if (!empty($match)) {
83: $this->setId($match[1]);
84: }
85:
86: $this->href = $href;
87:
88: return $this;
89: }
90:
91: /**
92: * @return string
93: */
94: public function getHref()
95: {
96: return $this->href;
97: }
98:
99: public function createJson()
100: {
101: return (object) array(
102: 'ttl' => $this->getTtl(),
103: 'body' => $this->getBody()
104: );
105: }
106:
107: public function create($params = array())
108: {
109: $this->getLogger()->alert('Please use Queue::createMessage() or Queue::createMessages()');
110:
111: return $this->noCreate();
112: }
113:
114: public function update($params = array())
115: {
116: return $this->noUpdate();
117: }
118:
119: /**
120: * This operation immediately deletes the specified message.
121: *
122: * @param string $claimId Specifies that the message should be deleted
123: * only if it has the specified claim ID, and that claim has not expired.
124: * This is useful for ensuring only one agent processes any given
125: * message. Whenever a worker client's claim expires before it has a
126: * chance to delete a message it has processed, the worker must roll
127: * back any actions it took based on that message because another worker
128: * will now be able to claim and process the same message.
129: *
130: * If you do *not* specify $claimId, but the message is claimed, the
131: * operation fails. You can only delete claimed messages by providing
132: * an appropriate $claimId.
133: *
134: * @return bool
135: * @throws DeleteMessageException
136: */
137: public function delete($claimId = null)
138: {
139: $url = $this->url(null, array('claim_id' => $claimId));
140: $this->getClient()
141: ->delete($url)
142: ->send();
143:
144: return true;
145: }
146:
147: /**
148: * If this message has been claimed, retrieve the claim id.
149: *
150: * @return string
151: */
152: public function getClaimIdFromHref()
153: {
154: $url = Url::factory($this->href);
155:
156: return $url->getQuery()->get('claim_id');
157: }
158: }
159: