mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 01:33:11 +08:00
Create command to send visits to matomo
This commit is contained in:
@@ -40,7 +40,7 @@ readonly class SendVisitToMatomo
|
||||
}
|
||||
|
||||
try {
|
||||
$this->visitSender->sendVisitToMatomo($visit, $visitLocated->originalIpAddress);
|
||||
$this->visitSender->sendVisit($visit, $visitLocated->originalIpAddress);
|
||||
} catch (Throwable $e) {
|
||||
// Capture all exceptions to make sure this does not interfere with the regular execution
|
||||
$this->logger->error('An error occurred while trying to send visit to Matomo. {e}', ['e' => $e]);
|
||||
|
||||
@@ -4,18 +4,48 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Matomo;
|
||||
|
||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||
use Shlinkio\Shlink\Core\Matomo\Model\SendVisitsResult;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Visit\Repository\VisitIterationRepositoryInterface;
|
||||
use Throwable;
|
||||
|
||||
readonly class MatomoVisitSender implements MatomoVisitSenderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private MatomoTrackerBuilderInterface $trackerBuilder,
|
||||
private ShortUrlStringifier $shortUrlStringifier,
|
||||
private VisitIterationRepositoryInterface $visitIterationRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
public function sendVisitToMatomo(Visit $visit, ?string $originalIpAddress = null): void
|
||||
/**
|
||||
* Sends all visits in provided date range to matomo, and returns the amount of affected visits
|
||||
*/
|
||||
public function sendVisitsInDateRange(
|
||||
DateRange $dateRange,
|
||||
VisitSendingProgressTrackerInterface|null $progressTracker = null,
|
||||
): SendVisitsResult {
|
||||
$visitsIterator = $this->visitIterationRepository->findAllVisits($dateRange);
|
||||
$successfulVisits = 0;
|
||||
$failedVisits = 0;
|
||||
|
||||
foreach ($visitsIterator as $index => $visit) {
|
||||
try {
|
||||
$this->sendVisit($visit);
|
||||
$progressTracker?->success($index);
|
||||
$successfulVisits++;
|
||||
} catch (Throwable $e) {
|
||||
$progressTracker?->error($index, $e);
|
||||
$failedVisits++;
|
||||
}
|
||||
}
|
||||
|
||||
return new SendVisitsResult($successfulVisits, $failedVisits);
|
||||
}
|
||||
|
||||
public function sendVisit(Visit $visit, ?string $originalIpAddress = null): void
|
||||
{
|
||||
$tracker = $this->trackerBuilder->buildMatomoTracker();
|
||||
|
||||
|
||||
@@ -4,9 +4,19 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Matomo;
|
||||
|
||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||
use Shlinkio\Shlink\Core\Matomo\Model\SendVisitsResult;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
|
||||
interface MatomoVisitSenderInterface
|
||||
{
|
||||
public function sendVisitToMatomo(Visit $visit, ?string $originalIpAddress = null): void;
|
||||
/**
|
||||
* Sends all visits in provided date range to matomo, and returns the amount of affected visits
|
||||
*/
|
||||
public function sendVisitsInDateRange(
|
||||
DateRange $dateRange,
|
||||
VisitSendingProgressTrackerInterface|null $progressTracker = null,
|
||||
): SendVisitsResult;
|
||||
|
||||
public function sendVisit(Visit $visit, ?string $originalIpAddress = null): void;
|
||||
}
|
||||
|
||||
33
module/Core/src/Matomo/Model/SendVisitsResult.php
Normal file
33
module/Core/src/Matomo/Model/SendVisitsResult.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Matomo\Model;
|
||||
|
||||
use Countable;
|
||||
|
||||
final readonly class SendVisitsResult implements Countable
|
||||
{
|
||||
/**
|
||||
* @param int<0, max> $successfulVisits
|
||||
* @param int<0, max> $failedVisits
|
||||
*/
|
||||
public function __construct(public int $successfulVisits = 0, public int $failedVisits = 0)
|
||||
{
|
||||
}
|
||||
|
||||
public function hasSuccesses(): bool
|
||||
{
|
||||
return $this->successfulVisits > 0;
|
||||
}
|
||||
|
||||
public function hasFailures(): bool
|
||||
{
|
||||
return $this->failedVisits > 0;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return $this->successfulVisits + $this->failedVisits;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Matomo;
|
||||
|
||||
use Throwable;
|
||||
|
||||
interface VisitSendingProgressTrackerInterface
|
||||
{
|
||||
public function success(int $index): void;
|
||||
|
||||
public function error(int $index, Throwable $e): void;
|
||||
}
|
||||
Reference in New Issue
Block a user