Extend Query definition
[PREMIUM-224]
This commit is contained in:
@ -5,7 +5,7 @@ namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Controller;
|
|||||||
use MailPoet\Automation\Engine\Data\Automation;
|
use MailPoet\Automation\Engine\Data\Automation;
|
||||||
use MailPoet\Automation\Engine\Data\Step;
|
use MailPoet\Automation\Engine\Data\Step;
|
||||||
use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction;
|
use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction;
|
||||||
use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\Query;
|
use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare;
|
||||||
use MailPoet\Entities\StatisticsClickEntity;
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
use MailPoet\Entities\StatisticsOpenEntity;
|
use MailPoet\Entities\StatisticsOpenEntity;
|
||||||
use MailPoet\Newsletter\NewslettersRepository;
|
use MailPoet\Newsletter\NewslettersRepository;
|
||||||
@ -33,7 +33,7 @@ class OverviewStatisticsController {
|
|||||||
$this->newsletterUrl = $newsletterUrl;
|
$this->newsletterUrl = $newsletterUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getStatisticsForAutomation(Automation $automation, Query $query): array {
|
public function getStatisticsForAutomation(Automation $automation, QueryWithCompare $query): array {
|
||||||
$emails = $this->getEmailsFromAutomation($automation);
|
$emails = $this->getEmailsFromAutomation($automation);
|
||||||
$data = [
|
$data = [
|
||||||
'sent' => ['current' => 0, 'previous' => 0],
|
'sent' => ['current' => 0, 'previous' => 0],
|
||||||
@ -57,8 +57,8 @@ class OverviewStatisticsController {
|
|||||||
|
|
||||||
$currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
$currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
||||||
$emails,
|
$emails,
|
||||||
$query->getPrimaryAfter(),
|
$query->getAfter(),
|
||||||
$query->getPrimaryBefore(),
|
$query->getBefore(),
|
||||||
$requiredData
|
$requiredData
|
||||||
);
|
);
|
||||||
foreach ($currentStatistics as $newsletterId => $statistic) {
|
foreach ($currentStatistics as $newsletterId => $statistic) {
|
||||||
@ -83,8 +83,8 @@ class OverviewStatisticsController {
|
|||||||
|
|
||||||
$previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
$previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
||||||
$emails,
|
$emails,
|
||||||
$query->getSecondaryAfter(),
|
$query->getCompareWithAfter(),
|
||||||
$query->getSecondaryBefore(),
|
$query->getCompareWithBefore(),
|
||||||
$requiredData
|
$requiredData
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ use MailPoet\Automation\Engine\API\Endpoint;
|
|||||||
use MailPoet\Automation\Engine\Exceptions\NotFoundException;
|
use MailPoet\Automation\Engine\Exceptions\NotFoundException;
|
||||||
use MailPoet\Automation\Engine\Storage\AutomationStorage;
|
use MailPoet\Automation\Engine\Storage\AutomationStorage;
|
||||||
use MailPoet\Automation\Integrations\MailPoet\Analytics\Controller\OverviewStatisticsController;
|
use MailPoet\Automation\Integrations\MailPoet\Analytics\Controller\OverviewStatisticsController;
|
||||||
use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\Query;
|
use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare;
|
||||||
use MailPoet\Validator\Builder;
|
use MailPoet\Validator\Builder;
|
||||||
|
|
||||||
class OverviewEndpoint extends Endpoint {
|
class OverviewEndpoint extends Endpoint {
|
||||||
@ -32,7 +32,7 @@ class OverviewEndpoint extends Endpoint {
|
|||||||
if (!$automation) {
|
if (!$automation) {
|
||||||
throw new NotFoundException(__('Automation not found', 'mailpoet'));
|
throw new NotFoundException(__('Automation not found', 'mailpoet'));
|
||||||
}
|
}
|
||||||
$query = Query::fromRequest($request);
|
$query = QueryWithCompare::fromRequest($request);
|
||||||
|
|
||||||
$result = $this->overviewStatisticsController->getStatisticsForAutomation($automation, $query);
|
$result = $this->overviewStatisticsController->getStatisticsForAutomation($automation, $query);
|
||||||
return new Response($result);
|
return new Response($result);
|
||||||
@ -41,22 +41,7 @@ class OverviewEndpoint extends Endpoint {
|
|||||||
public static function getRequestSchema(): array {
|
public static function getRequestSchema(): array {
|
||||||
return [
|
return [
|
||||||
'id' => Builder::integer()->required(),
|
'id' => Builder::integer()->required(),
|
||||||
'query' => Builder::object(
|
'query' => QueryWithCompare::getRequestSchema(),
|
||||||
[
|
|
||||||
'primary' => Builder::object(
|
|
||||||
[
|
|
||||||
'after' => Builder::string()->formatDateTime()->required(),
|
|
||||||
'before' => Builder::string()->formatDateTime()->required(),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'secondary' => Builder::object(
|
|
||||||
[
|
|
||||||
'after' => Builder::string()->formatDateTime()->required(),
|
|
||||||
'before' => Builder::string()->formatDateTime()->required(),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Entities;
|
|||||||
|
|
||||||
use MailPoet\API\REST\Request;
|
use MailPoet\API\REST\Request;
|
||||||
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
||||||
|
use MailPoet\Validator\Builder;
|
||||||
|
use MailPoet\Validator\Schema;
|
||||||
|
|
||||||
class Query {
|
class Query {
|
||||||
|
|
||||||
@ -13,68 +15,110 @@ class Query {
|
|||||||
/** @var \DateTimeImmutable */
|
/** @var \DateTimeImmutable */
|
||||||
private $primaryBefore;
|
private $primaryBefore;
|
||||||
|
|
||||||
/** @var \DateTimeImmutable */
|
/** @var int */
|
||||||
private $secondaryAfter;
|
private $limit;
|
||||||
|
|
||||||
/** @var \DateTimeImmutable */
|
/** @var string */
|
||||||
private $secondaryBefore;
|
private $orderBy;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private $orderDirection;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $page;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
\DateTimeImmutable $primaryAfter,
|
\DateTimeImmutable $primaryAfter,
|
||||||
\DateTimeImmutable $primaryBefore,
|
\DateTimeImmutable $primaryBefore,
|
||||||
\DateTimeImmutable $secondaryAfter,
|
int $limit = 25,
|
||||||
\DateTimeImmutable $secondaryBefore
|
string $orderBy = '',
|
||||||
|
string $orderDirection = 'asc',
|
||||||
|
int $page = 1
|
||||||
) {
|
) {
|
||||||
$this->primaryAfter = $primaryAfter;
|
$this->primaryAfter = $primaryAfter;
|
||||||
$this->primaryBefore = $primaryBefore;
|
$this->primaryBefore = $primaryBefore;
|
||||||
$this->secondaryAfter = $secondaryAfter;
|
$this->limit = $limit;
|
||||||
$this->secondaryBefore = $secondaryBefore;
|
$this->orderBy = $orderBy;
|
||||||
|
$this->orderDirection = $orderDirection;
|
||||||
|
$this->page = $page;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPrimaryAfter(): \DateTimeImmutable {
|
public function getAfter(): \DateTimeImmutable {
|
||||||
return $this->primaryAfter;
|
return $this->primaryAfter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPrimaryBefore(): \DateTimeImmutable {
|
public function getBefore(): \DateTimeImmutable {
|
||||||
return $this->primaryBefore;
|
return $this->primaryBefore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSecondaryAfter(): \DateTimeImmutable {
|
public function getLimit(): int {
|
||||||
return $this->secondaryAfter;
|
return $this->limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSecondaryBefore(): \DateTimeImmutable {
|
public function getOrderBy(): string {
|
||||||
return $this->secondaryBefore;
|
return $this->orderBy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromRequest(Request $request): self {
|
public function getOrderDirection(): string {
|
||||||
|
return $this->orderDirection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPage(): int {
|
||||||
|
return $this->page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
* @return Query
|
||||||
|
* @throws UnexpectedValueException
|
||||||
|
*/
|
||||||
|
public static function fromRequest(Request $request) {
|
||||||
$query = $request->getParam('query');
|
$query = $request->getParam('query');
|
||||||
if (!is_array($query)) {
|
if (!is_array($query)) {
|
||||||
throw new UnexpectedValueException('Invalid query parameters');
|
throw new UnexpectedValueException('Invalid query parameters');
|
||||||
}
|
}
|
||||||
$primary = $query['primary'] ?? null;
|
$primary = $query['primary'] ?? null;
|
||||||
$secondary = $query['secondary'] ?? null;
|
if (!is_array($primary)) {
|
||||||
if (!is_array($primary) || !is_array($secondary)) {
|
|
||||||
throw new UnexpectedValueException('Invalid query parameters');
|
throw new UnexpectedValueException('Invalid query parameters');
|
||||||
}
|
}
|
||||||
$primaryAfter = $primary['after'] ?? null;
|
$primaryAfter = $primary['after'] ?? null;
|
||||||
$primaryBefore = $primary['before'] ?? null;
|
$primaryBefore = $primary['before'] ?? null;
|
||||||
$secondaryAfter = $secondary['after'] ?? null;
|
|
||||||
$secondaryBefore = $secondary['before'] ?? null;
|
|
||||||
if (
|
if (
|
||||||
!is_string($primaryAfter) ||
|
!is_string($primaryAfter) ||
|
||||||
!is_string($primaryBefore) ||
|
!is_string($primaryBefore)
|
||||||
!is_string($secondaryAfter) ||
|
|
||||||
!is_string($secondaryBefore)
|
|
||||||
) {
|
) {
|
||||||
throw new UnexpectedValueException('Invalid query parameters');
|
throw new UnexpectedValueException('Invalid query parameters');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$limit = $query['limit'] ?? 25;
|
||||||
|
$orderBy = $query['order_by'] ?? '';
|
||||||
|
$orderDirection = isset($query['order']) && strtolower($query['order']) === 'asc' ? 'asc' : 'desc';
|
||||||
|
$page = $query['page'] ?? 1;
|
||||||
|
|
||||||
return new self(
|
return new self(
|
||||||
new \DateTimeImmutable($primaryAfter),
|
new \DateTimeImmutable($primaryAfter),
|
||||||
new \DateTimeImmutable($primaryBefore),
|
new \DateTimeImmutable($primaryBefore),
|
||||||
new \DateTimeImmutable($secondaryAfter),
|
$limit,
|
||||||
new \DateTimeImmutable($secondaryBefore)
|
$orderBy,
|
||||||
|
$orderDirection,
|
||||||
|
$page
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRequestSchema(): Schema {
|
||||||
|
return Builder::object(
|
||||||
|
[
|
||||||
|
'primary' => Builder::object(
|
||||||
|
[
|
||||||
|
'after' => Builder::string()->formatDateTime()->required(),
|
||||||
|
'before' => Builder::string()->formatDateTime()->required(),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
'limit' => Builder::integer()->minimum(1)->maximum(100),
|
||||||
|
'order_by' => Builder::string(),
|
||||||
|
'order' => Builder::string(),
|
||||||
|
'page' => Builder::integer()->minimum(1),
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,109 @@
|
|||||||
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Entities;
|
||||||
|
|
||||||
|
use MailPoet\API\REST\Request;
|
||||||
|
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
||||||
|
use MailPoet\Validator\Builder;
|
||||||
|
use MailPoet\Validator\Schema;
|
||||||
|
|
||||||
|
class QueryWithCompare extends Query {
|
||||||
|
|
||||||
|
/** @var \DateTimeImmutable */
|
||||||
|
private $secondaryAfter;
|
||||||
|
|
||||||
|
/** @var \DateTimeImmutable */
|
||||||
|
private $secondaryBefore;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
\DateTimeImmutable $primaryAfter,
|
||||||
|
\DateTimeImmutable $primaryBefore,
|
||||||
|
\DateTimeImmutable $secondaryAfter,
|
||||||
|
\DateTimeImmutable $secondaryBefore,
|
||||||
|
int $limit = 25,
|
||||||
|
string $orderBy = '',
|
||||||
|
string $orderDirection = 'asc',
|
||||||
|
int $page = 0
|
||||||
|
) {
|
||||||
|
parent::__construct($primaryAfter, $primaryBefore, $limit, $orderBy, $orderDirection, $page);
|
||||||
|
$this->secondaryAfter = $secondaryAfter;
|
||||||
|
$this->secondaryBefore = $secondaryBefore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCompareWithAfter(): \DateTimeImmutable {
|
||||||
|
return $this->secondaryAfter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCompareWithBefore(): \DateTimeImmutable {
|
||||||
|
return $this->secondaryBefore;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
* @return QueryWithCompare
|
||||||
|
* @throws UnexpectedValueException
|
||||||
|
*/
|
||||||
|
public static function fromRequest(Request $request) {
|
||||||
|
|
||||||
|
$query = $request->getParam('query');
|
||||||
|
if (!is_array($query)) {
|
||||||
|
throw new UnexpectedValueException('Invalid query parameters');
|
||||||
|
}
|
||||||
|
$primary = $query['primary'] ?? null;
|
||||||
|
$secondary = $query['secondary'] ?? null;
|
||||||
|
if (!is_array($primary) || !is_array($secondary)) {
|
||||||
|
throw new UnexpectedValueException('Invalid query parameters');
|
||||||
|
}
|
||||||
|
$primaryAfter = $primary['after'] ?? null;
|
||||||
|
$primaryBefore = $primary['before'] ?? null;
|
||||||
|
$secondaryAfter = $secondary['after'] ?? null;
|
||||||
|
$secondaryBefore = $secondary['before'] ?? null;
|
||||||
|
if (
|
||||||
|
!is_string($primaryAfter) ||
|
||||||
|
!is_string($primaryBefore) ||
|
||||||
|
!is_string($secondaryAfter) ||
|
||||||
|
!is_string($secondaryBefore)
|
||||||
|
) {
|
||||||
|
throw new UnexpectedValueException('Invalid query parameters');
|
||||||
|
}
|
||||||
|
|
||||||
|
$limit = $query['limit'] ?? 25;
|
||||||
|
$orderBy = $query['orderBy'] ?? '';
|
||||||
|
$orderDirection = $query['orderDirection'] ?? 'asc';
|
||||||
|
$page = $query['page'] ?? 0;
|
||||||
|
|
||||||
|
return new self(
|
||||||
|
new \DateTimeImmutable($primaryAfter),
|
||||||
|
new \DateTimeImmutable($primaryBefore),
|
||||||
|
new \DateTimeImmutable($secondaryAfter),
|
||||||
|
new \DateTimeImmutable($secondaryBefore),
|
||||||
|
$limit,
|
||||||
|
$orderBy,
|
||||||
|
$orderDirection,
|
||||||
|
$page
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRequestSchema(): Schema {
|
||||||
|
return Builder::object(
|
||||||
|
[
|
||||||
|
'primary' => Builder::object(
|
||||||
|
[
|
||||||
|
'after' => Builder::string()->formatDateTime()->required(),
|
||||||
|
'before' => Builder::string()->formatDateTime()->required(),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
'secondary' => Builder::object(
|
||||||
|
[
|
||||||
|
'after' => Builder::string()->formatDateTime()->required(),
|
||||||
|
'before' => Builder::string()->formatDateTime()->required(),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
'limit' => Builder::integer()->minimum(1)->maximum(100),
|
||||||
|
'orderBy' => Builder::string(),
|
||||||
|
'orderDirection' => Builder::string(),
|
||||||
|
'page' => Builder::integer()->minimum(1),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user