Track
4 days ago
GATracking.php
6 months ago
StatisticsBouncesRepository.php
3 years ago
StatisticsClicksRepository.php
2 years ago
StatisticsFormsRepository.php
3 years ago
StatisticsNewslettersRepository.php
1 year ago
StatisticsOpensRepository.php
2 days ago
StatisticsUnsubscribesRepository.php
2 months ago
StatisticsWooCommercePurchasesRepository.php
1 year ago
UnsubscribeReasonTracker.php
2 months ago
UserAgentsRepository.php
1 year ago
index.php
3 years ago
UserAgentsRepository.php
41 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Statistics; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\Repository; |
| 9 | use MailPoet\Entities\UserAgentEntity; |
| 10 | |
| 11 | /** |
| 12 | * @extends Repository<UserAgentEntity> |
| 13 | */ |
| 14 | class UserAgentsRepository extends Repository { |
| 15 | protected function getEntityClassName() { |
| 16 | return UserAgentEntity::class; |
| 17 | } |
| 18 | |
| 19 | public function findOrCreate(string $userAgent): UserAgentEntity { |
| 20 | $hash = (string)crc32($userAgent); |
| 21 | $userAgentEntity = $this->findOneBy(['hash' => $hash]); |
| 22 | return $userAgentEntity ?? $this->create($userAgent); |
| 23 | } |
| 24 | |
| 25 | public function create(string $userAgent): UserAgentEntity { |
| 26 | $userAgentEntity = new UserAgentEntity($userAgent); |
| 27 | |
| 28 | $this->entityManager->getConnection()->executeStatement( |
| 29 | 'INSERT INTO ' . $this->getTableName() . ' (user_agent, hash) VALUES (:user_agent, :hash) ON DUPLICATE KEY UPDATE id = id', |
| 30 | [ |
| 31 | 'user_agent' => $userAgentEntity->getUserAgent(), |
| 32 | 'hash' => $userAgentEntity->getHash(), |
| 33 | ] |
| 34 | ); |
| 35 | |
| 36 | /** @var UserAgentEntity $userAgentEntity */ |
| 37 | $userAgentEntity = $this->findOneBy(['hash' => $userAgentEntity->getHash()]); |
| 38 | return $userAgentEntity; |
| 39 | } |
| 40 | } |
| 41 |