Skip to content

Commit

Permalink
Added option to check if record exists within certain period
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed Oct 12, 2022
1 parent e12ac4f commit 1350e63
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=8.1",
"doctrine/orm": "^2.0|^3.0",
"juniwalk/utils": ">=1.1.0"
"juniwalk/utils": ">=1.1.14"
},

"suggest": {
Expand Down
35 changes: 32 additions & 3 deletions src/Chronicler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

namespace JuniWalk\Nestor;

use JuniWalk\Nestor\Entity\Record;
use JuniWalk\Nestor\Enums\Type;
use JuniWalk\Nestor\Exceptions\RecordFailedException;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\EntityManagerInterface as EntityManager;
use Doctrine\ORM\ORMException;
use JuniWalk\Nestor\Entity\Record;
use JuniWalk\Nestor\Entity\RecordRepository;
use JuniWalk\Nestor\Enums\Type;
use JuniWalk\Nestor\Exceptions\RecordFailedException;
use JuniWalk\Nestor\Exceptions\RecordNotValidException;
use JuniWalk\Utils\Strings;

final class Chronicler
{
Expand All @@ -22,6 +25,7 @@ final class Chronicler
public function __construct(
private readonly string $entityName,
private readonly EntityManager $entityManager,
private readonly RecordRepository $recordRepository,
) {
if (!is_subclass_of($entityName, Record::class)) {
throw new RecordNotValidException;
Expand Down Expand Up @@ -68,6 +72,31 @@ public function record(Record $record): void
}


public function isRecorded(Record $record, string $period = null): bool
{
if (!$hash = $record->getHash()) {
return false;
}

$result = $this->recordRepository->findOneBy(function($qb) use ($hash, $period) {
$qb->andWhere('e.hash = :hash')->setParameter('hash', $hash);

if (is_null($period)) {
return $qb;
}

$dateEnd = new DateTime('-'.Strings::trim($period, '+-'));
$dateStart = new DateTime;

$qb->andWhere('e.date BETWEEN :dateStart AND :dateEnd')
->setParameter('dateStart', $dateStart->setTime(0, 0, 0))
->setParameter('dateEnd', $dateEnd->setTime(23, 59, 59));
});

return $result instanceof Record;
}


public function createRecord(string $event, string $message, iterable $params = []): RecordBuilder
{
return (new RecordBuilder($this))
Expand Down
21 changes: 18 additions & 3 deletions src/Entity/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ abstract class Record
#[ORM\Column(type: 'text', nullable: true)]
protected ?string $note;

#[ORM\Column(type: 'string', length: 8, nullable: true)]
protected ?string $hash;


final public function __construct(string $event, string $message)
{
Expand Down Expand Up @@ -148,7 +151,7 @@ public function isFinishable(): bool
}


public function setParams(iterable $params): void
public function setParams(array $params): void
{
$params = array_filter($params, function($v): bool {
return !is_null($v);
Expand All @@ -158,7 +161,7 @@ public function setParams(iterable $params): void
}


public function getParams(): iterable
public function getParams(): array
{
return $this->params ?: [];
}
Expand All @@ -170,7 +173,7 @@ public function getParam(string $key)//: mixed
}


public function getParamsUnified(): iterable
public function getParamsUnified(): array
{
return Arrays::flatten($this->params);
}
Expand All @@ -186,4 +189,16 @@ public function getNote(): ?string
{
return $this->note;
}


public function getHash(): ?string
{
return $this->hash;
}


protected function createUniqueHash(): string
{
return substr(sha1((string) $this), 0, 8);
}
}
29 changes: 29 additions & 0 deletions src/Entity/RecordRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

/**
* @copyright Martin Procházka (c) 2022
* @license MIT License
*/

namespace JuniWalk\Nestor\Entity;

use Doctrine\ORM\EntityManagerInterface as EntityManager;
use JuniWalk\Nestor\Exceptions\RecordNotValidException;
use JuniWalk\Utils\Exceptions\EntityNotFoundException;
use JuniWalk\Utils\ORM\AbstractRepository;

abstract class RecordRepository extends AbstractRepository
{
/**
* @throws RecordNotValidException
* @throws EntityNotFoundException
*/
final public function __construct(EntityManager $entityManager)
{
parent::__construct($entityManager);

if (!is_subclass_of($this->entityName, Record::class)) {
throw new RecordNotValidException;
}
}
}

0 comments on commit 1350e63

Please sign in to comment.