From fa43495d810952d9449f266d074494e48d9b4b3e Mon Sep 17 00:00:00 2001 From: John Oleksowicz Date: Mon, 27 Feb 2023 17:30:05 -0600 Subject: [PATCH] Minor refactoring MAILPOET-4986 --- .../DynamicSegments/Filters/DateFilter.php | 21 +++++++------------ .../Filters/SubscriberSubscribedDate.php | 2 +- .../Filters/WooCommercePurchaseDate.php | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/mailpoet/lib/Segments/DynamicSegments/Filters/DateFilter.php b/mailpoet/lib/Segments/DynamicSegments/Filters/DateFilter.php index 3124e58320..c9dc1fe3e6 100644 --- a/mailpoet/lib/Segments/DynamicSegments/Filters/DateFilter.php +++ b/mailpoet/lib/Segments/DynamicSegments/Filters/DateFilter.php @@ -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); diff --git a/mailpoet/lib/Segments/DynamicSegments/Filters/SubscriberSubscribedDate.php b/mailpoet/lib/Segments/DynamicSegments/Filters/SubscriberSubscribedDate.php index 59feec71f9..d58900e9a3 100644 --- a/mailpoet/lib/Segments/DynamicSegments/Filters/SubscriberSubscribedDate.php +++ b/mailpoet/lib/Segments/DynamicSegments/Filters/SubscriberSubscribedDate.php @@ -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"); diff --git a/mailpoet/lib/Segments/DynamicSegments/Filters/WooCommercePurchaseDate.php b/mailpoet/lib/Segments/DynamicSegments/Filters/WooCommercePurchaseDate.php index d5cb3bb6da..8f49c81898 100644 --- a/mailpoet/lib/Segments/DynamicSegments/Filters/WooCommercePurchaseDate.php +++ b/mailpoet/lib/Segments/DynamicSegments/Filters/WooCommercePurchaseDate.php @@ -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])) {