| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailPoet\Statistics; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
|
| 8 |
use MailPoet\Doctrine\Repository; |
| 9 |
use MailPoet\Entities\FormEntity; |
| 10 |
use MailPoet\Entities\StatisticsFormEntity; |
| 11 |
use MailPoet\Entities\SubscriberEntity; |
| 12 |
|
| 13 |
/** |
| 14 |
* @extends Repository<StatisticsFormEntity> |
| 15 |
*/ |
| 16 |
class StatisticsFormsRepository extends Repository { |
| 17 |
protected function getEntityClassName() { |
| 18 |
return StatisticsFormEntity::class; |
| 19 |
} |
| 20 |
|
| 21 |
public function getTotalSignups(int $formId): int { |
| 22 |
return $this->countBy(['form' => $formId]); |
| 23 |
} |
| 24 |
|
| 25 |
public function record(FormEntity $form, SubscriberEntity $subscriber): ?StatisticsFormEntity { |
| 26 |
if ($form->getId() > 0 && $subscriber->getId() > 0) { |
| 27 |
// check if we already have a record for today |
| 28 |
$statisticsForm = $this->findOneBy(['form' => $form, 'subscriber' => $subscriber]); |
| 29 |
|
| 30 |
if (!$statisticsForm) { |
| 31 |
// create a new entry |
| 32 |
$statisticsForm = new StatisticsFormEntity($form, $subscriber); |
| 33 |
$this->persist($statisticsForm); |
| 34 |
$this->flush(); |
| 35 |
} |
| 36 |
return $statisticsForm; |
| 37 |
} |
| 38 |
return null; |
| 39 |
} |
| 40 |
} |
| 41 |
|