Skip to content

Commit 26440f1

Browse files
committed
Added twitch link to profile
1 parent 907f58c commit 26440f1

13 files changed

Lines changed: 129 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SpeedPuzzling\Web\Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
final class Version20260303212100 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return '';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql('ALTER TABLE player ADD twitch VARCHAR(255) DEFAULT NULL');
20+
}
21+
22+
public function down(Schema $schema): void
23+
{
24+
$this->addSql('ALTER TABLE player DROP twitch');
25+
}
26+
}

src/Entity/Player.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class Player
5656
#[Column(nullable: true)]
5757
public null|string $instagram = null;
5858

59+
#[Immutable(Immutable::PRIVATE_WRITE_SCOPE)]
60+
#[Column(nullable: true)]
61+
public null|string $twitch = null;
62+
5963
#[Immutable(Immutable::PRIVATE_WRITE_SCOPE)]
6064
#[Column(type: Types::TEXT, nullable: true)]
6165
public null|string $bio = null;
@@ -172,6 +176,7 @@ public function changeProfile(
172176
null|string $bio,
173177
null|string $facebook,
174178
null|string $instagram,
179+
null|string $twitch,
175180
): void {
176181
$this->name = $name;
177182
$this->email = $email;
@@ -181,6 +186,7 @@ public function changeProfile(
181186
$this->bio = $bio;
182187
$this->facebook = $facebook;
183188
$this->instagram = $instagram;
189+
$this->twitch = $twitch;
184190
}
185191

186192
/**

src/FormData/EditProfileFormData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class EditProfileFormData
1717
public null|string $bio = null;
1818
public null|string $facebook = null;
1919
public null|string $instagram = null;
20+
public null|string $twitch = null;
2021

2122
public static function fromPlayerProfile(PlayerProfile $playerProfile): self
2223
{
@@ -28,6 +29,7 @@ public static function fromPlayerProfile(PlayerProfile $playerProfile): self
2829
$data->bio = $playerProfile->bio;
2930
$data->facebook = $playerProfile->facebook;
3031
$data->instagram = $playerProfile->instagram;
32+
$data->twitch = $playerProfile->twitch;
3133

3234
return $data;
3335
}

src/FormType/EditProfileFormType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
9696
'required' => false,
9797
]);
9898

99+
$builder->add('twitch', TextType::class, [
100+
'label' => 'Twitch',
101+
'required' => false,
102+
]);
103+
99104
$builder->add('bio', TextareaType::class, [
100105
'label' => 'forms.about_me',
101106
'required' => false,

src/Message/EditProfile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(
1919
public null|string $bio,
2020
public null|string $facebook,
2121
public null|string $instagram,
22+
public null|string $twitch,
2223
) {
2324
}
2425

@@ -34,6 +35,7 @@ public static function fromFormData(string $playerId, EditProfileFormData $formD
3435
bio: $formData->bio,
3536
facebook: $formData->facebook,
3637
instagram: $formData->instagram,
38+
twitch: $formData->twitch,
3739
);
3840
}
3941
}

src/MessageHandler/EditProfileHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function __invoke(EditProfile $message): void
5656
bio: $message->bio,
5757
facebook: $message->facebook,
5858
instagram: $message->instagram,
59+
twitch: $message->twitch,
5960
);
6061
}
6162
}

src/Query/GetPlayerProfile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function byId(string $playerId): PlayerProfile
4444
bio,
4545
facebook,
4646
instagram,
47+
twitch,
4748
stripe_customer_id,
4849
modal_displayed,
4950
locale,
@@ -102,6 +103,7 @@ public function byUserId(string $userId): PlayerProfile
102103
bio,
103104
facebook,
104105
instagram,
106+
twitch,
105107
stripe_customer_id,
106108
modal_displayed,
107109
locale,

src/Results/PlayerProfile.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* bio: null|string,
2727
* facebook: null|string,
2828
* instagram: null|string,
29+
* twitch: null|string,
2930
* stripe_customer_id: null|string,
3031
* modal_displayed: bool,
3132
* locale: null|string,
@@ -62,6 +63,7 @@ public function __construct(
6263
public null|string $bio,
6364
public null|string $facebook,
6465
public null|string $instagram,
66+
public null|string $twitch,
6567
public bool $modalDisplayed,
6668
public null|string $stripeCustomerId,
6769
public null|string $locale,
@@ -142,6 +144,7 @@ public static function fromDatabaseRow(array $row, DateTimeImmutable $now): self
142144
bio: $row['bio'],
143145
facebook: $row['facebook'],
144146
instagram: $row['instagram'],
147+
twitch: $row['twitch'],
145148
modalDisplayed: $row['modal_displayed'],
146149
stripeCustomerId: $row['stripe_customer_id'],
147150
locale: $row['locale'],
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SpeedPuzzling\Web\Services;
6+
7+
use Nette\Utils\Strings;
8+
use SpeedPuzzling\Web\Value\Link;
9+
10+
readonly final class GenerateTwitchLink
11+
{
12+
public function fromUserInput(string $input): Link
13+
{
14+
$input = trim($input, " \n\r\t\v\0");
15+
16+
if (str_starts_with($input, 'http')) {
17+
$link = $input;
18+
} else {
19+
$link = 'https://www.twitch.tv/' . $input;
20+
}
21+
22+
$text = str_replace(['https://www.twitch.tv/', 'https://twitch.tv/'], '', $link);
23+
if (str_contains($text, '?')) {
24+
$text = Strings::before($text, '?') ?? '';
25+
}
26+
$text = trim($text, '/');
27+
28+
return new Link($link, $text);
29+
}
30+
}

src/Twig/LinksTwigExtension.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use SpeedPuzzling\Web\Services\GenerateFacebookLink;
88
use SpeedPuzzling\Web\Services\GenerateInstagramLink;
9+
use SpeedPuzzling\Web\Services\GenerateTwitchLink;
910
use Twig\Extension\AbstractExtension;
1011
use Twig\Markup;
1112
use Twig\TwigFilter;
@@ -15,6 +16,7 @@ final class LinksTwigExtension extends AbstractExtension
1516
public function __construct(
1617
readonly private GenerateInstagramLink $generateInstagramLink,
1718
readonly private GenerateFacebookLink $generateFacebookLink,
19+
readonly private GenerateTwitchLink $generateTwitchLink,
1820
) {
1921
}
2022

@@ -26,6 +28,7 @@ public function getFilters(): array
2628
return [
2729
new TwigFilter('instagram', [$this, 'generateInstagramLink']),
2830
new TwigFilter('facebook', [$this, 'generateFacebookLink']),
31+
new TwigFilter('twitch', [$this, 'generateTwitchLink']),
2932
];
3033
}
3134

@@ -46,4 +49,11 @@ public function generateFacebookLink(string $input): Markup|string
4649

4750
return new Markup("<a target='_blank' href='{$link->link}'>{$link->text}</a>", 'UTF-8');
4851
}
52+
53+
public function generateTwitchLink(string $input): Markup
54+
{
55+
$link = $this->generateTwitchLink->fromUserInput($input);
56+
57+
return new Markup("<a target='_blank' href='{$link->link}'>{$link->text}</a>", 'UTF-8');
58+
}
4959
}

0 commit comments

Comments
 (0)