Files
emails/api/src/Repository/Attachment.php
2022-11-25 20:52:52 -03:00

95 lines
2.6 KiB
PHP

<?php
namespace ProVM\Emails\Repository;
use PDO;
use Psr\Log\LoggerInterface;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Implement\Repository;
class Attachment extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, State\Attachment $stateRepository)
{
parent::__construct($connection, $logger);
$this->setStateRepository($stateRepository)
->setTable('attachments');
}
protected State\Attachment $stateRepository;
public function getStateRepository(): State\Attachment
{
return $this->stateRepository;
}
public function setStateRepository(State\Attachment $repository): Attachment
{
$this->stateRepository = $repository;
return $this;
}
protected function idField(): string
{
return 'filename';
}
protected function fieldsForInsert(): array
{
return array_merge($this->fieldsForUpdate(), ['filename']);
}
protected function fieldsForUpdate(): array
{
return [
'message_id'
];
}
protected function fieldsForCreate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForUpdate(ModelInterface $model): array
{
return [
$model->getMessage()->getId(),
$model->getFilename()
];
}
protected function valuesForInsert(ModelInterface $model): array
{
return $this->valuesForUpdate($model);
}
protected function defaultFind(ModelInterface $model): ModelInterface
{
return $this->fetchByFilename($model->getFilename());
}
protected function valuesForCreate(array $data): array
{
return [
$data['message_id'],
$data['filename']
];
}
protected function defaultSearch(array $data): ModelInterface
{
return $this->fetchByFilename($data['filename']);
}
public function load(array $row): \ProVM\Emails\Model\Attachment
{
$model = new \ProVM\Emails\Model\Attachment();
$model
->setFilename($row['filename'])
->setStateRepository($this->getStateRepository());
return $model;
}
public function fetchByFilename(string $filename): \ProVM\Emails\Model\Attachment
{
$query = "SELECT * FROM {$this->getTable()} WHERE filename = ?";
return $this->fetchOne($query, [$filename]);
}
public function fetchByMessage(int $message_id): array
{
$query = "SELECT * FROM {$this->getTable()} WHERE message_id = ?";
return $this->fetchMany($query, [$message_id]);
}
}