I have such part of function:
$token = $this->getToken($user);
if ('main' !== session('type') && is_null($token)) {
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
} else {
if (!$instagramProfile = $this->findInstagramProfile($user)) {
$profile = create_instagram_profile($user);
$user = $this->createUser($profile);
$token = $user->api_token;
} else {
if($instagramProfile->profile->user) {
$token = $instagramProfile->profile->user->api_token;
} else {
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
}
};
}
I have many scenarios:
- param
type
which save in session; - instagram profile exist;
- user exists.
In worlds - very simple, but in code it stay not understandable.
How I can optimize such part of code?
Maybe create more abstract function?