Replace usages of StatisticsForms with Doctrine
[MAILPOET-3032]
This commit is contained in:
@ -3,11 +3,18 @@
|
||||
namespace MailPoet\API\JSON\ResponseBuilders;
|
||||
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\Models\StatisticsForms;
|
||||
use MailPoet\Statistics\StatisticsFormsRepository;
|
||||
|
||||
class FormsResponseBuilder {
|
||||
const DATE_FORMAT = 'Y-m-d H:i:s';
|
||||
|
||||
/** @var StatisticsFormsRepository */
|
||||
private $statisticsFormsRepository;
|
||||
|
||||
public function __construct(StatisticsFormsRepository $statisticsFormsRepository) {
|
||||
$this->statisticsFormsRepository = $statisticsFormsRepository;
|
||||
}
|
||||
|
||||
public function build(FormEntity $form) {
|
||||
return [
|
||||
'id' => (string)$form->getId(), // (string) for BC
|
||||
@ -27,7 +34,7 @@ class FormsResponseBuilder {
|
||||
|
||||
foreach ($forms as $form) {
|
||||
$form = $this->build($form);
|
||||
$form['signups'] = StatisticsForms::getTotalSignups($form['id']);
|
||||
$form['signups'] = $this->statisticsFormsRepository->getTotalSignups($form['id']);
|
||||
$form['segments'] = (
|
||||
!empty($form['settings']['segments'])
|
||||
? $form['settings']['segments']
|
||||
|
@ -85,7 +85,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\NewsletterTemplatesResponseBuilder::class);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\CustomFieldsResponseBuilder::class);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\SubscribersResponseBuilder::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\FormsResponseBuilder::class);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\FormsResponseBuilder::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\SegmentsResponseBuilder::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder::class)->setPublic(true);
|
||||
// Automatic emails
|
||||
|
@ -3,7 +3,9 @@
|
||||
namespace MailPoet\Statistics;
|
||||
|
||||
use MailPoet\Doctrine\Repository;
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\Entities\StatisticsFormEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
|
||||
/**
|
||||
* @extends Repository<StatisticsFormEntity>
|
||||
@ -12,4 +14,24 @@ class StatisticsFormsRepository extends Repository {
|
||||
protected function getEntityClassName() {
|
||||
return StatisticsFormEntity::class;
|
||||
}
|
||||
|
||||
public function getTotalSignups(int $formId): int {
|
||||
return $this->countBy(['form' => $formId]);
|
||||
}
|
||||
|
||||
public function record(FormEntity $form, SubscriberEntity $subscriber): ?StatisticsFormEntity {
|
||||
if ($form->getId() > 0 && $subscriber->getId() > 0) {
|
||||
// check if we already have a record for today
|
||||
$statisticsForm = $this->findOneBy(['form' => $form, 'subscriber' => $subscriber]);
|
||||
|
||||
if (!$statisticsForm) {
|
||||
// create a new entry
|
||||
$statisticsForm = new StatisticsFormEntity($form, $subscriber);
|
||||
$this->persist($statisticsForm);
|
||||
$this->flush();
|
||||
}
|
||||
return $statisticsForm;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,9 @@ namespace MailPoet\Subscribers;
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\Form\FormsRepository;
|
||||
use MailPoet\Form\Util\FieldNameObfuscator;
|
||||
use MailPoet\Models\StatisticsForms;
|
||||
use MailPoet\NotFoundException;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\Statistics\StatisticsFormsRepository;
|
||||
use MailPoet\Subscription\Captcha;
|
||||
use MailPoet\Subscription\CaptchaSession;
|
||||
use MailPoet\Subscription\SubscriptionUrlFactory;
|
||||
@ -46,6 +46,9 @@ class SubscriberSubscribeController {
|
||||
/** @var SubscriptionThrottling */
|
||||
private $throttling;
|
||||
|
||||
/** @var StatisticsFormsRepository */
|
||||
private $statisticsFormsRepository;
|
||||
|
||||
public function __construct(
|
||||
Captcha $subscriptionCaptcha,
|
||||
CaptchaSession $captchaSession,
|
||||
@ -56,6 +59,7 @@ class SubscriberSubscribeController {
|
||||
RequiredCustomFieldValidator $requiredCustomFieldValidator,
|
||||
SettingsController $settings,
|
||||
FormsRepository $formsRepository,
|
||||
StatisticsFormsRepository $statisticsFormsRepository,
|
||||
WPFunctions $wp
|
||||
) {
|
||||
$this->formsRepository = $formsRepository;
|
||||
@ -68,6 +72,7 @@ class SubscriberSubscribeController {
|
||||
$this->subscriberActions = $subscriberActions;
|
||||
$this->wp = $wp;
|
||||
$this->throttling = $throttling;
|
||||
$this->statisticsFormsRepository = $statisticsFormsRepository;
|
||||
}
|
||||
|
||||
public function subscribe(array $data): array {
|
||||
@ -116,7 +121,7 @@ class SubscriberSubscribeController {
|
||||
}
|
||||
|
||||
// record form statistics
|
||||
StatisticsForms::record($form->getId(), $subscriber->getId());
|
||||
$this->statisticsFormsRepository->record($form, $subscriber);
|
||||
|
||||
$formSettings = $form->getSettings();
|
||||
if (!empty($formSettings['on_success'])) {
|
||||
|
@ -20,18 +20,21 @@ class FormsResponseBuilderTest extends \MailPoetTest {
|
||||
/** @var array */
|
||||
protected $formSettings;
|
||||
|
||||
/** @var FormsResponseBuilder */
|
||||
private $responseBuilder;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
|
||||
$this->container = ContainerWrapper::getInstance();
|
||||
$this->entityManager = $this->container->get(EntityManager::class);
|
||||
$this->responseBuilder = $this->container->get(FormsResponseBuilder::class);
|
||||
}
|
||||
|
||||
public function testItBuildsForm() {
|
||||
$form = $this->createForm('Form 1');
|
||||
|
||||
$responseBuilder = new FormsResponseBuilder();
|
||||
$response = $responseBuilder->build($form);
|
||||
$response = $this->responseBuilder->build($form);
|
||||
|
||||
expect($response['name'])->equals($this->formName);
|
||||
expect($response['status'])->equals(FormEntity::STATUS_ENABLED);
|
||||
@ -44,8 +47,7 @@ class FormsResponseBuilderTest extends \MailPoetTest {
|
||||
$form1 = $this->createForm('Form 1');
|
||||
$form2 = $this->createForm('Form 2');
|
||||
|
||||
$responseBuilder = new FormsResponseBuilder();
|
||||
$response = $responseBuilder->buildForListing([$form1, $form2]);
|
||||
$response = $this->responseBuilder->buildForListing([$form1, $form2]);
|
||||
|
||||
expect($response)->count(2);
|
||||
expect($response[0]['signups'])->equals(0);
|
||||
|
Reference in New Issue
Block a user