Commit 331d5493 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3171006 by Xiaohua Guan, yas, baldwinlouie: Refactor "Unused private...

Issue #3171006 by Xiaohua Guan, yas, baldwinlouie: Refactor "Unused private method getInstanceTypePricingLink()"
parent 851f728d
Loading
Loading
Loading
Loading
+81 −1
Changes for modules/cloud_service_providers/aws_cloud/src/Plugin/Block/AwsCloudResourcesBlock.php: 81 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 *   category = @Translation("AWS Cloud")
 * )
 */
class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterface {
class AwsCloudResourcesBlock extends BlockBase implements ContainerFactoryPluginInterface {

  use ResourceBlockTrait;

@@ -235,6 +235,86 @@ class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterfac
    ];
  }

  /**
   * Build a resource table row.
   *
   * @param array $resources
   *   Entity array to build the row.
   *
   * @return array
   *   Fully built rows.
   */
  protected function buildResourceTableRows(array $resources) {
    $rows = [];
    $data = [];
    $i = 0;
    $index = 0;

    foreach ($resources ?: [] as $key => $values) {
      $data[] = $key === 'instance_type_prices'
        ? $this->getInstanceTypePricingLink($key, $values[0])
        : $this->getResourceLink($key, $values[0], $values[1]);

      // Skip if $data doesn't have any value.
      if (empty($data[count($data) - 1])) {
        continue;
      }

      $rows[$index] = [
        'no_striping' => TRUE,
        'data' => $data,
      ];
      if ($i++ % 2) {
        $index++;
        $data = [];
      }
    }
    return $rows;
  }

  /**
   * Generate AWS resource link.
   *
   * @param string $resource_type
   *   The resource type.
   * @param string $permission
   *   The getResourceCount permission.
   * @param array $params
   *   The getResourceCount params.
   *
   * @return \Drupal\Core\Link
   *   The AWS resource link.
   */
  protected function getResourceLink($resource_type, $permission, array $params = []) {
    // Fetch the labels.
    $labels = $this->getDisplayLabels($resource_type);
    $cloud_context = $this->configuration['cloud_context'];

    if (!empty($cloud_context)) {
      $route_name = "view.${resource_type}.list";
      $params = [
        'cloud_context' => $cloud_context,
      ];
    }
    else {
      $route_name = "view.${resource_type}.all";
    }

    return Link::createFromRoute(
      $this->formatPlural(
        $this->getResourceCount($resource_type, $permission, $params),
        '1 @label',
        '@count @plural_label',
        [
          '@label' => $labels['singular'] ?? $resource_type,
          '@plural_label' => $labels['plural'] ?? $resource_type,
        ]
      ),
      $route_name,
      $params
    );
  }

  /**
   * Generate AWS Instance Type Pricing link.
   *
+35
Changes for modules/cloud_service_providers/k8s/src/Plugin/Block/K8sResourcesBlock.php: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -224,6 +224,41 @@ class K8sResourcesBlock extends K8sBaseBlock {
    ];
  }

  /**
   * Build a resource table row.
   *
   * @param array $resources
   *   Entity array to build the row.
   *
   * @return array
   *   Fully built rows.
   */
  protected function buildResourceTableRows(array $resources) {
    $rows = [];
    $data = [];
    $i = 0;
    $index = 0;

    foreach ($resources ?: [] as $key => $values) {
      $data[] = $this->getResourceLink($key, $values[0], $values[1]);

      // Skip if $data doesn't have any value.
      if (empty($data[count($data) - 1])) {
        continue;
      }

      $rows[$index] = [
        'no_striping' => TRUE,
        'data' => $data,
      ];
      if ($i++ % 2) {
        $index++;
        $data = [];
      }
    }
    return $rows;
  }

  /**
   * Generate K8s resource link.
   *
−82
Changes for src/Traits/ResourceBlockTrait.php: 0 added lines, 82 removed lines.
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\cloud\Traits;

use Drupal\Core\Link;

/**
 * The trait with common functions for Resource Blocks.
 */
@@ -54,49 +52,6 @@ trait ResourceBlockTrait {
      ->loadByProperties($params);
  }

  /**
   * Generate AWS resource link.
   *
   * @param string $resource_type
   *   The resource type.
   * @param string $permission
   *   The getResourceCount permission.
   * @param array $params
   *   The getResourceCount params.
   *
   * @return \Drupal\Core\Link
   *   The AWS resource link.
   */
  protected function getResourceLink($resource_type, $permission, array $params = []) {
    // Fetch the labels.
    $labels = $this->getDisplayLabels($resource_type);
    $cloud_context = $this->configuration['cloud_context'];

    if (!empty($cloud_context)) {
      $route_name = "view.${resource_type}.list";
      $params = [
        'cloud_context' => $cloud_context,
      ];
    }
    else {
      $route_name = "view.${resource_type}.all";
    }

    return Link::createFromRoute(
      $this->formatPlural(
        $this->getResourceCount($resource_type, $permission, $params),
        '1 @label',
        '@count @plural_label',
        [
          '@label' => $labels['singular'] ?? $resource_type,
          '@plural_label' => $labels['plural'] ?? $resource_type,
        ]
      ),
      $route_name,
      $params
    );
  }

  /**
   * Load the cloud configs as an array for use in a select dropdown.
   *
@@ -117,41 +72,4 @@ trait ResourceBlockTrait {
    return $cloud_configs;
  }

  /**
   * Build a resource table row.
   *
   * @param array $resources
   *   Entity array to build the row.
   *
   * @return array
   *   Fully built rows.
   */
  protected function buildResourceTableRows(array $resources) {
    $rows = [];
    $data = [];
    $i = 0;
    $index = 0;

    foreach ($resources ?: [] as $key => $values) {
      $data[] = $key === 'instance_type_prices'
        ? $this->getInstanceTypePricingLink($key, $values[0])
        : $this->getResourceLink($key, $values[0], $values[1]);

      // Skip if $data doesn't have any value.
      if (empty($data[count($data) - 1])) {
        continue;
      }

      $rows[$index] = [
        'no_striping' => TRUE,
        'data' => $data,
      ];
      if ($i++ % 2) {
        $index++;
        $data = [];
      }
    }
    return $rows;
  }

}