-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaFactory.php
More file actions
76 lines (65 loc) · 2.37 KB
/
Copy pathMediaFactory.php
File metadata and controls
76 lines (65 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Code16\OzuClient\Database\Factories;
use Code16\OzuClient\Eloquent\Media;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Storage;
class MediaFactory extends Factory
{
protected $model = Media::class;
public function definition()
{
return [
];
}
public function image(string $key): Factory
{
return $this
->state(function (array $attributes) use ($key) {
return [
'model_key' => $key,
'file_name' => sprintf('data/medias/%s.jpg', $this->faker->unique()->slug()),
'mime_type' => 'image/jpeg',
'disk' => 'local',
'size' => $this->faker->numberBetween(100, 100000),
];
});
}
public function file(string $key): Factory
{
return $this
->state(function (array $attributes) use ($key) {
return [
'model_key' => $key,
'file_name' => sprintf('data/files/%s.jpg', $this->faker->unique()->slug()),
'mime_type' => 'image/jpeg',
'disk' => 'local',
'size' => $this->faker->numberBetween(100, 100000),
];
});
}
public function withFile(?string $fileName = null, string $type = 'image')
{
return $this->state(function (array $attributes) use ($fileName, $type) {
$fileName = $fileName ?: fake()->slug().($type === 'image' ? '.jpg' : '.pdf');
$path = ($type === 'image' ? $this->getRandomFixtureImagePath() : $this->getRandomFixtureDocumentPath());
Storage::disk('local')
->put('/data/'.($type === 'image' ? 'medias' : 'files')."/$fileName", file_get_contents($path));
return [
'file_name' => 'data/'.($type === 'image' ? 'medias' : 'files')."/$fileName",
];
});
}
private function getRandomFixtureImagePath(): string
{
return base_path(
sprintf(
'vendor/code16/ozu-client/database/fixtures/images/%s.jpeg',
rand(1, 26)
)
);
}
private function getRandomFixtureDocumentPath(): string
{
return base_path('vendor/code16/ozu-client/database/fixtures/documents/doc.pdf');
}
}