Skip to content

Commit

Permalink
Improved RecordBuilder::create method
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed May 12, 2022
1 parent 581e121 commit 292650d
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/RecordBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
namespace JuniWalk\Nestor;

use JuniWalk\Nestor\Entity\Record;
use JuniWalk\Nestor\Exceptions\RecordNotValidException;

final class RecordBuilder
{
/** @var Chronicler */
private $chronicler;

/** @var string[] */
private $data;
private $record;


/**
Expand All @@ -29,13 +30,21 @@ public function __construct(Chronicler $chronicler)

/**
* @return Record
* @throws RecordNotValidException
*/
public function create(): Record
{
$entity = $this->chronicler->getEntityName();
$record = new $entity($this->data['event'], $this->data['message']);
$entityName = $this->chronicler->getEntityName();
$record = new $entityName(
$this->record['event'],
$this->record['message']
);

foreach ($this->record as $key => $value) {
if (!method_exists($record, 'set'.$key)) {
throw new RecordNotValidException;
}

foreach ($this->data as $key => $value) {
$record->{'set'.$key}($value);
}

Expand All @@ -58,7 +67,7 @@ public function record(): void
*/
public function withType(string $type): self
{
$this->data['type'] = $type;
$this->record['type'] = $type;
return $this;
}

Expand All @@ -69,7 +78,7 @@ public function withType(string $type): self
*/
public function withLevel(string $level): self
{
$this->data['level'] = $level;
$this->record['level'] = $level;
return $this;
}

Expand All @@ -80,7 +89,7 @@ public function withLevel(string $level): self
*/
public function withMessage(string $message): self
{
$this->data['message'] = $message;
$this->record['message'] = $message;
return $this;
}

Expand All @@ -91,7 +100,18 @@ public function withMessage(string $message): self
*/
public function withEvent(?string $event): self
{
$this->data['event'] = $event;
$this->record['event'] = $event;
return $this;
}


/**
* @param mixed[] $params
* @return static
*/
public function withParams(iterable $params): self
{
$this->record['params'] = $params;
return $this;
}

Expand All @@ -103,7 +123,7 @@ public function withEvent(?string $event): self
*/
public function withParam(string $name, $value): self
{
$this->data['params'][$name] = $value;
$this->record['params'][$name] = $value;
return $this;
}
}

0 comments on commit 292650d

Please sign in to comment.