Skip to content

Commit cfcf532

Browse files
authored
Merge pull request #516 from HiEventsDev/develop
2 parents b517ad2 + 7020a64 commit cfcf532

File tree

26 files changed

+565
-256
lines changed

26 files changed

+565
-256
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace HiEvents\DomainObjects\Enums;
4+
5+
enum ImageTypes
6+
{
7+
use BaseEnum;
8+
9+
case GENERIC;
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace HiEvents\Http\Actions\Images;
4+
5+
use HiEvents\Http\Actions\BaseAction;
6+
use HiEvents\Http\Request\Image\CreateImageRequest;
7+
use HiEvents\Resources\Image\ImageResource;
8+
use HiEvents\Services\Application\Handlers\Images\CreateImageHandler;
9+
use HiEvents\Services\Application\Handlers\Images\DTO\CreateImageDTO;
10+
use HiEvents\Services\Infrastructure\Image\Exception\CouldNotUploadImageException;
11+
use Illuminate\Http\JsonResponse;
12+
13+
class CreateImageAction extends BaseAction
14+
{
15+
public function __construct(
16+
public readonly CreateImageHandler $createImageHandler,
17+
)
18+
{
19+
}
20+
21+
/**
22+
* @throws CouldNotUploadImageException
23+
*/
24+
public function __invoke(CreateImageRequest $request): JsonResponse
25+
{
26+
$image = $this->createImageHandler->handle(new CreateImageDTO(
27+
userId: $this->getAuthenticatedUser()->getId(),
28+
image: $request->file('image'),
29+
));
30+
31+
return $this->resourceResponse(ImageResource::class, $image);
32+
}
33+
}

backend/app/Http/Request/Event/CreateEventImageRequest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace HiEvents\Http\Request\Event;
44

55
use HiEvents\DomainObjects\Enums\EventImageType;
6+
use HiEvents\Validators\Rules\RulesHelper;
67
use Illuminate\Foundation\Http\FormRequest;
78
use Illuminate\Validation\Rule;
89

@@ -11,14 +12,15 @@ class CreateEventImageRequest extends FormRequest
1112
public function rules(): array
1213
{
1314
return [
14-
'image' => [
15-
'required',
16-
'image',
17-
'max:8192', //8mb
18-
'dimensions:min_width=600,min_height=50,max_width=4000,max_height=4000',
19-
'mimes:jpeg,png,jpg,webp',
20-
],
15+
'image' => RulesHelper::IMAGE_RULES,
2116
'type' => Rule::in(EventImageType::valuesArray()),
2217
];
2318
}
19+
20+
public function messages(): array
21+
{
22+
return [
23+
'image.dimensions' => __('The image must be at least 600 pixels wide and 50 pixels tall, and no more than 4000 pixels wide and 4000 pixels tall.'),
24+
];
25+
}
2426
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace HiEvents\Http\Request\Image;
4+
5+
use HiEvents\Validators\Rules\RulesHelper;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
8+
class CreateImageRequest extends FormRequest
9+
{
10+
public function rules(): array
11+
{
12+
return [
13+
'image' => RulesHelper::IMAGE_RULES,
14+
];
15+
}
16+
17+
public function messages(): array
18+
{
19+
return [
20+
'image.dimensions' => __('The image must be at least 600 pixels wide and 50 pixels tall, and no more than 4000 pixels wide and 4000 pixels tall.'),
21+
];
22+
}
23+
}

backend/app/Services/Application/Handlers/Event/CreateEventImageHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function handle(CreateEventImageDTO $imageData): ImageDomainObject
2323
return $this->createEventImageService->createImage(
2424
eventId: $imageData->event_id,
2525
image: $imageData->image,
26-
type: $imageData->type,
26+
imageType: $imageData->imageType,
2727
);
2828
}
2929
}

