You are here

class NetlifyManager in Build Hooks 3.x

Same name and namespace in other branches
  1. 8.2 modules/build_hooks_netlify/src/NetlifyManager.php \Drupal\build_hooks_netlify\NetlifyManager

Defines a manager service for netlify deployments.

Hierarchy

Expanded class hierarchy of NetlifyManager

1 file declares its use of NetlifyManager
NetlifyFrontendEnvironment.php in modules/build_hooks_netlify/src/Plugin/FrontendEnvironment/NetlifyFrontendEnvironment.php
1 string reference to 'NetlifyManager'
build_hooks_netlify.services.yml in modules/build_hooks_netlify/build_hooks_netlify.services.yml
modules/build_hooks_netlify/build_hooks_netlify.services.yml
1 service uses NetlifyManager
build_hooks_netlify.netlify_manager in modules/build_hooks_netlify/build_hooks_netlify.services.yml
Drupal\build_hooks_netlify\NetlifyManager

File

modules/build_hooks_netlify/src/NetlifyManager.php, line 12

Namespace

Drupal\build_hooks_netlify
View source
class NetlifyManager implements NetlifyManagerInterface {
  const NETLIFY_BASE_PATH = 'https://api.netlify.com/api/v1/';
  const NETLIFY_DATE_FORMAT = 'Y-m-d\\TH:i:s';

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * GuzzleHttp\ClientInterface definition.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * The Date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Constructs a new NetlifyManager object.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client, DateFormatterInterface $date_formatter) {
    $this->configFactory = $config_factory;
    $this->httpClient = $http_client;
    $this->dateFormatter = $date_formatter;
  }

  /**
   * Get the latest builds from netlify for and environment.
   *
   * @param array $settings
   *   The plugin settings array.
   * @param int $limit
   *   Number of desired builds to retrieve.
   *
   * @return array
   *   An array with info about the builds.
   *
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function retrieveLatestBuildsFromNetlifyForEnvironment(array $settings, $limit = 1) {
    $url = $this
      ->buildNetlifyApiRetrieveBuildsUrl($settings);
    $options = [
      'headers' => [
        'Accept' => 'application/json',
      ],
    ];
    $response = $this->httpClient
      ->request('GET', $url, $options);
    $payload = json_decode($response
      ->getBody()
      ->getContents(), TRUE);

    // Since there is no way in the api to filter by branch,
    // we do it by filtering the results:
    $results = array_filter($payload, $this
      ->filterByBranch($settings['branch']));
    return array_slice($results, 0, $limit);
  }

  /**
   * Build the url to retrieve latest builds from netlify for an environment.
   *
   * @param array $config
   *   The configuration array from the plugin.
   *
   * @return string
   *   The url to call to get the builds.
   */
  private function buildNetlifyApiRetrieveBuildsUrl(array $config) {
    $netlifyConf = $this->configFactory
      ->get('build_hooks_netlify.settings');
    $access_token = $netlifyConf
      ->get('netlify_api_key');
    $api_id = $config['api_id'];
    return self::NETLIFY_BASE_PATH . "/sites/{$api_id}/deploys?access_token={$access_token}";
  }

  /**
   * Converts the datetime format into a drupal formatted date.
   *
   * @param string $datetime
   *   Date in the format returned by the Netlify api.
   *
   * @return string
   *   Drupal formatted date.
   */
  public function formatNetlifyDateTime($datetime) {

    // We remove the last5 digits because we can't handle milliseconds:
    $datetime_no_millis = substr($datetime, 0, -5);
    $timezone = new \DateTimeZone('UTC');
    $date = \DateTime::createFromFormat(self::NETLIFY_DATE_FORMAT, $datetime_no_millis, $timezone);
    return $this->dateFormatter
      ->format($date
      ->getTimestamp(), 'long');
  }

  /**
   * Simple callback to filter an array by the branch property.
   *
   * @param string $branch
   *   The branch to filter for.
   *
   * @return \Closure
   *   Closure to be used in array_filter.
   */
  private function filterByBranch($branch) {
    return function ($item) use ($branch) {
      return $item['branch'] == $branch;
    };
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NetlifyManager::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition.
NetlifyManager::$dateFormatter protected property The Date formatter service.
NetlifyManager::$httpClient protected property GuzzleHttp\ClientInterface definition.
NetlifyManager::buildNetlifyApiRetrieveBuildsUrl private function Build the url to retrieve latest builds from netlify for an environment.
NetlifyManager::filterByBranch private function Simple callback to filter an array by the branch property.
NetlifyManager::formatNetlifyDateTime public function Converts the datetime format into a drupal formatted date. Overrides NetlifyManagerInterface::formatNetlifyDateTime
NetlifyManager::NETLIFY_BASE_PATH constant
NetlifyManager::NETLIFY_DATE_FORMAT constant
NetlifyManager::retrieveLatestBuildsFromNetlifyForEnvironment public function Get the latest builds from netlify for and environment. Overrides NetlifyManagerInterface::retrieveLatestBuildsFromNetlifyForEnvironment
NetlifyManager::__construct public function Constructs a new NetlifyManager object.