Update ProfileService, use account transformer

This commit is contained in:
Daniel Supernault 2021-05-01 13:35:47 -06:00
parent 9cf962fff5
commit 391b1287ac
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 23 additions and 35 deletions

View file

@ -57,7 +57,7 @@ class MediaTagService
protected function idToUsername($id)
{
$profile = ProfileService::build()->profileId($id);
$profile = ProfileService::get($id);
if(!$profile) {
return 'unavailable';
@ -65,8 +65,8 @@ class MediaTagService
return [
'id' => (string) $id,
'username' => $profile->username,
'avatar' => $profile->avatarUrl()
'username' => $profile['username'],
'avatar' => $profile['avatar']
];
}
@ -98,4 +98,4 @@ class MediaTagService
Cache::forget($key);
return true;
}
}
}

View file

@ -4,41 +4,29 @@ namespace App\Services;
use Cache;
use Illuminate\Support\Facades\Redis;
use App\{
Follower,
Profile
};
use App\Transformer\Api\AccountTransformer;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Profile;
class ProfileService {
protected $profile;
protected $profile_prefix;
public static function build()
public static function get($id)
{
return new self();
}
public function profile(Profile $profile)
{
$this->profile = $profile;
$this->profile_prefix = 'profile:model:'.$profile->id;
return $this;
}
public function profileId($id)
{
return Cache::rememberForever('profile:model:'.$id, function() use($id) {
return Profile::findOrFail($id);
$key = 'profile:model:' . $id;
$ttl = now()->addHours(4);
$res = Cache::remember($key, $ttl, function() use($id) {
$profile = Profile::find($id);
if(!$profile) {
return false;
}
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return $fractal->createData($resource)->toArray();
});
return $res;
}
public function get()
{
return Cache::rememberForever($this->profile_prefix, function() {
return $this->profile;
});
}
}
}