4
0

update 2023-06-21 02:09:59

This commit is contained in:
Robot 2023-06-21 02:09:59 +02:00
parent 579f9635c2
commit 9b28e87d96
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
4 changed files with 157 additions and 12 deletions

View File

@ -19,6 +19,8 @@ class Watcher << (F,LightGreen) >> #Green {
# Chapters $chapters
# Verses $verses
# bool $fresh
# string $today
# object $verse
+ __construct(Load $load, Insert $insert, ...)
+ api(string $translation, int $book, ...) : bool
- translation(string $translation) : ?string
@ -28,6 +30,8 @@ class Watcher << (F,LightGreen) >> #Green {
- update(string $translation, int $book, ...) : bool
- getVerse(int $number, array $verses) : ?object
- updateHash(string $translation, int $book, ...) : bool
- hold() : bool
- bump() : bool
}
note right of Watcher::__construct
@ -126,6 +130,20 @@ note right of Watcher::updateHash
int $chapter
string $hash
end note
note left of Watcher::hold
Check if its time to match the API hash
since: 2.0.1
return: bool
end note
note right of Watcher::bump
Bump the checking time
since: 2.0.1
return: bool
end note
@enduml
```

View File

@ -12,6 +12,7 @@
namespace VDM\Joomla\GetBible;
use Joomla\CMS\Date\Date;
use VDM\Joomla\GetBible\Database\Load;
use VDM\Joomla\GetBible\Database\Insert;
use VDM\Joomla\GetBible\Database\Update;
@ -92,6 +93,22 @@ final class Watcher
*/
protected bool $fresh = false;
/**
* The sql date of today
*
* @var string
* @since 2.0.1
*/
protected string $today;
/**
* The target verse
*
* @var object
* @since 2.0.1
*/
protected object $verse;
/**
* Constructor
*
@ -121,6 +138,9 @@ final class Watcher
$this->books = $books;
$this->chapters = $chapters;
$this->verses = $verses;
// just in-case we set a date
$this->today = (new Date())->toSql();
}
/**
@ -156,7 +176,7 @@ final class Watcher
// load the verses if not found
if ($this->verses($translation, $book, $chapter))
{
if ($this->fresh)
if ($this->fresh || $this->hold())
{
return true;
}
@ -167,7 +187,7 @@ final class Watcher
// confirm chapter hash has not changed
if (hash_equals($hash, $api_hash))
{
return true;
return $this->bump();
}
if ($this->update($translation, $book, $chapter))
@ -292,9 +312,9 @@ final class Watcher
private function verses(string $translation, int $book, int $chapter): bool
{
// check local value
if (($name = $this->load->value(
if (($this->verse = $this->load->item(
['abbreviation' => $translation, 'book_nr' => $book, 'chapter' => $chapter, 'verse' => 1],
'name', 'verse'
'verse'
)) !== null)
{
return true;
@ -351,6 +371,7 @@ final class Watcher
if (($object = $this->getVerse((int) $verse->verse, $api_verses)) !== null)
{
$object->id = $verse->id;
$object->created = $this->today;
$update[] = $object;
}
}
@ -409,12 +430,55 @@ final class Watcher
$update = [];
$update['id'] = $item->id;
$update['sha'] = $hash;
$update['created'] = $this->today;
// update the local verses
// update the local chapter
return $this->update->row($update, 'id', 'chapter');
}
return false;
}
/**
* Check if its time to match the API hash
*
* @return bool false if its time to check for an update
* @since 2.0.1
*/
private function hold(): bool
{
// Create DateTime objects from the strings
$today = new \DateTime($this->today);
$created = new \DateTime($this->verse->created);
// Calculate the difference
$interval = $today->diff($created);
// Check if the interval is more than 1 month
if ($interval->m >= 1 || $interval->y >= 1)
{
return false; // More than a month, it's time to check for an update
}
else
{
return true; // Within the last month, hold off on the update check
}
}
/**
* Bump the checking time
*
* @return bool true when the update was a success
* @since 2.0.1
*/
private function bump(): bool
{
$update = [];
$update['id'] = $this->verse->id;
$update['created'] = $this->today;
// update the local verse
return $this->update->row($update, 'id', 'verse');
}
}

View File

@ -62,6 +62,22 @@
*/
protected bool $fresh = false;
/**
* The sql date of today
*
* @var string
* @since 2.0.1
*/
protected string $today;
/**
* The target verse
*
* @var object
* @since 2.0.1
*/
protected object $verse;
/**
* Constructor
*
@ -91,6 +107,9 @@
$this->books = $books;
$this->chapters = $chapters;
$this->verses = $verses;
// just in-case we set a date
$this->today = (new Date())->toSql();
}
/**
@ -126,7 +145,7 @@
// load the verses if not found
if ($this->verses($translation, $book, $chapter))
{
if ($this->fresh)
if ($this->fresh || $this->hold())
{
return true;
}
@ -137,7 +156,7 @@
// confirm chapter hash has not changed
if (hash_equals($hash, $api_hash))
{
return true;
return $this->bump();
}
if ($this->update($translation, $book, $chapter))
@ -262,9 +281,9 @@
private function verses(string $translation, int $book, int $chapter): bool
{
// check local value
if (($name = $this->load->value(
if (($this->verse = $this->load->item(
['abbreviation' => $translation, 'book_nr' => $book, 'chapter' => $chapter, 'verse' => 1],
'name', 'verse'
'verse'
)) !== null)
{
return true;
@ -321,6 +340,7 @@
if (($object = $this->getVerse((int) $verse->verse, $api_verses)) !== null)
{
$object->id = $verse->id;
$object->created = $this->today;
$update[] = $object;
}
}
@ -379,10 +399,53 @@
$update = [];
$update['id'] = $item->id;
$update['sha'] = $hash;
$update['created'] = $this->today;
// update the local verses
// update the local chapter
return $this->update->row($update, 'id', 'chapter');
}
return false;
}
/**
* Check if its time to match the API hash
*
* @return bool false if its time to check for an update
* @since 2.0.1
*/
private function hold(): bool
{
// Create DateTime objects from the strings
$today = new \DateTime($this->today);
$created = new \DateTime($this->verse->created);
// Calculate the difference
$interval = $today->diff($created);
// Check if the interval is more than 1 month
if ($interval->m >= 1 || $interval->y >= 1)
{
return false; // More than a month, it's time to check for an update
}
else
{
return true; // Within the last month, hold off on the update check
}
}
/**
* Bump the checking time
*
* @return bool true when the update was a success
* @since 2.0.1
*/
private function bump(): bool
{
$update = [];
$update['id'] = $this->verse->id;
$update['created'] = $this->today;
// update the local verse
return $this->update->row($update, 'id', 'verse');
}

View File

@ -1,5 +1,5 @@
{
"add_head": "0",
"add_head": "1",
"add_licensing_template": "2",
"extends": "0",
"guid": "f815fb33-f721-48a5-a84e-53f1986e8881",
@ -42,6 +42,6 @@
"namespace": "VDM\\Joomla\\GetBible\\Watcher",
"description": "The GetBible Watcher\r\n\r\n@since 2.0.1",
"licensing_template": "\/**\r\n * @package GetBible\r\n *\r\n * @created 30th May, 2023\r\n * @author Llewellyn van der Merwe <https:\/\/dev.vdm.io>\r\n * @git GetBible <https:\/\/git.vdm.dev\/getBible>\r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n",
"head": "",
"head": "use Joomla\\CMS\\Date\\Date;",
"composer": ""
}