URL Shortener 1.0
A Laravel-based URL shortener application
Chargement...
Recherche...
Aucune correspondance
ShortUrlFactory.php
Aller à la documentation de ce fichier.
1<?php
2
4
5use Illuminate\Database\Eloquent\Factories\Factory;
8use Illuminate\Support\Str;
9
15class ShortUrlFactory extends Factory
16{
22 protected $model = ShortUrl::class;
23
29 public function definition(): array
30 {
31 return [
32 'user_id' => User::factory(),
33 'code' => $this->generateAlphanumericCode(),
34 'original_url' => $this->faker->url(),
35 'clicks' => $this->faker->numberBetween(0, 1000),
36 'last_used_at' => $this->faker->dateTimeBetween('-6 months', 'now'),
37 ];
38 }
39
43 private function generateAlphanumericCode(): string
44 {
45 $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
46 $charactersLength = strlen($characters);
47 $randomCode = '';
48
49 for ($i = 0; $i < 6; $i++) {
50 $randomCode .= $characters[random_int(0, $charactersLength - 1)];
51 }
52
53 return $randomCode;
54 }
55
59 public function neverUsed(): static
60 {
61 return $this->state(fn (array $attributes) => [
62 'clicks' => 0,
63 'last_used_at' => null,
64 ]);
65 }
66
70 public function recentlyUsed(): static
71 {
72 return $this->state(fn (array $attributes) => [
73 'clicks' => $this->faker->numberBetween(50, 500),
74 'last_used_at' => $this->faker->dateTimeBetween('-7 days', 'now'),
75 ]);
76 }
77
81 public function oldUsage(): static
82 {
83 return $this->state(fn (array $attributes) => [
84 'clicks' => $this->faker->numberBetween(100, 1000),
85 'last_used_at' => $this->faker->dateTimeBetween('-6 months', '-3 months'),
86 ]);
87 }
88
92 public function highTraffic(): static
93 {
94 return $this->state(fn (array $attributes) => [
95 'clicks' => $this->faker->numberBetween(500, 2000),
96 'last_used_at' => $this->faker->dateTimeBetween('-1 month', 'now'),
97 ]);
98 }
99}
Factory for generating ShortUrl instances.
recentlyUsed()
Create a ShortUrl used recently.
neverUsed()
Create a ShortUrl that has never been used.
definition()
Define the model's default state.
generateAlphanumericCode()
Generate a 6-character alphanumeric code.
oldUsage()
Create a ShortUrl used long ago.
highTraffic()
Create a ShortUrl with high traffic.