OpenXE/classes/Modules/SubscriptionCycle/Service/SubscriptionCycleJobService.php

142 lines
3.9 KiB
PHP
Raw Normal View History

2021-05-21 08:49:41 +02:00
<?php
declare(strict_types=1);
namespace Xentral\Modules\SubscriptionCycle\Service;
use DateTimeInterface;
use Xentral\Components\Database\Database;
use Xentral\Modules\SubscriptionCycle\Exception\InvalidArgumentException;
final class SubscriptionCycleJobService
{
private Database $db;
2021-05-21 08:49:41 +02:00
/**
* SubscriptionCycleJobService constructor.
*
* @param Database $db
*/
public function __construct(Database $db)
{
$this->db = $db;
}
/**
* @param int $subscriptionCycleJobId
*/
public function delete(int $subscriptionCycleJobId): void
{
$this->db->perform(
'DELETE FROM `subscription_cycle_job` WHERE `id` = :subscription_cycle_job_id',
['subscription_cycle_job_id' => $subscriptionCycleJobId]
);
}
/**
* @param int $addressId
* @param string $documentType
*
* @throws InvalidArgumentException
*/
public function deleteJobsByAddressIdAndDoctype(int $addressId, string $documentType): void
{
$this->ensureDocumentType($documentType);
$this->db->perform(
'DELETE FROM `subscription_cycle_job` WHERE `address_id` = :address_id AND `document_type` = :document_type',
[
'address_id' => $addressId,
'document_type' => $documentType,
]
);
}
/**
* @param int $addressId
* @param string $documentType
* @param string|null $jobType
* @param int|null $printerId
*
* @throws InvalidArgumentException
*
* @return int
*/
public function create(int $addressId, string $documentType, ?string $jobType = null, ?int $printerId = null): int
2021-05-21 08:49:41 +02:00
{
$this->ensureDocumentType($documentType);
$this->db->perform(
'INSERT INTO `subscription_cycle_job`
(`address_id`, `document_type`, `job_type`, `printer_id`, `created_at`)
VALUES (:address_id, :document_type, :job_type, :printer_id, NOW())',
2021-05-21 08:49:41 +02:00
[
'address_id' => $addressId,
'document_type' => $documentType,
'job_type' => $jobType,
'printer_id' => $printerId,
]
);
return $this->db->lastInsertId();
2021-05-21 08:49:41 +02:00
}
/**
* @param int $subscriptionCycleJobId
*
* @return array
*/
public function getJob(int $subscriptionCycleJobId): array
{
return $this->db->fetchRow(
'SELECT * FROM `subscription_cycle_job` WHERE `id` = :subscription_cycle_job_id',
['subscription_cycle_job_id' => $subscriptionCycleJobId]
);
}
/**
* @param int|null $limit
*
* @return array
*/
public function listAll(?int $limit = null): array
{
if ($limit !== null) {
return $this->db->fetchAll('SELECT * FROM `subscription_cycle_job` LIMIT :limit', ['limit' => $limit]);
}
return $this->db->fetchAll('SELECT * FROM `subscription_cycle_job`');
}
/**
* @param string $documentType
*
* @throws InvalidArgumentException
*
* @return int[]
*/
public function getAddressIdsByDocumentType(string $documentType): array
{
$this->ensureDocumentType($documentType);
return array_map(
'intval',
$this->db->fetchCol(
'SELECT `address_id` FROM `subscription_cycle_job` WHERE `document_type` = :document_type',
['document_type' => $documentType]
)
);
}
/**
* @param string $documentType
*
* @throws InvalidArgumentException
*/
private function ensureDocumentType(string $documentType): void
{
if (!in_array($documentType, ['rechnung', 'auftrag'])) {
throw new InvalidArgumentException("{$documentType} is not a valid documentType");
}
}
}