You are here

public function Developer::getCompanies in Apigee Edge 8

1 call to Developer::getCompanies()
Developer::hasCompany in src/Entity/Developer.php

File

src/Entity/Developer.php, line 272

Class

Developer
Defines the Developer entity class.

Namespace

Drupal\apigee_edge\Entity

Code

public function getCompanies() : array {

  // If companies is null it means the original API response that this
  // object constructed did not contain a non-empty company list.
  // One of the reasons of this could be that the entity got loaded
  // by calling the list developers API endpoint that does not return the
  // companies.
  // @see https://apidocs.apigee.com/management/apis/get/organizations/%7Borg_name%7D/developers
  if ($this->companiesCache
    ->getCompanies($this
    ->getDeveloperId()) === NULL) {

    /** @var \Drupal\apigee_edge\Entity\Controller\DeveloperControllerInterface $controller */
    $controller = \Drupal::service('apigee_edge.controller.developer');

    // If controller has an internal cache let's check whether this
    // developer in it and it has a non-empty company list.
    if ($controller instanceof EntityCacheAwareControllerInterface) {

      /** @var \Apigee\Edge\Api\Management\Entity\DeveloperInterface|null $cached_developer */
      $cached_developer = $controller
        ->entityCache()
        ->getEntity($this
        ->getDeveloperId());
      if ($cached_developer && !empty($cached_developer
        ->getCompanies())) {

        // Save it to the local cache so we can serve it from there
        // next time.
        $this->companiesCache
          ->saveCompanies([
          $cached_developer,
        ]);
        return $this->companiesCache
          ->getCompanies($cached_developer
          ->id());
      }
      else {

        // Let's remove the developer from the cache otherwise we get it back
        // with the same empty company list as before (maybe returned by the
        // list developers API endpoint) for this developer.
        $controller
          ->entityCache()
          ->removeEntities([
          $this
            ->getDeveloperId(),
        ]);
      }
    }

    /** @var \Apigee\Edge\Api\Management\Entity\DeveloperInterface $developer */
    try {
      $developer = $controller
        ->load($this
        ->getEmail());

      // Save the list of companies (even if it is actually empty) to this
      // local cache property so we can return this information without
      // calling Apigee Edge next time.
      $this->companiesCache
        ->saveCompanies([
        $developer,
      ]);
    } catch (ApiException $exception) {
      $message = 'Unable to load companies of %developer developer from Apigee Edge. @message %function (line %line of %file). <pre>@backtrace_string</pre>';
      $context = [
        '%developer' => $this
          ->getEmail(),
      ];
      $context += Error::decodeException($exception);
      \Drupal::logger('apigee_edge')
        ->error($message, $context);

      // Return an empty array if the API call fails because this is the
      // safest thing that we can do in this case.
      // Do not change the value of $this->companies because this way we can
      // ensure the this method tries to call the API again next time.
      return [];
    }
  }
  return $this->companiesCache
    ->getCompanies($this
    ->getDeveloperId());
}