PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/private/Notification/Notification.php

https://gitlab.com/wuhang2003/core
PHP | 453 lines | 214 code | 59 blank | 180 comment | 35 complexity | 17fc1e64f52aedacda5335b365ca02bd MD5 | raw file
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Notification;
  23. use OCP\Notification\IAction;
  24. use OCP\Notification\INotification;
  25. class Notification implements INotification {
  26. /** @var string */
  27. protected $app;
  28. /** @var string */
  29. protected $user;
  30. /** @var \DateTime */
  31. protected $dateTime;
  32. /** @var string */
  33. protected $objectType;
  34. /** @var string */
  35. protected $objectId;
  36. /** @var string */
  37. protected $subject;
  38. /** @var array */
  39. protected $subjectParameters;
  40. /** @var string */
  41. protected $subjectParsed;
  42. /** @var string */
  43. protected $message;
  44. /** @var array */
  45. protected $messageParameters;
  46. /** @var string */
  47. protected $messageParsed;
  48. /** @var string */
  49. protected $link;
  50. /** @var string */
  51. protected $icon;
  52. /** @var array */
  53. protected $actions;
  54. /** @var array */
  55. protected $actionsParsed;
  56. /** @var bool */
  57. protected $hasPrimaryAction;
  58. /** @var bool */
  59. protected $hasPrimaryParsedAction;
  60. /**
  61. * Constructor
  62. */
  63. public function __construct() {
  64. $this->app = '';
  65. $this->user = '';
  66. $this->dateTime = new \DateTime();
  67. $this->dateTime->setTimestamp(0);
  68. $this->objectType = '';
  69. $this->objectId = '';
  70. $this->subject = '';
  71. $this->subjectParameters = [];
  72. $this->subjectParsed = '';
  73. $this->message = '';
  74. $this->messageParameters = [];
  75. $this->messageParsed = '';
  76. $this->link = '';
  77. $this->icon = '';
  78. $this->actions = [];
  79. $this->actionsParsed = [];
  80. }
  81. /**
  82. * @param string $app
  83. * @return $this
  84. * @throws \InvalidArgumentException if the app id is invalid
  85. * @since 8.2.0
  86. */
  87. public function setApp($app) {
  88. if (!is_string($app) || $app === '' || isset($app[32])) {
  89. throw new \InvalidArgumentException('The given app name is invalid');
  90. }
  91. $this->app = $app;
  92. return $this;
  93. }
  94. /**
  95. * @return string
  96. * @since 8.2.0
  97. */
  98. public function getApp() {
  99. return $this->app;
  100. }
  101. /**
  102. * @param string $user
  103. * @return $this
  104. * @throws \InvalidArgumentException if the user id is invalid
  105. * @since 8.2.0
  106. */
  107. public function setUser($user) {
  108. if (!is_string($user) || $user === '' || isset($user[64])) {
  109. throw new \InvalidArgumentException('The given user id is invalid');
  110. }
  111. $this->user = $user;
  112. return $this;
  113. }
  114. /**
  115. * @return string
  116. * @since 8.2.0
  117. */
  118. public function getUser() {
  119. return $this->user;
  120. }
  121. /**
  122. * @param \DateTime $dateTime
  123. * @return $this
  124. * @throws \InvalidArgumentException if the $dateTime is invalid
  125. * @since 9.0.0
  126. */
  127. public function setDateTime(\DateTime $dateTime) {
  128. if ($dateTime->getTimestamp() === 0) {
  129. throw new \InvalidArgumentException('The given date time is invalid');
  130. }
  131. $this->dateTime = $dateTime;
  132. return $this;
  133. }
  134. /**
  135. * @return \DateTime
  136. * @since 9.0.0
  137. */
  138. public function getDateTime() {
  139. return $this->dateTime;
  140. }
  141. /**
  142. * @param string $type
  143. * @param string $id
  144. * @return $this
  145. * @throws \InvalidArgumentException if the object type or id is invalid
  146. * @since 8.2.0 - 9.0.0: Type of $id changed to string
  147. */
  148. public function setObject($type, $id) {
  149. if (!is_string($type) || $type === '' || isset($type[64])) {
  150. throw new \InvalidArgumentException('The given object type is invalid');
  151. }
  152. $this->objectType = $type;
  153. if (!is_int($id) && (!is_string($id) || $id === '' || isset($id[64]))) {
  154. throw new \InvalidArgumentException('The given object id is invalid');
  155. }
  156. $this->objectId = (string) $id;
  157. return $this;
  158. }
  159. /**
  160. * @return string
  161. * @since 8.2.0
  162. */
  163. public function getObjectType() {
  164. return $this->objectType;
  165. }
  166. /**
  167. * @return string
  168. * @since 8.2.0 - 9.0.0: Return type changed to string
  169. */
  170. public function getObjectId() {
  171. return $this->objectId;
  172. }
  173. /**
  174. * @param string $subject
  175. * @param array $parameters
  176. * @return $this
  177. * @throws \InvalidArgumentException if the subject or parameters are invalid
  178. * @since 8.2.0
  179. */
  180. public function setSubject($subject, array $parameters = []) {
  181. if (!is_string($subject) || $subject === '' || isset($subject[64])) {
  182. throw new \InvalidArgumentException('The given subject is invalid');
  183. }
  184. $this->subject = $subject;
  185. if (!is_array($parameters)) {
  186. throw new \InvalidArgumentException('The given subject parameters are invalid');
  187. }
  188. $this->subjectParameters = $parameters;
  189. return $this;
  190. }
  191. /**
  192. * @return string
  193. * @since 8.2.0
  194. */
  195. public function getSubject() {
  196. return $this->subject;
  197. }
  198. /**
  199. * @return string[]
  200. * @since 8.2.0
  201. */
  202. public function getSubjectParameters() {
  203. return $this->subjectParameters;
  204. }
  205. /**
  206. * @param string $subject
  207. * @return $this
  208. * @throws \InvalidArgumentException if the subject are invalid
  209. * @since 8.2.0
  210. */
  211. public function setParsedSubject($subject) {
  212. if (!is_string($subject) || $subject === '') {
  213. throw new \InvalidArgumentException('The given parsed subject is invalid');
  214. }
  215. $this->subjectParsed = $subject;
  216. return $this;
  217. }
  218. /**
  219. * @return string
  220. * @since 8.2.0
  221. */
  222. public function getParsedSubject() {
  223. return $this->subjectParsed;
  224. }
  225. /**
  226. * @param string $message
  227. * @param array $parameters
  228. * @return $this
  229. * @throws \InvalidArgumentException if the message or parameters are invalid
  230. * @since 8.2.0
  231. */
  232. public function setMessage($message, array $parameters = []) {
  233. if (!is_string($message) || $message === '' || isset($message[64])) {
  234. throw new \InvalidArgumentException('The given message is invalid');
  235. }
  236. $this->message = $message;
  237. if (!is_array($parameters)) {
  238. throw new \InvalidArgumentException('The given message parameters are invalid');
  239. }
  240. $this->messageParameters = $parameters;
  241. return $this;
  242. }
  243. /**
  244. * @return string
  245. * @since 8.2.0
  246. */
  247. public function getMessage() {
  248. return $this->message;
  249. }
  250. /**
  251. * @return string[]
  252. * @since 8.2.0
  253. */
  254. public function getMessageParameters() {
  255. return $this->messageParameters;
  256. }
  257. /**
  258. * @param string $message
  259. * @return $this
  260. * @throws \InvalidArgumentException if the message are invalid
  261. * @since 8.2.0
  262. */
  263. public function setParsedMessage($message) {
  264. if (!is_string($message) || $message === '') {
  265. throw new \InvalidArgumentException('The given parsed message is invalid');
  266. }
  267. $this->messageParsed = $message;
  268. return $this;
  269. }
  270. /**
  271. * @return string
  272. * @since 8.2.0
  273. */
  274. public function getParsedMessage() {
  275. return $this->messageParsed;
  276. }
  277. /**
  278. * @param string $link
  279. * @return $this
  280. * @throws \InvalidArgumentException if the link are invalid
  281. * @since 8.2.0
  282. */
  283. public function setLink($link) {
  284. if (!is_string($link) || $link === '' || isset($link[4000])) {
  285. throw new \InvalidArgumentException('The given link is invalid');
  286. }
  287. $this->link = $link;
  288. return $this;
  289. }
  290. /**
  291. * @return string
  292. * @since 8.2.0
  293. */
  294. public function getLink() {
  295. return $this->link;
  296. }
  297. /**
  298. * @return IAction
  299. * @since 8.2.0
  300. */
  301. public function createAction() {
  302. return new Action();
  303. }
  304. /**
  305. * @param IAction $action
  306. * @return $this
  307. * @throws \InvalidArgumentException if the action are invalid
  308. * @since 8.2.0
  309. */
  310. public function addAction(IAction $action) {
  311. if (!$action->isValid()) {
  312. throw new \InvalidArgumentException('The given action is invalid');
  313. }
  314. if ($action->isPrimary()) {
  315. if ($this->hasPrimaryAction) {
  316. throw new \InvalidArgumentException('The notification already has a primary action');
  317. }
  318. $this->hasPrimaryAction = true;
  319. }
  320. $this->actions[] = $action;
  321. return $this;
  322. }
  323. /**
  324. * @return IAction[]
  325. * @since 8.2.0
  326. */
  327. public function getActions() {
  328. return $this->actions;
  329. }
  330. /**
  331. * @param IAction $action
  332. * @return $this
  333. * @throws \InvalidArgumentException if the action are invalid
  334. * @since 8.2.0
  335. */
  336. public function addParsedAction(IAction $action) {
  337. if (!$action->isValidParsed()) {
  338. throw new \InvalidArgumentException('The given parsed action is invalid');
  339. }
  340. if ($action->isPrimary()) {
  341. if ($this->hasPrimaryParsedAction) {
  342. throw new \InvalidArgumentException('The notification already has a primary action');
  343. }
  344. $this->hasPrimaryParsedAction = true;
  345. }
  346. $this->actionsParsed[] = $action;
  347. return $this;
  348. }
  349. /**
  350. * @return IAction[]
  351. * @since 8.2.0
  352. */
  353. public function getParsedActions() {
  354. return $this->actionsParsed;
  355. }
  356. /**
  357. * @return bool
  358. * @since 8.2.0
  359. */
  360. public function isValid() {
  361. return
  362. $this->isValidCommon()
  363. &&
  364. $this->getSubject() !== ''
  365. ;
  366. }
  367. /**
  368. * @return bool
  369. * @since 8.2.0
  370. */
  371. public function isValidParsed() {
  372. return
  373. $this->isValidCommon()
  374. &&
  375. $this->getParsedSubject() !== ''
  376. ;
  377. }
  378. /**
  379. * @return bool
  380. */
  381. protected function isValidCommon() {
  382. return
  383. $this->getApp() !== ''
  384. &&
  385. $this->getUser() !== ''
  386. &&
  387. $this->getDateTime()->getTimestamp() !== 0
  388. &&
  389. $this->getObjectType() !== ''
  390. &&
  391. $this->getObjectId() !== ''
  392. ;
  393. }
  394. }