cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/src/Plugin/Block/StaleSnapshotsBlock.php
modules/cloud_service_providers/aws_cloud/src/Plugin/Block/StaleSnapshotsBlock.php
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block displaying stale snapshots.
*
* @Block(
* id = "aws_cloud_stale_snapshots_block",
* admin_label = @Translation("Stale Snapshots"),
* category = @Translation("AWS Cloud")
* )
*/
class StaleSnapshotsBlock extends BlockBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* Stores the configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Creates a StaleSnapshotsBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory,
AccountInterface $current_user
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function build() {
return $this->buildSnapshotList();
}
/**
* Build a list of stale snapshots.
*
* @return array
* A set of render arrays.
*/
private function buildSnapshotList() {
$build = [];
$snapshots = $this->getStaleSnapshots();
if (count($snapshots)) {
$unused_days = $this->configFactory->get('aws_cloud.settings')->get('aws_cloud_stale_snapshot_criteria');
$urls = [];
/* @var \Drupal\aws_cloud\Entity\Ec2\Snapshot $snapshot */
foreach ($snapshots as $snapshot) {
$days_running = $snapshot->daysRunning();
$link_text = $this->t('@snapshot (%running @days)', [
'@snapshot' => $snapshot->getName(),
'%running' => $days_running,
'@days' => $this->formatPlural($days_running, 'day', 'days'),
]);
$urls[] = $snapshot->toLink($link_text);
}
$build[] = [
'#markup' => $this->t('The following snapshots have been running for more than %num days', ['%num' => $unused_days]),
];
$build[] = [
'#theme' => 'item_list',
'#items' => $urls,
];
}
else {
$build = [
'#markup' => $this->t('Great job! You have no stale snapshots.'),
];
}
return $build;
}
/**
* Get a list of stale snapshots.
*
* @return array
* Array of stale snapshots.
*/
private function getStaleSnapshots() {
$snapshots = aws_cloud_get_stale_snapshots();
if (!$this->currentUser->hasPermission('view any aws cloud snapshot')) {
/* @var \Drupal\aws_cloud\Entity\Ec2\Snapshot $snapshot */
foreach ($snapshots as $key => $snapshot) {
// Only return volumes the user has access to.
if ($snapshot->getOwnerId() !== $this->currentUser->id()) {
unset($snapshots[$key]);
}
}
}
return $snapshots;
}
}
