Skip to content

Commit bc135a7

Browse files
committed
Tracking Mode base
1 parent 354629a commit bc135a7

File tree

8 files changed

+149
-3
lines changed

8 files changed

+149
-3
lines changed

config/container.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
$container->add(\App\Controller\URLShortener\LinkController::class)
3232
->addArgument(League\Plates\Engine::class)
3333
->addArgument(\App\Service\UrlShortener\ShortlinkService::class)
34-
->addArgument(\App\Service\UrlShortener\ShortlinkPasswordService::class);
34+
->addArgument(\App\Service\UrlShortener\ShortlinkPasswordService::class)
35+
->addArgument(\App\Service\UrlShortener\ShortlinkTrackingService::class);
3536

3637
$container->add(\App\Controller\Administration\DashboardController::class)
3738
->addArgument(\League\Plates\Engine::class);
@@ -61,6 +62,9 @@
6162

6263
$container->add(\App\Service\UrlShortener\ShortlinkPasswordService::class);
6364

65+
$container->add(\App\Service\UrlShortener\ShortlinkTrackingService::class)
66+
->addArgument(\App\Table\UrlShortener\ShortlinkTrackingTable::class);
67+
6468
#
6569
# Repositories
6670
#
@@ -76,6 +80,9 @@
7680
$container->add(\App\Table\UrlShortener\ShortlinkTable::class)
7781
->addArgument(\Envms\FluentPDO\Query::class);
7882

83+
$container->add(\App\Table\UrlShortener\ShortlinkTrackingTable::class)
84+
->addArgument(\Envms\FluentPDO\Query::class);
85+
7986
#
8087
# Validations
8188
#

src/App/Controller/URLShortener/CreateController.php

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use App\Service\UrlShortener\ShortlinkPasswordService;
1010
use App\Service\UrlShortener\ShortlinkService;
1111
use League\Plates\Engine;
12-
use Psr\Http\Message\RequestInterface;
1312
use Psr\Http\Message\ResponseInterface;
1413
use Psr\Http\Message\ServerRequestInterface;
1514

src/App/Controller/URLShortener/LinkController.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use App\Http\HtmlResponse;
66
use App\Model\UrlShortener\Shortlink;
7+
use App\Model\UrlShortener\ShortlinkTracking;
78
use App\Service\UrlShortener\ShortlinkPasswordService;
89
use App\Service\UrlShortener\ShortlinkService;
10+
use App\Service\UrlShortener\ShortlinkTrackingService;
911
use Laminas\Diactoros\Response\RedirectResponse;
1012
use League\Plates\Engine;
1113
use Psr\Http\Message\ResponseInterface;
@@ -17,7 +19,8 @@ class LinkController
1719
public function __construct(
1820
private readonly Engine $template,
1921
private readonly ShortlinkService $shortlinkService,
20-
private readonly ShortlinkPasswordService $passwordService
22+
private readonly ShortlinkPasswordService $passwordService,
23+
private readonly ShortlinkTrackingService $shortlinkTrackingService
2124
)
2225
{
2326
}
@@ -38,6 +41,7 @@ public function load(ServerRequestInterface $request, array $args): ResponseInte
3841

3942
if(empty($shortlink->getDestination()))
4043
{
44+
MESSAGES->add('danger', 'url-shortener-link-not-found');
4145
return new HtmlResponse($this->template->render('urlShortener/linkInformation'));
4246
}
4347

@@ -58,6 +62,16 @@ public function load(ServerRequestInterface $request, array $args): ResponseInte
5862
return new HtmlResponse($this->template->render('urlShortener/linkInformation'));
5963
}
6064

65+
if($shortlink->isTracking())
66+
{
67+
$tracking = new ShortlinkTracking();
68+
$tracking->setLink($shortlink->getId());
69+
$tracking->setUserIp($_SERVER['REMOTE_ADDR']);
70+
$tracking->setUseragent($_SERVER['HTTP_USER_AGENT']);
71+
$tracking->setAccessed(new \DateTime());
72+
$this->shortlinkTrackingService->track($tracking);
73+
}
74+
6175
return new RedirectResponse($shortlink->getDestination());
6276

