-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAWSQueue.php
127 lines (109 loc) · 2.82 KB
/
AWSQueue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* AWSQueue
*/
class AWSQueue extends CModel
{
/*
* @var AWSQueueManager
*/
private static $sqs;
/**
* @var string name of the queue accepts letter, numbers, - and _
*/
private $_name;
/**
* @var string url of the queue
*/
private $_url;
public function __construct($url=null)
{
if($url!==null)
$this->url = $url;
}
/**
* @return string name of the queue
*/
public function getName()
{
return $this->_name;
}
/**
* @return string url of the queue
*/
public function getUrl()
{
return $this->_url;
}
/**
* @param string $url url of the queue
*/
public function setUrl($url)
{
$p = parse_url($url);
if(isset($p['path'])) {
$path = explode('/',$p['path']);
$this->_name = array_pop($path);
}
$this->_url=$url;
}
/**
* @return array attributes of queue
*/
public function attributeNames()
{
return array('name','url');
}
/**
* @return AWSQueueManager the sqs application component
*/
private function sqs()
{
if(self::$sqs!==null)
return self::$sqs;
else {
self::$sqs=Yii::app()->sqs;
if(self::$sqs instanceof AWSQueueManager)
return self::$sqs;
else
throw new CException(Yii::t('yii','AWSQueue requires a "sqs" AWSQueueManager application component.'));
}
}
/**
* @param $message mixed message to add to the queue
* @param $options array options for this message
* @return boolean if added with success
*/
public function send($message, $options=array())
{
if($this->_url!==null)
return (boolean)$this->sqs()->send($this->url, (string)$message, $options);
return false;
}
public function sendBatch($messageArray, $options=array())
{
return $this->sqs()->sendBatch($this->url, $messageArray, $options);
}
public function receiveBatch($items=10, $options=array())
{
$options['MaxNumberOfMessages']=$items;
return $this->sqs()->receive($this->url, $options);
}
public function receive($options=array())
{
if($this->_url!==null)
return $this->sqs()->receive($this->url, $options);
return null;
}
public function delete($handle, $options=array())
{
if($this->_url!==null)
return (boolean)$this->sqs()->delete($this->_url, $handle, $options);
return false;
}
public function deleteBatch($handles, $options = array()){
if($this->_url!==null)
return (boolean)$this->sqs()->deleteBatch($this->_url, $handles, $options);
return false;
}
}