You are here

public function AutomaticUpdatesPsa::getPublicServiceMessages in Automatic Updates 8

Get public service messages.

Return value

array A return of translatable strings.

Overrides AutomaticUpdatesPsaInterface::getPublicServiceMessages

File

src/Services/AutomaticUpdatesPsa.php, line 114

Class

AutomaticUpdatesPsa
Class AutomaticUpdatesPsa.

Namespace

Drupal\automatic_updates\Services

Code

public function getPublicServiceMessages() {
  $messages = [];
  if (!$this->config
    ->get('enable_psa')) {
    return $messages;
  }
  if ($cache = $this->cache
    ->get('automatic_updates_psa')) {
    $response = $cache->data;
  }
  else {
    $psa_endpoint = $this->config
      ->get('psa_endpoint');
    try {
      $response = $this->httpClient
        ->get($psa_endpoint)
        ->getBody()
        ->getContents();
      $this->cache
        ->set('automatic_updates_psa', $response, $this->time
        ->getCurrentTime() + $this->config
        ->get('check_frequency'));
    } catch (TransferException $exception) {
      $this->logger
        ->error($exception
        ->getMessage());
      return [
        $this
          ->t('Drupal PSA endpoint :url is unreachable.', [
          ':url' => $psa_endpoint,
        ]),
      ];
    }
  }
  try {
    $json_payload = json_decode($response, FALSE);
    if ($json_payload !== NULL) {
      foreach ($json_payload as $json) {
        if ($json->is_psa && ($json->type === 'core' || $this
          ->isValidExtension($json->type, $json->project))) {
          $messages[] = $this
            ->message($json->title, $json->link);
        }
        elseif ($json->type === 'core') {
          $this
            ->parseConstraints($messages, $json, \Drupal::VERSION);
        }
        elseif ($this
          ->isValidExtension($json->type, $json->project)) {
          $this
            ->contribParser($messages, $json);
        }
      }
    }
    else {
      $this->logger
        ->error('Drupal PSA JSON is malformed: @response', [
        '@response' => $response,
      ]);
      $messages[] = $this
        ->t('Drupal PSA JSON is malformed.');
    }
  } catch (\UnexpectedValueException $exception) {
    $this->logger
      ->error($exception
      ->getMessage());
    $messages[] = $this
      ->t('Drupal PSA endpoint service is malformed.');
  }
  return $messages;
}