6377
}

src/App/Model/UrlShortener/Shortlink.php

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Shortlink
1313
private string $destination;
1414
private ?\DateTime $expiryDate = null;
1515
private ?string $password = null;
16+
private bool $tracking = false;
1617

1718
public function getId(): int
1819
{
@@ -94,4 +95,14 @@ public function setPassword(?string $password): void
9495
$this->password = $password;
9596
}
9697

98+
public function isTracking(): bool
99+
{
100+
return $this->tracking;
101+
}
102+
103+
public function setTracking(bool $tracking): void
104+
{
105+
$this->tracking = $tracking;
106+
}
107+
97108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Model\UrlShortener;
4+
5+
class ShortlinkTracking
6+
{
7+
8+
private int $id;
9+
private int $link;
10+
private string $useragent;
11+
private string $userIp;
12+
private \DateTime $accessed;
13+
14+
public function getId(): int
15+
{
16+
return $this->id;
17+
}
18+
19+
public function setId(int $id): void
20+
{
21+
$this->id = $id;
22+
}
23+
24+
public function getLink(): int
25+
{
26+
return $this->link;
27+
}
28+
29+
public function setLink(int $link): void
30+
{
31+
$this->link = $link;
32+
}
33+
34+
public function getUseragent(): string
35+
{
36+
return $this->useragent;
37+
}
38+
39+
public function setUseragent(string $useragent): void
40+
{
41+
$this->useragent = $useragent;
42+
}
43+
44+
public function getUserIp(): string
45+
{
46+
return $this->userIp;
47+
}
48+
49+
public function setUserIp(string $userIp): void
50+
{
51+
$this->userIp = $userIp;
52+
}
53+
54+
public function getAccessed(): \DateTime
55+
{
56+
return $this->accessed;
57+
}
58+
59+
public function setAccessed(\DateTime $accessed): void
60+
{
61+
$this->accessed = $accessed;
62+
}
63+
64+
}

src/App/Service/UrlShortener/ShortlinkService.php

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function openShortlink(Shortlink $shortlink): void
6060
$shortlink->setId($shortlinkData['id']);
6161
$shortlink->setDestination($shortlinkData['destination']);
6262
$shortlink->setPassword($shortlinkData['password']);
63+
$shortlink->setTracking($shortlinkData['tracking'] == 1);
6364
if($shortlinkData['expiryDate'] !== NULL)
6465
{
6566
$shortlink->setExpiryDate(new \DateTime($shortlinkData['expiryDate']));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Service\UrlShortener;
4+
5+
use App\Model\UrlShortener\ShortlinkTracking;
6+
use App\Table\UrlShortener\ShortlinkTrackingTable;
7+
8+
class ShortlinkTrackingService
9+
{
10+
11+
public function __construct(
12+
private readonly ShortlinkTrackingTable $shortlinkTrackingTable
13+
)
14+
{
15+
}
16+
17+
public function track(ShortlinkTracking $tracking)
18+
{
19+
20+
$this->shortlinkTrackingTable->create($tracking);
21+
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Table\UrlShortener;
4+
5+
use App\Model\UrlShortener\ShortlinkTracking;
6+
use App\Software;
7+
use App\Table\AbstractTable;
8+
9+
class ShortlinkTrackingTable extends AbstractTable
10+
{
11+
12+
public function create(ShortlinkTracking $shortlinkTracking): bool|array
13+
{
14+
15+
$values = [
16+
'link' => $shortlinkTracking->getLink(),
17+
'useragent' => $shortlinkTracking->getUseragent(),
18+
'userIp' => $shortlinkTracking->getUserIp(),
19+
'accessed' => $shortlinkTracking->getAccessed()->format(Software::DATABASE_TIME_FORMAT)
20+
];
21+
22+
return $this->query->insertInto($this->getTableName())->values($values)->executeWithoutId();
23+
24+
}
25+
26+
}

0 commit comments

Comments
 (0)