Skip to content

Commit 12dc928

Browse files
Add logo for urls: update database.sql, UrlController.php, DAO/Url.php, UrlRepository.php, urls/index.html.twig, urls/show.html.twig
Add new column: logo varchar(500) to the database.sql. Add new $logo property with setter and getter to the DAO/Url.php. Update UrlRepository.php for the new logo column. Add getting logo path to the createAction() method in the UrlController.php, also update showAllAction() method. Add <img> tag for the logo output to the urls/index.html.twig and urls/show.html.twig views.
1 parent af83e98 commit 12dc928

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

database.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CREATE TABLE IF NOT EXISTS urls (
22
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
3+
logo varchar(500),
34
name varchar(255) UNIQUE NOT NULL,
45
created_at timestamp NOT NULL
56
);

src/Controllers/UrlController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ public function createAction(Request $request, Response $response): ResponseInte
103103
return $response->withRedirect($this->router->urlFor('urls.show', ['id' => $id]), 302);
104104
}
105105

106-
$id = (string) $this->urlRepository->create($domain[0] ?? '', $createdAt);
106+
$client = new Client();
107+
$document = new Document($client->request('GET', $name)->getBody()->getContents());
108+
$logo = optional($document->first('link[rel=icon]'))->getAttribute('href');
109+
110+
$id = (string) $this->urlRepository->create($logo ?? '', $domain[0] ?? '', $createdAt);
107111
$this->flash->addMessage('success', 'Страница успешно добавлена');
108112
return $response->withRedirect($this->router->urlFor('urls.show', ['id' => $id]), 302);
109113
}
@@ -119,6 +123,7 @@ public function showAllAction(Request $request, Response $response): ResponseInt
119123

120124
$url = new Url($row['name']);
121125
$url->setId($row['id']);
126+
$url->setLogo($row['logo']);
122127
$url->setCreatedAt($row['created_at']);
123128

124129
$lastCreatedAt = $check['created_at'] ?? '';

src/DAO/Url.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class Url
66
{
77
private int $id;
8+
private string $logo;
89
private string $name;
910
private string $createdAt;
1011
private ?string $lastCheck;
@@ -20,6 +21,11 @@ public function setId(int $id): void
2021
$this->id = $id;
2122
}
2223

24+
public function setLogo(string $logo): void
25+
{
26+
$this->logo = $logo;
27+
}
28+
2329
public function setName(string $name): void
2430
{
2531
$this->name = $name;
@@ -45,6 +51,11 @@ public function getId(): int
4551
return $this->id;
4652
}
4753

54+
public function getLogo(): string
55+
{
56+
return $this->logo;
57+
}
58+
4859
public function getName(): string
4960
{
5061
return $this->name;

src/Repositories/UrlRepository.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ public function __construct(PDO $connection)
1414
$this->connection = $connection;
1515
}
1616

17-
public function create(string $urlName, string $createdAt): int
17+
public function create(string $logo, string $urlName, string $createdAt): int
1818
{
19-
$sql = "INSERT INTO urls (name, created_at) VALUES (:name, :created_at)";
19+
$sql = "INSERT INTO urls (logo, name, created_at) VALUES (:logo, :name, :created_at)";
2020

2121
$stmt = $this->connection->prepare($sql);
22+
$stmt->bindParam(':logo', $logo);
2223
$stmt->bindParam(':name', $urlName);
2324
$stmt->bindParam(':created_at', $createdAt);
2425
$stmt->execute();
@@ -40,6 +41,7 @@ public function getById(int $id): ?Url
4041

4142
$url = new Url($result['name']);
4243
$url->setId($result['id']);
44+
$url->setLogo($result['logo']);
4345
$url->setCreatedAt($result['created_at']);
4446

4547
return $url;
@@ -59,14 +61,15 @@ public function getByName(string $name): ?Url
5961

6062
$url = new Url($result['name']);
6163
$url->setId($result['id']);
64+
$url->setLogo($result['logo']);
6265
$url->setCreatedAt($result['created_at']);
6366

6467
return $url;
6568
}
6669

6770
public function getAll(): array
6871
{
69-
$sql = "SELECT id, name, created_at FROM urls ORDER BY id ASC";
72+
$sql = "SELECT id, logo, name, created_at FROM urls ORDER BY id ASC";
7073
return $this->connection->query($sql)->fetchAll();
7174
}
7275
}

src/Templates/urls/index.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<thead>
99
<tr>
1010
<th>ID</th>
11+
<th>Логотип</th>
1112
<th>Имя</th>
1213
<th>Последняя проверка</th>
1314
<th>Код ответа</th>
@@ -17,6 +18,7 @@
1718
{% for url in urls %}
1819
<tr>
1920
<td>{{ url.id }}</td>
21+
<td><img src="{{ url.logo }}" alt="Logo"></td>
2022
<td><a href="/urls/{{ url.id }}">{{ url.name }}</a></td>
2123
<td>{{ url.lastCheck }}</td>
2224
<td>{{ url.lastStatusCode }}</td>

src/Templates/urls/show.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{% endfor %}
1010
{% endfor %}
1111
<div class="container-lg mt-3">
12-
<h1>Сайт: {{ url.name }}</h1>
12+
<h1><img src="{{ url.logo }}" alt="Logo"> Сайт: {{ url.name }}</h1>
1313
<div class="table-responsive">
1414
<table class="table table-bordered table-hover text-nowrap" data-test="url">
1515
<tbody>

0 commit comments

Comments
 (0)