UpdateChecklist.php in Update helper 2.x
File
modules/update_helper_checklist/src/UpdateChecklist.php
View source
<?php
namespace Drupal\update_helper_checklist;
use Drupal\checklistapi\Storage\StateStorage;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\update_helper_checklist\Entity\Update;
use Symfony\Component\Yaml\Yaml;
class UpdateChecklist {
public static $updateChecklistFileName = 'updates_checklist.yml';
protected $checkListStateStorage;
protected $moduleHandler;
protected $account;
protected $checklist;
public function __construct(StateStorage $stateStorage, ModuleHandlerInterface $moduleHandler, AccountInterface $account) {
$this->checkListStateStorage = $stateStorage;
$this->moduleHandler = $moduleHandler;
$this->account = $account;
}
protected function getChecklist() {
if (!$this->checklist) {
$this->checklist = checklistapi_checklist_load('update_helper_checklist');
}
return $this->checklist;
}
public function markUpdatesSuccessful(array $module_updates, $checkListPoints = TRUE) {
$update_ids = $this
->getFlatChecklistKeys($module_updates);
$this
->setSuccessfulByHook($update_ids, TRUE);
if ($checkListPoints) {
$this
->checkListPoints($update_ids);
}
}
public function markUpdatesFailed(array $module_updates) {
$update_ids = $this
->getFlatChecklistKeys($module_updates);
$this
->setSuccessfulByHook($update_ids, FALSE);
}
public function markAllUpdates($status = TRUE) {
$update_ids = [];
foreach ($this
->getChecklist()->items as $version_items) {
foreach ($version_items as $key => $item) {
if (is_array($item)) {
$update_ids[] = $key;
}
}
}
$this
->setSuccessfulByHook($update_ids, $status);
$this
->checkAllListPoints($status);
}
protected function setSuccessfulByHook(array $update_ids, $status = TRUE) {
foreach ($update_ids as $update_id) {
if ($update = Update::load($update_id)) {
$update
->setSuccessfulByHook($status);
$update
->save();
}
else {
Update::create([
'id' => $update_id,
'successful_by_hook' => $status,
])
->save();
}
}
}
protected function getFlatChecklistKeys(array $module_update_list) {
$flatKeys = [];
foreach ($module_update_list as $module_name => $updates) {
foreach ($updates as $update) {
$flatKeys[] = str_replace('.', '_', $module_name . ':' . $update);
}
}
return $flatKeys;
}
protected function checkListPoints(array $update_ids) {
$current_progress = $this->checkListStateStorage
->setChecklistId('update_helper_checklist')
->getSavedProgress();
$user = $this->account
->id();
$time = time();
foreach ($update_ids as $update_id) {
if (empty($current_progress['#items'][$update_id])) {
$current_progress['#items'][$update_id] = [
'#completed' => time(),
'#uid' => $user,
];
}
}
$current_progress['#completed_items'] = count($current_progress['#items']);
$current_progress['#changed'] = $time;
$current_progress['#changed_by'] = $user;
$this->checkListStateStorage
->setChecklistId('update_helper_checklist')
->setSavedProgress($current_progress);
}
protected function checkAllListPoints($status = TRUE) {
$current_progress = $this->checkListStateStorage
->setChecklistId('update_helper_checklist')
->getSavedProgress();
$user = $this->account
->id();
$time = time();
$current_progress['#changed'] = $time;
$current_progress['#changed_by'] = $user;
$exclude = [
'#title',
'#description',
'#weight',
];
foreach ($this
->getChecklist()->items as $version_items) {
foreach ($version_items as $item_name => $item) {
if (!in_array($item_name, $exclude)) {
if ($status) {
$current_progress['#items'][$item_name] = [
'#completed' => $time,
'#uid' => $user,
];
}
else {
unset($current_progress['#items'][$item_name]);
}
}
}
}
$current_progress['#completed_items'] = empty($current_progress['#items']) ? 0 : count($current_progress['#items']);
$this->checkListStateStorage
->setChecklistId('update_helper_checklist')
->setSavedProgress($current_progress);
}
public function getUpdateVersions($module) {
$module_directories = $this->moduleHandler
->getModuleDirectories();
if (empty($module_directories[$module])) {
return [];
}
$updates_file = $module_directories[$module] . DIRECTORY_SEPARATOR . static::$updateChecklistFileName;
if (!is_file($updates_file)) {
return [];
}
$updates_checklist = Yaml::parse(file_get_contents($updates_file));
return array_keys($updates_checklist);
}
}