feat: add application container and event classes for user and torrent uploads

- Introduced a new Container class extending the Illuminate Container for application-specific functionality.
- Added TorrentUploaded and UserRegistered event classes to handle torrent uploads and user registrations, respectively.
- Implemented methods for retrieving service instances and checking service existence in the Container.
- Established a structured approach for event data encapsulation with readonly properties and getter methods.

This update enhances the application's architecture by integrating a custom container and event-driven design, improving maintainability and extensibility.
This commit is contained in:
Yury Pikhtarev 2025-06-23 01:13:46 +04:00
commit 58e02e0924
No known key found for this signature in database
5 changed files with 207 additions and 0 deletions

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Container;
use Illuminate\Container\Container as IlluminateContainer;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\CircularDependencyException;
/**
* Application Container
*
* Extends Illuminate Container with application-specific functionality
*/
class Container extends IlluminateContainer
{
/**
* Get a service from the container
*
* @throws BindingResolutionException When the service cannot be resolved
* @throws CircularDependencyException
*/
public function get(string $id): mixed
{
return $this->resolve($id);
}
/**
* Check if a service exists in the container
*/
public function has(string $id): bool
{
return $this->bound($id);
}
}

View file

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace App\Events;
use DateTimeInterface;
/**
* Event fired when a new torrent is uploaded
*/
readonly class TorrentUploaded
{
/**
* Create a new event instance
*/
public function __construct(
public int $torrentId,
public int $uploaderId,
public string $torrentName,
public int $size,
public DateTimeInterface $uploadedAt
)
{
}
/**
* Get the torrent ID
*/
public function getTorrentId(): int
{
return $this->torrentId;
}
/**
* Get the uploader user ID
*/
public function getUploaderId(): int
{
return $this->uploaderId;
}
/**
* Get the torrent name
*/
public function getTorrentName(): string
{
return $this->torrentName;
}
/**
* Get the torrent size in bytes
*/
public function getSize(): int
{
return $this->size;
}
/**
* Get the upload timestamp
*/
public function getUploadedAt(): DateTimeInterface
{
return $this->uploadedAt;
}
}

View file

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace App\Events;
use DateTimeInterface;
/**
* Event fired when a new user is registered
*/
readonly class UserRegistered
{
/**
* Create a new event instance
*/
public function __construct(
public int $userId,
public string $username,
public string $email,
public DateTimeInterface $registeredAt
)
{
}
/**
* Get the user ID
*/
public function getUserId(): int
{
return $this->userId;
}
/**
* Get the username
*/
public function getUsername(): string
{
return $this->username;
}
/**
* Get the email
*/
public function getEmail(): string
{
return $this->email;
}
/**
* Get the registration timestamp
*/
public function getRegisteredAt(): DateTimeInterface
{
return $this->registeredAt;
}
}

View file

View file

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Exceptions;
use Throwable;
/**
* Application Exception Handler
*
* Handles all uncaught exceptions in the application
*/
class Handler
{
/**
* Handle an uncaught exception
*/
public function handle(Throwable $exception): void
{
// Log the exception
error_log($exception->getMessage() . ' in ' . $exception->getFile() . ':' . $exception->getLine());
// You can add custom handling here
}
/**
* Render an exception for HTTP response
*/
public function render(Throwable $exception): string
{
if (php_sapi_name() === 'cli') {
return $exception->getMessage() . "\n";
}
// Return JSON for API requests or HTML for web
$isJson = isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json');
if ($isJson) {
return json_encode([
'error' => $exception->getMessage(),
'code' => $exception->getCode()
]);
}
return '<h1>Error</h1><p>' . htmlspecialchars($exception->getMessage()) . '</p>';
}
}