PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/notifications/prowl/Message.php

http://github.com/spotweb/spotweb
PHP | 323 lines | 113 code | 36 blank | 174 comment | 15 complexity | 4cf0615e442d0b47b9743e2a00e5ebbf MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Copyright [2011] [Mario Mueller]
  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. namespace Prowl {
  18. /**
  19. * Prowl Message
  20. *
  21. * This class represents a single message
  22. * to be send by the connector.
  23. *
  24. * @author Mario Mueller <mario.mueller.work at gmail.com>
  25. * @version 1.0.0
  26. * @package Prowl
  27. * @subpackage Message
  28. */
  29. class Message {
  30. /**
  31. * Your API keys. Please use the
  32. * setter to modify this.
  33. * @var array
  34. */
  35. private $aApiKeys = array();
  36. /**
  37. * A priority value from -2 to 2
  38. * @var integer
  39. */
  40. private $iPriority = 0;
  41. /**
  42. * The application identifier.
  43. * @var string
  44. */
  45. private $sApplication = 'ProwlPHP';
  46. /**
  47. * The event title.
  48. * @var string
  49. */
  50. private $sEvent = null;
  51. /**
  52. * The event description.
  53. * @var string
  54. */
  55. private $sDescription = null;
  56. /**
  57. * Filter instance. This one is
  58. * passed from the connection on push, if the message
  59. * has no filter set.
  60. * @var \Prowl\Security\Secureable
  61. */
  62. private $oFilterIntance = null;
  63. /**
  64. * An alternative way to filter. You can set a
  65. * closure instead of a filter instance. If both are
  66. * set, the closure will be preferred.
  67. *
  68. * @var \Closure
  69. */
  70. private $cFilterCallback = null;
  71. /**
  72. * An Url to send with the message for redirecting.
  73. */
  74. private $sUrl = null;
  75. /**
  76. * Sets an Url to be sent with the message.
  77. * @throws \InvalidArgumentException
  78. * @param string $sUrl
  79. * @return void
  80. */
  81. public function setUrl($sUrl) {
  82. $sUrl = filter_var($sUrl, FILTER_VALIDATE_URL);
  83. if (!$sUrl) {
  84. throw new \InvalidArgumentException("Given url [$sUrl] did not pass the validation.");
  85. }
  86. $this->sUrl = (string)$sUrl;
  87. }
  88. /**
  89. * Returns the Url that should be sent with the message
  90. * @return string
  91. */
  92. public function getUrl() {
  93. return $this->sUrl;
  94. }
  95. /**
  96. * Set a filter instance. If you do not need a filter, use the
  97. * Passthrough filter.
  98. *
  99. * @param \Prowl\Security\Secureable $oFilterInstance
  100. * @return \Prowl\Message
  101. */
  102. public function setFilter(\Prowl\Security\Secureable $oFilterInstance) {
  103. $this->oFilterIntance = $oFilterInstance;
  104. return $this;
  105. }
  106. /**
  107. * Returns the filter instance, if set. It might return null
  108. * when no filter is set.
  109. *
  110. * @return \Prowl\Security\Secureable
  111. */
  112. public function getFilter() {
  113. return $this->oFilterIntance;
  114. }
  115. /**
  116. * An alternative way to filter. You can set a
  117. * closure instead of a filter instance. If both are
  118. * set, the closure will be preferred.
  119. * @param \Closure $cCallback
  120. * @return void
  121. */
  122. public function setFilterCallback(\Closure $cCallback) {
  123. $this->cFilterCallback = $cCallback;
  124. }
  125. /**
  126. * Getter for the filter closure.
  127. * @return \Closure
  128. */
  129. public function getFilterCallback() {
  130. return $this->cFilterCallback;
  131. }
  132. /**
  133. * Sets the event.
  134. * @throws InvalidArgumentException
  135. * @param string $sEvent The event.
  136. * @return \Prowl\Message
  137. */
  138. public function setEvent($sEvent) {
  139. $iContentLength = mb_strlen($sEvent, 'utf-8');
  140. if ($iContentLength > 1024) {
  141. throw new \InvalidArgumentException('Event length is limited to 1024 chars. Yours is ' . $iContentLength);
  142. }
  143. $this->sEvent = (string)$sEvent;
  144. return $this;
  145. }
  146. /**
  147. * Returns the event.
  148. * @return string
  149. */
  150. public function getEvent() {
  151. return $this->sEvent;
  152. }
  153. /**
  154. * Sets the application.
  155. * @throws \InvalidArgumentException
  156. * @param string $sApp The name of the sending application.
  157. * @return \Prowl\Message
  158. */
  159. public function setApplication($sApp) {
  160. $iContentLength = mb_strlen($sApp, 'utf-8');
  161. if ($iContentLength > 254) {
  162. throw new \InvalidArgumentException('Application length is limited to 254 chars. Yours is ' . $iContentLength);
  163. }
  164. $this->sApplication = (string)$sApp;
  165. return $this;
  166. }
  167. /**
  168. * Returns the application string.
  169. *
  170. * @return string
  171. */
  172. public function getApplication() {
  173. return $this->sApplication;
  174. }
  175. /**
  176. * Sets the event description.
  177. *
  178. * @throws \InvalidArgumentException
  179. * @param string $sDescription The event description.
  180. * @return \Prowl\Message
  181. */
  182. public function setDescription($sDescription) {
  183. $iContentLength = mb_strlen($sDescription, 'utf-8');
  184. if ($iContentLength > 10000) {
  185. throw new \InvalidArgumentException('Description is too long. Limit is 10.000, yours is ' . $iContentLength);
  186. }
  187. $this->sDescription = (string)$sDescription;
  188. return $this;
  189. }
  190. /**
  191. * Returns the description.
  192. *
  193. * @return string
  194. */
  195. public function getDescription() {
  196. return $this->sDescription;
  197. }
  198. /**
  199. * Sets the api key.
  200. * This method uses a fluent interface.
  201. *
  202. * @throws \InvalidArgumentException
  203. * @param string $sKey An valid api key.
  204. * @return \Prowl\Message
  205. */
  206. public function addApiKey($sKey) {
  207. if (is_string($sKey)) {
  208. $this->aApiKeys[] = (string)$sKey;
  209. } else {
  210. throw new \InvalidArgumentException('The param was not a string.');
  211. }
  212. return $this;
  213. } // function
  214. /**
  215. * Removes an api key from the receiver list.
  216. *
  217. * @throws \OutOfRangeException
  218. * @param string $sKey
  219. * @return \Prowl\Message
  220. */
  221. public function removeApiKey($sKey) {
  222. $iIndex = array_search($sKey, $this->aApiKeys);
  223. if ($iIndex === false) {
  224. throw new \OutOfRangeException('This API key does not exist in list.');
  225. } else {
  226. unset($this->aApiKeys[$iIndex]);
  227. }
  228. return $this;
  229. }
  230. /**
  231. * Returns all actual api keys as array.
  232. *
  233. * @return array[string]
  234. */
  235. public function getApiKeysAsArray() {
  236. return $this->aApiKeys;
  237. }
  238. /**
  239. * Returns all actual api keys as string
  240. *
  241. * @return string
  242. */
  243. public function getApiKeysAsString() {
  244. return implode(',', $this->getApiKeysAsArray());
  245. }
  246. /**
  247. * Sets the proirity (-2 to 2)
  248. * This method uses a fluent interface.
  249. *
  250. * @throws \InvalidArgumentException
  251. * @param integer $iPriority An signed integer from -2 to 2
  252. * @return \Prowl\Message
  253. */
  254. public function setPriority($iPriority) {
  255. $mVal = filter_var($iPriority, FILTER_VALIDATE_INT);
  256. if (($mVal !== false) && ($mVal >= -2) && ($mVal <= 2)) {
  257. $this->iPriority = $mVal;
  258. } else {
  259. throw new \InvalidArgumentException('The param was not between -2 and 2 or even an integer.');
  260. }
  261. return $this;
  262. }
  263. /**
  264. * Returns the priority as signed integer
  265. *
  266. * @return integer
  267. */
  268. public function getPriority() {
  269. return $this->iPriority;
  270. }
  271. /**
  272. * Validates the basic needs of the prowl api.
  273. *
  274. * @throws \InvalidArgumentException
  275. * @return boolean
  276. */
  277. public function validate() {
  278. if (is_null($this->sEvent)) {
  279. throw new \InvalidArgumentException('Validation Error: Event is missing');
  280. }
  281. if (sizeof($this->aApiKeys) == 0) {
  282. throw new \InvalidArgumentException('Validation Error: No api keys present.');
  283. }
  284. return true;
  285. }
  286. }
  287. }