Skip to content

Commit 02cefcc

Browse files
committed
Added IP row in "alert" column
1 parent 7bf2713 commit 02cefcc

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

migrations/Version20220730170339.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20220730170339 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE aln_alert ADD ip VARCHAR(64) DEFAULT NULL, DROP time, DROP amount');
24+
}
25+
26+
public function down(Schema $schema): void
27+
{
28+
// this down() migration is auto-generated, please modify it to your needs
29+
$this->addSql('ALTER TABLE aln_alert ADD time VARCHAR(5) DEFAULT NULL, ADD amount SMALLINT DEFAULT NULL, DROP ip');
30+
}
31+
}

src/Entity/AlnAlert.php

+6-31
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace App\Entity;
66

7-
use App\Dbal\Types\AlnTimeType;
87
use App\Repository\AlnAlertRepository;
98
use Doctrine\DBAL\Types\Types;
109
use Doctrine\ORM\Mapping as ORM;
@@ -30,14 +29,8 @@ class AlnAlert
3029
#[ORM\ManyToOne]
3130
private ?AlnFeeder $feeder = null;
3231

33-
/**
34-
* @var ?array{hours: int<0, 23>, minutes: int<0, 59>}
35-
*/
36-
#[ORM\Column(type: AlnTimeType::ALN_TIME_TYPE, nullable: true)]
37-
private ?array $time = null;
38-
39-
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
40-
private ?int $amount = null;
32+
#[ORM\Column(length: 64, nullable: true)]
33+
private ?string $ip = null;
4134

4235
public function __construct()
4336
{
@@ -98,32 +91,14 @@ public function setFeeder(?AlnFeeder $feeder): self
9891
return $this;
9992
}
10093

101-
/**
102-
* @return ?array{hours: int<0, 23>, minutes: int<0, 59>}
103-
*/
104-
public function getTime(): ?array
105-
{
106-
return $this->time;
107-
}
108-
109-
/**
110-
* @param ?array{hours: int<0, 23>, minutes: int<0, 59>} $time
111-
*/
112-
public function setTime(?array $time): self
113-
{
114-
$this->time = $time;
115-
116-
return $this;
117-
}
118-
119-
public function getAmount(): ?int
94+
public function getIp(): ?string
12095
{
121-
return $this->amount;
96+
return $this->ip;
12297
}
12398

124-
public function setAmount(?int $amount): self
99+
public function setIp(?string $ip): self
125100
{
126-
$this->amount = $amount;
101+
$this->ip = $ip;
127102

128103
return $this;
129104
}

src/Socket/FeederCommunicator.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public function onData(ConnectionInterface $from, string $data): void
9090
$this->logger->warning($e->getMessage(), ['exception' => $e]);
9191

9292
$alert = new AlnAlert();
93+
if ($ip = $this->getIpFrom($from)) {
94+
$alert->setIp($ip);
95+
}
9396
$alert->setMessage($e->getMessage());
9497
if ($identifier = array_search($from, $this->connections, true)) {
9598
$feeder = $this->feederRepository->findOneByIdentifier($identifier);
@@ -153,11 +156,7 @@ private function identified(IdentificationMessage $message, ConnectionInterface
153156

154157
$feeder = $this->feederRepository->findOrCreateFeeder($message->getIdentifier());
155158
$feeder->setLastSeen(new DateTimeImmutable('now'));
156-
157-
if ($address = $connection->getRemoteAddress()) {
158-
$host = parse_url($address, PHP_URL_HOST);
159-
assert(is_string($host));
160-
$ip = trim($host, '[]');
159+
if ($ip = $this->getIpFrom($connection)) {
161160
$feeder->setIp($ip);
162161
}
163162

@@ -183,4 +182,17 @@ private function recordManualMeal(MealButtonPressedMessage $message): void
183182
$this->doctrine->getManager()->flush();
184183
// TODO: send push?
185184
}
185+
186+
private function getIpFrom(ConnectionInterface $connection): ?string
187+
{
188+
if (!$address = $connection->getRemoteAddress()) {
189+
return null;
190+
}
191+
$host = parse_url($address, PHP_URL_HOST);
192+
if (!is_string($host)) {
193+
return null;
194+
}
195+
196+
return trim($host, '[]');
197+
}
186198
}

0 commit comments

Comments
 (0)