1<?php 2 3declare(strict_types=1); 4 5 6/** 7 * Circles - Bring cloud-users closer together. 8 * 9 * This file is licensed under the Affero General Public License version 3 or 10 * later. See the COPYING file. 11 * 12 * @author Maxence Lange <maxence@artificial-owl.com> 13 * @copyright 2017 14 * @license GNU AGPL version 3 or any later version 15 * 16 * This program is free software: you can redistribute it and/or modify 17 * it under the terms of the GNU Affero General Public License as 18 * published by the Free Software Foundation, either version 3 of the 19 * License, or (at your option) any later version. 20 * 21 * This program is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 * GNU Affero General Public License for more details. 25 * 26 * You should have received a copy of the GNU Affero General Public License 27 * along with this program. If not, see <http://www.gnu.org/licenses/>. 28 * 29 */ 30 31 32namespace OCA\Circles\Model\GlobalScale; 33 34use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore; 35use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools; 36use JsonSerializable; 37use OCA\Circles\Exceptions\JsonException; 38use OCA\Circles\Exceptions\ModelException; 39use OCA\Circles\Model\Circle; 40use OCA\Circles\Model\DeprecatedCircle; 41use OCA\Circles\Model\DeprecatedMember; 42 43/** 44 * Class GSEvent 45 * 46 * @package OCA\Circles\Model\GlobalScale 47 */ 48class GSEvent implements JsonSerializable { 49 public const SEVERITY_LOW = 1; 50 public const SEVERITY_HIGH = 3; 51 52 public const TEST = '\OCA\Circles\GlobalScale\Test'; 53 public const GLOBAL_SYNC = '\OCA\Circles\GlobalScale\GlobalSync'; 54 public const CIRCLE_STATUS = '\OCA\Circles\GlobalScale\CircleStatus'; 55 56 public const CIRCLE_CREATE = '\OCA\Circles\GlobalScale\CircleCreate'; 57 public const CIRCLE_UPDATE = '\OCA\Circles\GlobalScale\CircleUpdate'; 58 public const CIRCLE_DESTROY = '\OCA\Circles\GlobalScale\CircleDestroy'; 59 public const MEMBER_ADD = '\OCA\Circles\GlobalScale\MemberAdd'; 60 public const MEMBER_JOIN = '\OCA\Circles\GlobalScale\MemberJoin'; 61 public const MEMBER_LEAVE = '\OCA\Circles\GlobalScale\MemberLeave'; 62 public const MEMBER_LEVEL = '\OCA\Circles\GlobalScale\MemberLevel'; 63 public const MEMBER_UPDATE = '\OCA\Circles\GlobalScale\MemberUpdate'; 64 public const MEMBER_REMOVE = '\OCA\Circles\GlobalScale\MemberRemove'; 65 public const USER_DELETED = '\OCA\Circles\GlobalScale\UserDeleted'; 66 67 public const FILE_SHARE = '\OCA\Circles\GlobalScale\FileShare'; 68 public const FILE_UNSHARE = '\OCA\Circles\GlobalScale\FileUnshare'; 69 70 71 use TArrayTools; 72 73 74 /** @var string */ 75 private $type = ''; 76 77 /** @var string */ 78 private $source = ''; 79 80 /** @var DeprecatedCircle */ 81 private $deprecatedCircle; 82 83 /** @var Circle */ 84 private $circle; 85 86 /** @var DeprecatedMember */ 87 private $member; 88 89 /** @var SimpleDataStore */ 90 private $data; 91 92 /** @var int */ 93 private $severity = self::SEVERITY_LOW; 94 95 /** @var SimpleDataStore */ 96 private $result; 97 98 /** @var string */ 99 private $key = ''; 100 101 /** @var bool */ 102 private $local = false; 103 104 /** @var bool */ 105 private $force = false; 106 107 /** @var bool */ 108 private $async = false; 109 110 /** @var bool */ 111 private $checked = false; 112 113 114 /** 115 * GSEvent constructor. 116 * 117 * @param string $type 118 * @param bool $local 119 * @param bool $force 120 */ 121 public function __construct(string $type = '', bool $local = false, bool $force = false) { 122 $this->type = $type; 123 $this->local = $local; 124 $this->force = $force; 125 $this->data = new SimpleDataStore(); 126 $this->result = new SimpleDataStore(); 127 } 128 129 130 /** 131 * @return string 132 */ 133 public function getType(): string { 134 return $this->type; 135 } 136 137 /** 138 * @param mixed $type 139 * 140 * @return GSEvent 141 */ 142 public function setType($type): self { 143 $this->type = $type; 144 145 return $this; 146 } 147 148 149 /** 150 * @return string 151 */ 152 public function getSource(): string { 153 return $this->source; 154 } 155 156 /** 157 * @param string $source 158 * 159 * @return GSEvent 160 */ 161 public function setSource(string $source): self { 162 $this->source = $source; 163 164 if ($this->hasMember() && $this->member->getInstance() === '') { 165 $this->member->setInstance($source); 166 } 167 168 if ($this->hasCircle() 169 && $this->getDeprecatedCircle() 170 ->hasViewer() 171 && $this->getDeprecatedCircle() 172 ->getViewer() 173 ->getInstance() === '') { 174 $this->getDeprecatedCircle() 175 ->getViewer() 176 ->setInstance($source); 177 } 178 179 return $this; 180 } 181 182 183 /** 184 * @return bool 185 */ 186 public function isLocal(): bool { 187 return $this->local; 188 } 189 190 /** 191 * @param bool $local 192 * 193 * @return GSEvent 194 */ 195 public function setLocal(bool $local): self { 196 $this->local = $local; 197 198 return $this; 199 } 200 201 202 /** 203 * @return bool 204 */ 205 public function isForced(): bool { 206 return $this->force; 207 } 208 209 /** 210 * @param bool $force 211 * 212 * @return GSEvent 213 */ 214 public function setForced(bool $force): self { 215 $this->force = $force; 216 217 return $this; 218 } 219 220 221 /** 222 * @return bool 223 */ 224 public function isAsync(): bool { 225 return $this->async; 226 } 227 228 /** 229 * @param bool $async 230 * 231 * @return GSEvent 232 */ 233 public function setAsync(bool $async): self { 234 $this->async = $async; 235 236 return $this; 237 } 238 239 240 /** 241 * @return DeprecatedCircle 242 * @deprecated 243 */ 244 public function getDeprecatedCircle(): DeprecatedCircle { 245 return $this->deprecatedCircle; 246 } 247 248 /** 249 * @param DeprecatedCircle $deprecatedCircle 250 * 251 * @return GSEvent 252 * @deprecated 253 */ 254 public function setDeprecatedCircle(DeprecatedCircle $deprecatedCircle): self { 255 $this->deprecatedCircle = $deprecatedCircle; 256 257 return $this; 258 } 259 260 /** 261 * @return bool 262 */ 263 public function hasCircle(): bool { 264 return ($this->deprecatedCircle !== null || $this->circle !== null); 265 } 266 267 /** 268 * @param Circle $circle 269 * 270 * @return GSEvent 271 */ 272 public function setCircle(Circle $circle): self { 273 $this->circle = $circle; 274 275 return $this; 276 } 277 278 /** 279 * @return Circle 280 */ 281 public function getCircle(): Circle { 282 return $this->circle; 283 } 284 285 286 /** 287 * @return DeprecatedMember 288 */ 289 public function getMember(): DeprecatedMember { 290 return $this->member; 291 } 292 293 /** 294 * @param DeprecatedMember $member 295 * 296 * @return GSEvent 297 */ 298 public function setMember(DeprecatedMember $member): self { 299 $this->member = $member; 300 301 return $this; 302 } 303 304 /** 305 * @return bool 306 */ 307 public function hasMember(): bool { 308 return ($this->member !== null); 309 } 310 311 312 /** 313 * @param SimpleDataStore $data 314 * 315 * @return GSEvent 316 */ 317 public function setData(SimpleDataStore $data): self { 318 $this->data = $data; 319 320 return $this; 321 } 322 323 /** 324 * @return SimpleDataStore 325 */ 326 public function getData(): SimpleDataStore { 327 return $this->data; 328 } 329 330 331 /** 332 * @return int 333 */ 334 public function getSeverity(): int { 335 return $this->severity; 336 } 337 338 /** 339 * @param int $severity 340 * 341 * @return GSEvent 342 */ 343 public function setSeverity(int $severity): self { 344 $this->severity = $severity; 345 346 return $this; 347 } 348 349 350 /** 351 * @return SimpleDataStore 352 */ 353 public function getResult(): SimpleDataStore { 354 return $this->result; 355 } 356 357 /** 358 * @param SimpleDataStore $result 359 * 360 * @return GSEvent 361 */ 362 public function setResult(SimpleDataStore $result): self { 363 $this->result = $result; 364 365 return $this; 366 } 367 368 369 /** 370 * @return string 371 */ 372 public function getKey(): string { 373 return $this->key; 374 } 375 376 /** 377 * @param string $key 378 * 379 * @return GSEvent 380 */ 381 public function setKey(string $key): self { 382 $this->key = $key; 383 384 return $this; 385 } 386 387 388 /** 389 * @return bool 390 */ 391 public function isValid(): bool { 392 if ($this->getType() === '') { 393 return false; 394 } 395 396 return true; 397 } 398 399 400 /** 401 * @param string $json 402 * 403 * @return GSEvent 404 * @throws JsonException 405 * @throws ModelException 406 */ 407 public function importFromJson(string $json): self { 408 $data = json_decode($json, true); 409 if (!is_array($data)) { 410 throw new JsonException('invalid JSON'); 411 } 412 413 return $this->import($data); 414 } 415 416 417 /** 418 * @param array $data 419 * 420 * @return GSEvent 421 * @throws ModelException 422 */ 423 public function import(array $data): self { 424 $this->setType($this->get('type', $data)); 425 $this->setSeverity($this->getInt('severity', $data)); 426 $this->setData(new SimpleDataStore($this->getArray('data', $data))); 427 $this->setResult(new SimpleDataStore($this->getArray('result', $data))); 428 $this->setSource($this->get('source', $data)); 429 $this->setKey($this->get('key', $data)); 430 $this->setForced($this->getBool('force', $data)); 431 $this->setAsync($this->getBool('async', $data)); 432 433 if (array_key_exists('circle', $data)) { 434 $this->setDeprecatedCircle(DeprecatedCircle::fromArray($data['circle'])); 435 } 436 437 if (array_key_exists('member', $data)) { 438 $this->setMember(DeprecatedMember::fromArray($data['member'])); 439 } 440 441 if (!$this->isValid()) { 442 throw new ModelException('invalid GSEvent'); 443 } 444 445 return $this; 446 } 447 448 449 /** 450 * @return array 451 */ 452 public function jsonSerialize(): array { 453 $arr = [ 454 'type' => $this->getType(), 455 'severity' => $this->getSeverity(), 456 'data' => $this->getData(), 457 'result' => $this->getResult(), 458 'key' => $this->getKey(), 459 'source' => $this->getSource(), 460 'force' => $this->isForced(), 461 'async' => $this->isAsync() 462 ]; 463 464 if ($this->hasCircle()) { 465 $arr['circle'] = $this->getDeprecatedCircle(); 466 } 467 if ($this->hasMember()) { 468 $arr['member'] = $this->getMember(); 469 } 470 471 $this->cleanArray($arr); 472 473 return $arr; 474 } 475} 476