GroupPostService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Services;
  3. use App\Models\GroupPost;
  4. use App\Transformer\Api\GroupPostTransformer;
  5. use Cache;
  6. use League\Fractal;
  7. use League\Fractal\Serializer\ArraySerializer;
  8. class GroupPostService
  9. {
  10. const CACHE_KEY = 'pf:services:groups:post:';
  11. public static function key($gid, $pid)
  12. {
  13. return self::CACHE_KEY.$gid.':'.$pid;
  14. }
  15. public static function get($gid, $pid)
  16. {
  17. return Cache::remember(self::key($gid, $pid), 604800, function () use ($gid, $pid) {
  18. $gp = GroupPost::whereGroupId($gid)->find($pid);
  19. if (! $gp) {
  20. return null;
  21. }
  22. $fractal = new Fractal\Manager();
  23. $fractal->setSerializer(new ArraySerializer());
  24. $resource = new Fractal\Resource\Item($gp, new GroupPostTransformer());
  25. $res = $fractal->createData($resource)->toArray();
  26. $res['pf_type'] = $gp['type'];
  27. $res['url'] = $gp->url();
  28. // if($gp['type'] == 'poll') {
  29. // $status['poll'] = PollService::get($status['id']);
  30. // }
  31. //$status['account']['url'] = url("/groups/{$gp['group_id']}/user/{$status['account']['id']}");
  32. return $res;
  33. });
  34. }
  35. public static function del($gid, $pid)
  36. {
  37. return Cache::forget(self::key($gid, $pid));
  38. }
  39. }