backend/app/Services/Application/Handlers/Event/DTO/CreateEventImageDTO.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CreateEventImageDTO extends BaseDTO
1111
public function __construct(
1212
public readonly int $event_id,
1313
public readonly UploadedFile $image,
14-
public readonly EventImageType $type,
14+
public readonly EventImageType $imageType,
1515
)
1616
{
1717
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace HiEvents\Services\Application\Handlers\Images;
4+
5+
use HiEvents\DomainObjects\Enums\ImageTypes;
6+
use HiEvents\DomainObjects\ImageDomainObject;
7+
use HiEvents\DomainObjects\UserDomainObject;
8+
use HiEvents\Services\Application\Handlers\Images\DTO\CreateImageDTO;
9+
use HiEvents\Services\Domain\Image\ImageUploadService;
10+
use HiEvents\Services\Infrastructure\Image\Exception\CouldNotUploadImageException;
11+
12+
class CreateImageHandler
13+
{
14+
public function __construct(
15+
private readonly ImageUploadService $imageUploadService,
16+
)
17+
{
18+
}
19+
20+
/**
21+
* @throws CouldNotUploadImageException
22+
*/
23+
public function handle(CreateImageDTO $imageData): ImageDomainObject
24+
{
25+
// For generic images, we associate them with the user
26+
return $this->imageUploadService->upload(
27+
image: $imageData->image,
28+
entityId: $imageData->userId,
29+
entityType: UserDomainObject::class,
30+
imageType: ImageTypes::GENERIC->name,
31+
);
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace HiEvents\Services\Application\Handlers\Images\DTO;
4+
5+
use Illuminate\Http\UploadedFile;
6+
7+
class CreateImageDTO
8+
{
9+
public function __construct(
10+
public readonly int $userId,
11+
public readonly UploadedFile $image,
12+
)
13+
{
14+
}
15+
}

backend/app/Services/Application/Handlers/Organizer/CreateOrganizerHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private function createOrganizer(CreateOrganizerDTO $organizerData): OrganizerDo
4949
image: $organizerData->logo,
5050
entityId: $organizer->getId(),
5151
entityType: OrganizerDomainObject::class,
52-
imageType: OrganizerImageType::LOGO->name,
52+
imageType: OrganizerImageType::LOGO,
5353
);
5454
}
5555

backend/app/Services/Domain/Event/CreateEventImageService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public function __construct(
2727
public function createImage(
2828
int $eventId,
2929
UploadedFile $image,
30-
EventImageType $type,
30+
EventImageType $imageType,
3131
): ImageDomainObject
3232
{
33-
return $this->databaseManager->transaction(function () use ($image, $eventId, $type) {
34-
if ($type === EventImageType::EVENT_COVER) {
33+
return $this->databaseManager->transaction(function () use ($entityType, $image, $eventId, $imageType) {
34+
if ($imageType === EventImageType::EVENT_COVER) {
3535
$this->imageRepository->deleteWhere([
3636
'entity_id' => $eventId,
3737
'entity_type' => EventDomainObject::class,
@@ -43,7 +43,7 @@ public function createImage(
4343
image: $image,
4444
entityId: $eventId,
4545
entityType: EventDomainObject::class,
46-
imageType: $type->name,
46+
imageType: $imageType->name,
4747
);
4848
});
4949
}

backend/app/Services/Domain/Image/ImageUploadService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
use HiEvents\Services\Infrastructure\Image\ImageStorageService;
99
use Illuminate\Http\UploadedFile;
1010

11-
readonly class ImageUploadService
11+
class ImageUploadService
1212
{
1313
public function __construct(
14-
private ImageStorageService $imageStorageService,
15-
private ImageRepositoryInterface $imageRepository
14+
private readonly ImageStorageService $imageStorageService,
15+
private readonly ImageRepositoryInterface $imageRepository
1616
)
1717
{
1818
}

backend/app/Services/Infrastructure/Image/ImageStorageService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use Illuminate\Support\Str;
1111
use Psr\Log\LoggerInterface;
1212

13-
readonly class ImageStorageService
13+
class ImageStorageService
1414
{
1515
public function __construct(
16-
private FilesystemManager $filesystemManager,
17-
private Repository $config,
18-
private LoggerInterface $logger,
16+
private readonly FilesystemManager $filesystemManager,
17+
private readonly Repository $config,
18+
private readonly LoggerInterface $logger,
1919
)
2020
{
2121
}

backend/app/Validators/Rules/RulesHelper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ class RulesHelper
1717
public const REQUIRED_EMAIL = ['email' , 'required', 'max:100'];
1818

1919
public const OPTIONAL_TEXT_MEDIUM_LENGTH = ['string', 'max:2000', 'nullable'];
20+
21+
public const IMAGE_RULES = [
22+
'required',
23+
'image',
24+
'max:8192', //8mb
25+
'dimensions:min_width=600,min_height=50,max_width=4000,max_height=4000',
26+
'mimes:jpeg,png,jpg,webp',
27+
];
2028
}

backend/routes/api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use HiEvents\Http\Actions\EventSettings\EditEventSettingsAction;
5454
use HiEvents\Http\Actions\EventSettings\GetEventSettingsAction;
5555
use HiEvents\Http\Actions\EventSettings\PartialEditEventSettingsAction;
56+
use HiEvents\Http\Actions\Images\CreateImageAction;
5657
use HiEvents\Http\Actions\Messages\GetMessagesAction;
5758
use HiEvents\Http\Actions\Messages\SendMessageAction;
5859
use HiEvents\Http\Actions\Orders\CancelOrderAction;
@@ -297,6 +298,9 @@ function (Router $router): void {
297298

298299
// Reports
299300
$router->get('/events/{event_id}/reports/{report_type}', GetReportAction::class);
301+
302+
// Images
303+
$router->post('/images', CreateImageAction::class);
300304
}
301305
);
302306

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Tests\Unit\Services\Application\Handlers\Images;
4+
5+
use HiEvents\DomainObjects\Enums\ImageTypes;
6+
use HiEvents\DomainObjects\ImageDomainObject;
7+
use HiEvents\DomainObjects\UserDomainObject;
8+
use HiEvents\Services\Application\Handlers\Images\CreateImageHandler;
9+
use HiEvents\Services\Application\Handlers\Images\DTO\CreateImageDTO;
10+
use HiEvents\Services\Domain\Image\ImageUploadService;
11+
use Illuminate\Http\UploadedFile;
12+
use Mockery as m;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class CreateImageHandlerTest extends TestCase
16+
{
17+
private ImageUploadService $imageUploadService;
18+
private CreateImageHandler $handler;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->imageUploadService = m::mock(ImageUploadService::class);
25+
26+
$this->handler = new CreateImageHandler(
27+
$this->imageUploadService
28+
);
29+
}
30+
31+
public function testHandleSuccessfullyCreatesImage(): void
32+
{
33+
$uploadedFile = m::mock(UploadedFile::class);
34+
$imageDomainObject = m::mock(ImageDomainObject::class);
35+
36+
$dto = new CreateImageDTO(
37+
userId: 42,
38+
image: $uploadedFile
39+
);
40+
41+
$this->imageUploadService
42+
->shouldReceive('upload')
43+
->once()
44+
->withArgs([
45+
$uploadedFile,
46+
42,
47+
UserDomainObject::class,
48+
ImageTypes::GENERIC->name,
49+
])
50+
->andReturn($imageDomainObject);
51+
52+
$result = $this->handler->handle($dto);
53+
54+
$this->assertSame($imageDomainObject, $result);
55+
}
56+
}

0 commit comments

Comments
 (0)