cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/src/Plugin/Block/DisassociatedSnapshotsBlock.php
modules/cloud_service_providers/aws_cloud/src/Plugin/Block/DisassociatedSnapshotsBlock.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 unused snapshots.
*
* @Block(
* id = "aws_cloud_disassociated_snapshots_block",
* admin_label = @Translation("Disassociated Snapshots"),
* category = @Translation("AWS Cloud")
* )
*/
class DisassociatedSnapshotsBlock 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 ResourcesBlock 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 unused snapshots.
*
* @return array
* A set of render arrays.
*/
private function buildSnapshotList() {
$build = [];
$snapshots = $this->getDisassociatedSnapshots();
if (count($snapshots)) {
$urls = [];
/* @var \Drupal\aws_cloud\Entity\Ec2\Snapshot $snapshot */
foreach ($snapshots as $snapshot) {
$urls[] = $snapshot->toLink($snapshot->getName());
}
$build[] = [
'#markup' => $this->t('The following snapshots are not associated with a volume'),
];
$build[] = [
'#theme' => 'item_list',
'#items' => $urls,
];
}
else {
$build = [
'#markup' => $this->t('Great job! You have no disassociated snapshots.'),
];
}
return $build;
}
/**
* Get a list of disassociated snapshots.
*
* @return array
* Array of disassociated snapshots.
*/
private function getDisassociatedSnapshots() {
$snapshots = aws_cloud_get_unused_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;
}
}
