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 * @var string
33 */
34 private $id;
35
36 /**
37 * The number of seconds since ts, relative to the server's clock.
38 *
39 * @var int
40 */
41 private $age;
42
43 /**
44 * Defines how long a message will be accessible. The message expires after
45 * ($ttl - $age) seconds.
46 *
47 * @var int
48 */
49 private $ttl = 600;
50
51 /**
52 * The arbitrary document submitted along with the original request to post
53 * the message.
54 *
55 * @var mixed
56 */
57 private $body;
58
59 /**
60 * An opaque relative URI that the client can use to uniquely identify a
61 * message resource, and interact with it.
62 *
63 * @var string
64 */
65 private $href;
66
67 protected static $url_resource = 'messages';
68 protected static $json_collection_name = 'messages';
69 protected static $json_name = '';
70
71 /**
72 * Set href (and ID).
73 *
74 * @param string $href
75 * @return self
76 */
77 public function setHref($href)
78 {
79 // We have to extract the ID out of the Href. Nice...
80 preg_match('#.+/([\w]+)#', $href, $match);
81 if (!empty($match)) {
82 $this->setId($match[1]);
83 }
84
85 $this->href = $href;
86
87 return $this;
88 }
89
90 /**
91 * @return string
92 */
93 public function getHref()
94 {
95 return $this->href;
96 }
97
98 public function createJson()
99 {
100 return (object) array(
101 'ttl' => $this->getTtl(),
102 'body' => $this->getBody()
103 );
104 }
105
106 public function create($params = array())
107 {
108 $this->getLogger()->alert('Please use Queue::createMessage() or Queue::createMessages()');
109
110 return $this->noCreate();
111 }
112
113 public function update($params = array())
114 {
115 return $this->noUpdate();
116 }
117
118 /**
119 * This operation immediately deletes the specified message.
120 *
121 * @param string $claimId Specifies that the message should be deleted
122 * only if it has the specified claim ID, and that claim has not expired.
123 * This is useful for ensuring only one agent processes any given
124 * message. Whenever a worker client's claim expires before it has a
125 * chance to delete a message it has processed, the worker must roll
126 * back any actions it took based on that message because another worker
127 * will now be able to claim and process the same message.
128 *
129 * If you do *not* specify $claimId, but the message is claimed, the
130 * operation fails. You can only delete claimed messages by providing
131 * an appropriate $claimId.
132 *
133 * @return bool
134 * @throws DeleteMessageException
135 */
136 public function delete($claimId = null)
137 {
138 $url = $this->url(null, array('claim_id' => $claimId));
139 $this->getClient()
140 ->delete($url)
141 ->send();
142
143 return true;
144 }
145
146 /**
147 * If this message has been claimed, retrieve the claim id.
148 *
149 * @return string
150 */
151 public function getClaimIdFromHref()
152 {
153 $url = Url::factory($this->href);
154
155 return $url->getQuery()->get('claim_id');
156 }
157 }
158