<?php
namespace Cms\ImportBundle\Entity;
use Cms\TenantBundle\Entity\TenantedEntity;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* Tracks and idividual import action for a given Import.
* For example, for imports that are able to be modified or updated, the newer sheet will involve creating a new ImportState entry.
* This also allows imports to be reverted and such.
*
* Class ImportState
* @package Cms\ImportBundle\Entity
*
* @ORM\Entity(
* repositoryClass = "Cms\ImportBundle\Doctrine\ImportStateRepository"
* )
* @ORM\Table(
* name = "cms__import__import_state"
* )
*/
class ImportState extends TenantedEntity
{
/**
* Link back to the Import entity for which this ImportState is for.
*
* @var Import
*
* @ORM\ManyToOne(
* targetEntity = "Cms\ImportBundle\Entity\Import"
* )
* @ORM\JoinColumn(
* name = "import",
* referencedColumnName = "id",
* onDelete = "CASCADE",
* nullable = false,
* )
*/
protected $import;
/**
* The time at which the state was uploaded.
* As new states are made on an upload of a document, this field should effectively never be NULL.
*
* @var DateTimeInterface|null
*
* @ORM\Column(
* type = "datetime",
* nullable = true,
* )
*/
protected $uploadedAt = null;
/**
* The type of the uploaded file, such as Excel, CSV, etc.
* As new states are made on an upload of a document, this field should effectively never be NULL.
*
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected $uploadedType = null;
/**
* The original filename of the uploaded document.
* As new states are made on an upload of a document, this field should effectively never be NULL.
*
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected $uploadedFilename = null;
/**
* Tracks the column names of the uploaded file.
* They should be properly numerically indexed.
*
* @var array
*
* @ORM\Column(
* type = "json",
* nullable = false,
* )
*/
protected $uploadedColumns = [];
/**
* The mappings from the Import column names to the column names in the sheet that was uploaded.
* If the mapping value for the Import is NULL, it means that the field should simply be ignored in the import, meaning existing data will be left untouched on existing entries in the system.
* If the mapping value for the Import is FALSE, it means that the field should be wiped in the import, meaning the existing data will be cleared from the existing entity.
* Otherwise, the mapping value should be a string which is the name of the column in the sheet.
* When creating a new state the mappings should be copied from the latest ImportState entity.
*
* @var array
*
* @ORM\Column(
* type = "json",
* nullable = false,
* )
*/
protected $mappings = [];
/**
* The time at which the state was processed.
* If this is NULL, it means that the state has not yet been processed properly.
*
* @var DateTimeInterface|null
*
* @ORM\Column(
* type = "datetime",
* nullable = true,
* )
*/
protected $processedAt = null;
/**
* The number of milliseconds it took for the import to process.
*
* @var int
*
* @ORM\Column(
* type = "integer",
* nullable = false,
* options = {
* "default" = 0,
* },
* )
*/
protected $processedTime = 0;
/**
* @return Import
*/
public function getImport(): Import
{
return $this->import;
}
/**
* @param Import $import
* @return $this
*/
public function setImport(Import $import): self
{
$this->import = $import;
return $this;
}
/**
* @return bool
*/
public function isUploaded(): bool
{
return ($this->getUploadedAt() instanceof DateTimeInterface);
}
/**
* @return DateTimeInterface|null
*/
public function getUploadedAt(): ?DateTimeInterface
{
return $this->uploadedAt;
}
/**
* @param DateTimeInterface $uploadedAt
* @return $this
*/
public function setUploadedAt(DateTimeInterface $uploadedAt): self
{
$this->uploadedAt = $uploadedAt;
return $this;
}
/**
* @return string|
* null
*/
public function getUploadedType(): ?string
{
return $this->uploadedType;
}
/**
* @param string $uploadedType
* @return $this
*/
public function setUploadedType(string $uploadedType): self
{
$this->uploadedType = $uploadedType;
return $this;
}
/**
* @return string|null
*/
public function getUploadedFilename(): ?string
{
return $this->uploadedFilename;
}
/**
* @param string $uploadedFilename
* @return $this
*/
public function setUploadedFilename(string $uploadedFilename): self
{
$this->uploadedFilename = $uploadedFilename;
return $this;
}
/**
* @return array|string[]
*/
public function getUploadedColumns(): array
{
return $this->uploadedColumns;
}
/**
* @param array|string[] $uploadedColumns
* @return $this
*/
public function setUploadedColumns(array $uploadedColumns): self
{
$this->uploadedColumns = $uploadedColumns;
return $this;
}
/**
* @return array|string[]
*/
public function getDisplayableColumns(): array
{
// TODO: can we just sort the other method call?
$columns = $this->getUploadedColumns();
sort($columns);
return $columns;
}
/**
* @return bool
*/
public function hasMappings(): bool
{
return ( ! empty($this->getMappings()));
}
/**
* @return bool
*/
public function isMapped(): bool
{
return $this->hasMappings();
}
/**
* @return array
*/
public function getMappings(): array
{
ksort($this->mappings);
return $this->mappings;
}
/**
* @param array $mappings
* @return $this
*/
public function setMappings(array $mappings): self
{
$this->mappings = $mappings;
ksort($this->mappings);
return $this;
}
/**
* @return bool
*/
public function isProcessed(): bool
{
return ($this->getProcessedAt() instanceof DateTimeInterface);
}
/**
* @return DateTimeInterface|null
*/
public function getProcessedAt(): ?DateTimeInterface
{
return $this->processedAt;
}
/**
* @param DateTimeInterface|null $processedAt
* @return $this
*/
public function setProcessedAt(?DateTimeInterface $processedAt): self
{
$this->processedAt = $processedAt;
return $this;
}
/**
* @return int
*/
public function getProcessedTime(): int
{
return $this->processedTime;
}
/**
* @param int $processedTime
* @return $this
*/
public function setProcessedTime(int $processedTime): self
{
$this->processedTime = $processedTime;
return $this;
}
}