Minor refactoring
MAILPOET-4986
This commit is contained in:
committed by
Aschepikov
parent
c747b71829
commit
fa43495d81
@@ -8,7 +8,6 @@ use MailPoetVendor\Carbon\CarbonImmutable;
|
||||
use MailPoetVendor\Doctrine\DBAL\Query\QueryBuilder;
|
||||
|
||||
abstract class DateFilter implements Filter {
|
||||
|
||||
const BEFORE = 'before';
|
||||
const AFTER = 'after';
|
||||
const ON = 'on';
|
||||
@@ -19,17 +18,13 @@ abstract class DateFilter implements Filter {
|
||||
abstract public function apply(QueryBuilder $queryBuilder, DynamicSegmentFilterEntity $filter): QueryBuilder;
|
||||
|
||||
protected function getValidOperators(): array {
|
||||
return [
|
||||
self::BEFORE,
|
||||
self::AFTER,
|
||||
self::ON,
|
||||
self::NOT_ON,
|
||||
self::IN_THE_LAST,
|
||||
self::NOT_IN_THE_LAST,
|
||||
];
|
||||
return array_merge(
|
||||
$this->getAbsoluteDateOperators(),
|
||||
$this->getRelativeDateOperators()
|
||||
);
|
||||
}
|
||||
|
||||
protected function getDateOperators() {
|
||||
protected function getAbsoluteDateOperators(): array {
|
||||
return [
|
||||
self::BEFORE,
|
||||
self::AFTER,
|
||||
@@ -38,15 +33,15 @@ abstract class DateFilter implements Filter {
|
||||
];
|
||||
}
|
||||
|
||||
protected function getRelativeDateOperators() {
|
||||
protected function getRelativeDateOperators(): array {
|
||||
return [
|
||||
self::IN_THE_LAST,
|
||||
self::NOT_IN_THE_LAST,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getDateForOperator(string $operator, string $value): string {
|
||||
if (in_array($operator, $this->getDateOperators())) {
|
||||
protected function getDateStringForOperator(string $operator, string $value): string {
|
||||
if (in_array($operator, $this->getAbsoluteDateOperators())) {
|
||||
$carbon = CarbonImmutable::createFromFormat('Y-m-d', $value);
|
||||
if (!$carbon instanceof CarbonImmutable) {
|
||||
throw new InvalidFilterException('Invalid date value', InvalidFilterException::INVALID_DATE_VALUE);
|
||||
|
@@ -15,7 +15,7 @@ class SubscriberSubscribedDate extends DateFilter {
|
||||
$value = $this->getDateValueFromFilter($filter);
|
||||
$parameterSuffix = $filter->getId() ?: Security::generateRandomString();
|
||||
$parameter = 'date' . $parameterSuffix;
|
||||
$date = $this->getDateForOperator($operator, $value);
|
||||
$date = $this->getDateStringForOperator($operator, $value);
|
||||
|
||||
if ($operator === self::BEFORE) {
|
||||
$queryBuilder->andWhere("DATE(last_subscribed_at) < :$parameter");
|
||||
|
@@ -29,7 +29,7 @@ class WooCommercePurchaseDate extends DateFilter {
|
||||
public function apply(QueryBuilder $queryBuilder, DynamicSegmentFilterEntity $filter): QueryBuilder {
|
||||
$operator = $this->getOperatorFromFilter($filter);
|
||||
$dateValue = $this->getDateValueFromFilter($filter);
|
||||
$date = $this->getDateForOperator($operator, $dateValue);
|
||||
$date = $this->getDateStringForOperator($operator, $dateValue);
|
||||
$subQuery = $this->getSubQuery($operator, $date);
|
||||
|
||||
if (in_array($operator, [self::NOT_ON, self::NOT_IN_THE_LAST])) {
|
||||
|
Reference in New Issue
Block a user