1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0
7: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
8: */
9:
10: namespace OpenCloud\Queues\Resource;
11:
12: use OpenCloud\Common\PersistentObject;
13: use OpenCloud\Queues\Exception\DeleteMessageException;
14:
15: /**
16: * A message is a task, a notification, or any meaningful data that gets posted
17: * to the queue. A message exists until it is deleted by a recipient or
18: * automatically by the system based on a TTL (time-to-live) value.
19: */
20: class Message extends PersistentObject
21: {
22:
23: /**
24: * @var string
25: */
26: private $id;
27:
28: /**
29: * The number of seconds since ts, relative to the server's clock.
30: *
31: * @var int
32: */
33: private $age;
34:
35: /**
36: * Defines how long a message will be accessible. The message expires after
37: * ($ttl - $age) seconds.
38: *
39: * @var int
40: */
41: private $ttl = 600;
42:
43: /**
44: * The arbitrary document submitted along with the original request to post
45: * the message.
46: *
47: * @var mixed
48: */
49: private $body;
50:
51: /**
52: * An opaque relative URI that the client can use to uniquely identify a
53: * message resource, and interact with it.
54: *
55: * @var string
56: */
57: private $href;
58:
59: protected static $url_resource = 'messages';
60: protected static $json_collection_name = 'messages';
61: protected static $json_name = '';
62:
63: /**
64: * Set href (and ID).
65: *
66: * @param string $href
67: * @return self
68: */
69: public function setHref($href)
70: {
71: // We have to extract the ID out of the Href. Nice...
72: preg_match('#.+/([\w\d]+)\?claim_id\=.+$#', $href, $match);
73: if (!empty($match)) {
74: $this->setId($match[1]);
75: }
76:
77: $this->href = $href;
78: return $this;
79: }
80:
81: public function createJson()
82: {
83: return (object) array(
84: 'ttl' => $this->getTtl(),
85: 'body' => $this->getBody()
86: );
87: }
88:
89: public function create($params = array())
90: {
91: $this->getLogger()->alert('Please use Queue::createMessage() or Queue::createMessages()');
92: return $this->noCreate();
93: }
94:
95: public function update($params = array())
96: {
97: return $this->noUpdate();
98: }
99:
100: /**
101: * This operation immediately deletes the specified message.
102: *
103: * @param string $claimId Specifies that the message should be deleted
104: * only if it has the specified claim ID, and that claim has not expired.
105: * This is useful for ensuring only one agent processes any given
106: * message. Whenever a worker client's claim expires before it has a
107: * chance to delete a message it has processed, the worker must roll
108: * back any actions it took based on that message because another worker
109: * will now be able to claim and process the same message.
110: *
111: * If you do *not* specify $claimId, but the message is claimed, the
112: * operation fails. You can only delete claimed messages by providing
113: * an appropriate $claimId.
114: *
115: * @return bool
116: * @throws DeleteMessageException
117: */
118: public function delete($claimId = null)
119: {
120: $url = $this->url(null, array('claim_id' => $claimId));
121: $this->getClient()
122: ->delete($url)
123: ->send();
124:
125: return true;
126: }
127:
128: }