KT-personal-website/app/Fetching/Gitea.php

78 lines
2.4 KiB
PHP
Raw Normal View History

2024-10-22 12:26:08 +02:00
<?php
namespace App\Fetching;
use App\Config;
class Gitea {
private $repoList = [];
private string $giteaUrl;
private $giteaFeed;
public string $giteaHash;
private int $numberEntry;
/**
* Constructor initializes the Gitea feed and sets the number of entries to display
*
* @param array $params Configuration parameters
* @return self
*/
public function __construct(array $params) {
$feed = new ApiFeed();
$this->giteaFeed = $feed->load($params['config']['fetching']['Gitea']);
$this->numberEntry = $params['config']['numberOfLastItem'];
return $this;
}
/**
* Retrieves a list of the most recently updated repositories that are not archived
*
* @return array List of repositories with their name, URL, and last update timestamp
*/
public function getLastRepo(): array {
$repoList = [];
if (count($this->giteaFeed->json) > 0) {
foreach ($this->giteaFeed->json as $value) {
if ($value['archived'] === false) {
$timestamp = new \DateTimeImmutable($value['updated_at']);
$timestamp = (int)$timestamp->format("U");
$repoList[$timestamp] = [
'name' => $value['name'],
'url' => $value['html_url'],
'update_at' => $value['updated_at']
];
}
}
krsort($repoList);
$repoList = array_slice($repoList, 0, $this->numberEntry);
$this->repoList = $repoList;
}
return $repoList;
}
/**
* Generates an HTML list of repositories from the repo list
*
* @return string|null The HTML string of the repository list or null if the repo list is empty
*/
public function makeList(): ?string {
if (!empty($this->repoList)) {
$htmlRepo = '<ul data-lastUpdate="' . date("Y-m-d") . '">';
foreach ($this->repoList as $value) {
$htmlRepo .=
'
<li>
<a href="' . $value['url'] . '">' . $value['name'] . '</a>
</li>';
}
$htmlRepo .= '
</ul>';
$this->giteaHash = sha1($htmlRepo);
return $htmlRepo;
}
return null;
}
}