You are here

public function InstagramAccount::getAccountName in Instagram Feeds 8

Gets Instagram account username.

Parameters

bool $save: Save entity or not when username obtained from Instagram API.

Return value

string Instagram username.

Overrides InstagramAccountInterface::getAccountName

File

src/Entity/InstagramAccount.php, line 322

Class

InstagramAccount
Defines the instagram_account entity class.

Namespace

Drupal\instagram_feeds\Entity

Code

public function getAccountName($save = FALSE) : string {
  if ($this
    ->get('account')
    ->isEmpty()) {
    $token = $this
      ->getToken();
    if (!$token) {
      return '';
    }
    $response = \Drupal::httpClient()
      ->get(self::INSTAGRAM_GRAPH_ENDPOINT . '/me?' . http_build_query([
      'fields' => 'username',
      'access_token' => $token,
    ]));
    $body = $this
      ->extractInstagramData($response);
    $this
      ->set('account', $body['username']);
    if (!$this
      ->isNew() && $save) {
      $this
        ->save();
    }
  }
  return $this
    ->get('account')
    ->first()
    ->getString();
}