Skip to content

Commit

Permalink
Merge pull request #87 from neighborhoods/KOJO-154-include-metadata-i…
Browse files Browse the repository at this point in the history
…n-job-state-change-model

KOJO-154 | Include metadata in JobStateChange model
  • Loading branch information
mucha55 committed Oct 30, 2019
2 parents 685b1d3 + 0682854 commit 4249600
Show file tree
Hide file tree
Showing 40 changed files with 930 additions and 13 deletions.
78 changes: 78 additions & 0 deletions src/Data/Job/FromArrayBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Data\Job;

use Neighborhoods\Kojo\Data;
use Neighborhoods\Kojo\Data\JobInterface;

class FromArrayBuilder implements FromArrayBuilderInterface
{
/** @var array */
protected $record;

public function build() : JobInterface
{
$job = new Data\Job();
$record = $this->getRecord();

$job->setId($record['kojo_job_id']);
$job->setAssignedState($record['assigned_state']);
$job->setNextStateRequest($record['next_state_request']);
$job->setTypeCode($record['type_code']);
$job->setName($record['name']);
$job->setPriority($record['priority']);
$job->setImportance($record['importance']);
$job->setWorkAtDateTime(new \DateTime($record['work_at_date_time']));
$job->setPreviousState($record['previous_state']);
$job->setWorkerUri($record['worker_uri']);
$job->setWorkerMethod($record['worker_method']);
$job->setCanWorkInParallel($record['can_work_in_parallel']);
$job->setLastTransitionInDateTime(new \DateTime($record['last_transition_date_time']));
$job->setLastTransitionInMicroTime(
\DateTime::createFromFormat(
'U.u',
sprintf(
'%s.%s',
(int)($record['last_transition_micro_time'] / 1000000),
$record['last_transition_micro_time'] % 1000000
)
)
);
$job->setTimesWorked($record['times_worked']);
$job->setTimesRetried($record['times_retried']);
$job->setTimesHeld($record['times_held']);
$job->setTimesCrashed($record['times_crashed']);
$job->setTimesPanicked($record['times_panicked']);
$job->setCreatedAtDateTime(new \DateTime($record['created_at_date_time']));

if (isset($record['completed_at_date_time'])) {
$job->setCompletedAtDateTime(new \DateTime($record['completed_at_date_time']));
}
if (isset($record['delete_after_date_time'])) {
$job->setDeleteAfterDateTime(new \DateTime($record['delete_after_date_time']));
}

return $job;
}

protected function getRecord() : array
{
if ($this->record === null) {
throw new \LogicException('FromArrayBuilder record has not been set.');
}

return $this->record;
}

public function setRecord(array $record) : FromArrayBuilderInterface
{
if ($this->record !== null) {
throw new \LogicException('FromArrayBuilder record is already set.');
}

$this->record = $record;

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Data/Job/FromArrayBuilder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
neighborhoods.kojo.data.job.from_array_builder:
class: Neighborhoods\Kojo\Data\Job\FromArrayBuilder
shared: false
46 changes: 46 additions & 0 deletions src/Data/Job/FromArrayBuilder/AwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Data\Job\FromArrayBuilder;

use Neighborhoods\Kojo\Data\Job\FromArrayBuilderInterface;

/** @codeCoverageIgnore */
trait AwareTrait
{
protected $NeighborhoodsKojoDataJobFromArrayBuilder;

public function setDataJobFromArrayBuilder(FromArrayBuilderInterface $dataJobFromArrayBuilder) : self
{
if ($this->hasDataJobFromArrayBuilder()) {
throw new \LogicException('NeighborhoodsKojoDataJobFromArrayBuilder is already set.');
}
$this->NeighborhoodsKojoDataJobFromArrayBuilder = $dataJobFromArrayBuilder;

return $this;
}

protected function getDataJobFromArrayBuilder() : FromArrayBuilderInterface
{
if (!$this->hasDataJobFromArrayBuilder()) {
throw new \LogicException('NeighborhoodsKojoDataJobFromArrayBuilder is not set.');
}

return $this->NeighborhoodsKojoDataJobFromArrayBuilder;
}

protected function hasDataJobFromArrayBuilder() : bool
{
return isset($this->NeighborhoodsKojoDataJobFromArrayBuilder);
}

protected function unsetDataJobFromArrayBuilder() : self
{
if (!$this->hasDataJobFromArrayBuilder()) {
throw new \LogicException('NeighborhoodsKojoDataJobFromArrayBuilder is not set.');
}
unset($this->NeighborhoodsKojoDataJobFromArrayBuilder);

return $this;
}
}
17 changes: 17 additions & 0 deletions src/Data/Job/FromArrayBuilder/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Data\Job\FromArrayBuilder;

use Neighborhoods\Kojo\Data\Job\FromArrayBuilderInterface;

/** @codeCoverageIgnore */
class Factory implements FactoryInterface
{
use AwareTrait;

public function create() : FromArrayBuilderInterface
{
return clone $this->getDataJobFromArrayBuilder();
}
}
5 changes: 5 additions & 0 deletions src/Data/Job/FromArrayBuilder/Factory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
neighborhoods.kojo.data.job.from_array_builder.factory:
class: Neighborhoods\Kojo\Data\Job\FromArrayBuilder\Factory
calls:
- [setDataJobFromArrayBuilder, ['@neighborhoods.kojo.data.job.from_array_builder']]
46 changes: 46 additions & 0 deletions src/Data/Job/FromArrayBuilder/Factory/AwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Data\Job\FromArrayBuilder\Factory;

use Neighborhoods\Kojo\Data\Job\FromArrayBuilder\FactoryInterface;

/** @codeCoverageIgnore */
trait AwareTrait
{
protected $NeighborhoodsKojoDataJobFromArrayBuilderFactory;

public function setDataJobFromArrayBuilderFactory(FactoryInterface $dataJobFromArrayBuilderFactory) : self
{
if ($this->hasDataJobFromArrayBuilderFactory()) {
throw new \LogicException('NeighborhoodsKojoDataJobFromArrayBuilderFactory is already set.');
}
$this->NeighborhoodsKojoDataJobFromArrayBuilderFactory = $dataJobFromArrayBuilderFactory;

return $this;
}

protected function getDataJobFromArrayBuilderFactory() : FactoryInterface
{
if (!$this->hasDataJobFromArrayBuilderFactory()) {
throw new \LogicException('NeighborhoodsKojoDataJobFromArrayBuilderFactory is not set.');
}

return $this->NeighborhoodsKojoDataJobFromArrayBuilderFactory;
}

protected function hasDataJobFromArrayBuilderFactory() : bool
{
return isset($this->NeighborhoodsKojoDataJobFromArrayBuilderFactory);
}

protected function unsetDataJobFromArrayBuilderFactory() : self
{
if (!$this->hasDataJobFromArrayBuilderFactory()) {
throw new \LogicException('NeighborhoodsKojoDataJobFromArrayBuilderFactory is not set.');
}
unset($this->NeighborhoodsKojoDataJobFromArrayBuilderFactory);

return $this;
}
}
12 changes: 12 additions & 0 deletions src/Data/Job/FromArrayBuilder/FactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Data\Job\FromArrayBuilder;

use Neighborhoods\Kojo\Data\Job\FromArrayBuilderInterface;

/** @codeCoverageIgnore */
interface FactoryInterface
{
public function create() : FromArrayBuilderInterface;
}
13 changes: 13 additions & 0 deletions src/Data/Job/FromArrayBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Data\Job;

use Neighborhoods\Kojo\Data\JobInterface;

interface FromArrayBuilderInterface
{
public function build() : JobInterface;

public function setRecord(array $record) : FromArrayBuilderInterface;
}
8 changes: 4 additions & 4 deletions src/JobStateChange/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ class Builder implements BuilderInterface

public function build() : JobStateChangeInterface
{
$JobStateChange = $this->getJobStateChangeFactory()->create();
$jobStateChange = $this->getJobStateChangeFactory()->create();
$record = $this->getRecord();

$JobStateChange->setId((int)$record[JobStateChangeInterface::PROP_ID]);
$JobStateChange->setData(
$jobStateChange->setId((int)$record[JobStateChangeInterface::PROP_ID]);
$jobStateChange->setData(
$this
->getJobStateChangeDataBuilderFactory()
->create()
->setRecord($record[JobStateChangeInterface::PROP_DATA])
->build()
);

return $JobStateChange;
return $jobStateChange;
}

protected function getRecord() : array
Expand Down
23 changes: 21 additions & 2 deletions src/JobStateChange/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Neighborhoods\Kojo\JobStateChange;

use Neighborhoods\Kojo\Process\Pool\Logger\Message;

class Data implements DataInterface
{
/** @var string */
Expand All @@ -11,8 +13,8 @@ class Data implements DataInterface
protected $new_state;
/** @var \DateTimeInterface */
protected $timestamp;
/** @var ? */
// protected $metadata;
/** @var Message\MetadataInterface */
protected $metadata;

public function getOldState() : string
{
Expand Down Expand Up @@ -64,4 +66,21 @@ public function setTimestamp(\DateTimeInterface $timestamp) : DataInterface
$this->timestamp = $timestamp;
return $this;
}

public function getMetadata() : Message\MetadataInterface
{
if ($this->metadata === null) {
throw new \LogicException('Data metadata has not been set.');
}
return $this->metadata;
}

public function setMetadata(Message\MetadataInterface $metadata) : DataInterface
{
if ($this->metadata !== null) {
throw new \LogicException('Data metadata is already set.');
}
$this->metadata = $metadata;
return $this;
}
}
10 changes: 10 additions & 0 deletions src/JobStateChange/Data/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
namespace Neighborhoods\Kojo\JobStateChange\Data;

use Neighborhoods\Kojo\JobStateChange\DataInterface;
use Neighborhoods\Kojo\Process\Pool\Logger\Message\Metadata;

class Builder implements BuilderInterface
{
use Metadata\FromArrayBuilder\Factory\AwareTrait;
use Factory\AwareTrait;
/** @var array */
protected $record;
Expand All @@ -20,6 +22,14 @@ public function build() : DataInterface
$data->setNewState($record[DataInterface::PROP_NEW_STATE]);
$data->setTimestamp(new \DateTimeImmutable($record[DataInterface::PROP_TIMESTAMP]));

$metadata = $this
->getProcessPoolLoggerMessageMetadataFromArrayBuilderFactory()
->create()
->setRecord($record[DataInterface::PROP_METADATA])
->build();

$data->setMetadata($metadata);

return $data;
}

Expand Down
1 change: 1 addition & 0 deletions src/JobStateChange/Data/Builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
shared: false
calls:
- [setJobStateChangeDataFactory, ['@job_state_change.data.factory']]
- [setProcessPoolLoggerMessageMetadataFromArrayBuilderFactory, ['@neighborhoods.kojo.process.pool.logger.message.metadata.from_array_builder.factory']]
job_state_change.data.builder:
alias: neighborhoods.kojo.job_state_change.data.builder
public: false
7 changes: 6 additions & 1 deletion src/JobStateChange/DataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace Neighborhoods\Kojo\JobStateChange;

use Neighborhoods\Kojo\Process\Pool\Logger\Message;

interface DataInterface
{
public const PROP_OLD_STATE = 'old_state';
public const PROP_NEW_STATE = 'new_state';
public const PROP_TIMESTAMP = 'timestamp';
// public const PROP_METADATA = 'metadata';
public const PROP_METADATA = 'metadata';

public function getOldState() : string;
public function setOldState(string $old_state) : DataInterface;
Expand All @@ -18,4 +20,7 @@ public function setNewState(string $new_state) : DataInterface;

public function getTimestamp() : \DateTimeInterface;
public function setTimestamp(\DateTimeInterface $timestamp) : DataInterface;

public function getMetadata() : Message\MetadataInterface;
public function setMetadata(Message\MetadataInterface $metadata) : DataInterface;
}
Loading

0 comments on commit 4249600

Please sign in to comment.