A PHP-based REST API that provides Twitch user account analytics, focusing on account age calculation based on display names. The project follows Clean Architecture principles and modern PHP practices.
- PHP 8.2+
- Laravel Framework 10.x
- Composer for dependency management
- PHPUnit for testing
- PHP_CodeSniffer for code style enforcement
src/
├── Api/
│ └── Controllers/
│ └── UserController.php
├── Application/
│ └── Services/
│ └── UserAccountService.php
├── Domain/
│ ├── Managers/
│ │ └── UserManager.php
│ ├── Models/
│ │ └── User.php
│ └── Interfaces/
│ └── UserRepositoryInterface.php
├── Infrastructure/
│ ├── Repositories/
│ │ └── MockUserRepository.php
│ └── Data/
│ └── mock-users.json
└── Tests/
├── Unit/
└── Integration/
File: Infrastructure/Data/mock-users.json
[
{
"id": "12345",
"login": "ninja",
"display_name": "Ninja",
"type": "",
"broadcaster_type": "partner",
"description": "Professional Gamer and Streamer",
"profile_image_url": "https://example.com/ninja.jpg",
"offline_image_url": "https://example.com/ninja-offline.jpg",
"view_count": 500000,
"created_at": "2011-11-20T00:00:00Z"
}
]Retrieves information about a Twitch user's account age based on their display name.
Endpoint: GET /api/users/age
Parameters:
name(required): The name of the Twitch user
Example Request:
curl -X GET "http://localhost:8000/api/users/age?name=Ninja"Success Response (200 OK):
{
"name": "Ninja",
"days_since_creation": 4482,
"created_at": "2011-11-20T00:00:00Z"
}Error Responses:
-
400 Bad Request
{ "error": "INVALID_REQUEST", "message": "Name parameter is required", "status": 400 } -
404 Not Found
{ "error": "USER_NOT_FOUND", "message": "No user found with given name: {name}", "status": 404 } -
500 Internal Server Error
{ "error": "INTERNAL_ERROR", "message": "An unexpected error occurred", "status": 500 }
- Handle HTTP requests and responses
- Validate input parameters
- Route to appropriate service
- Format API responses
- Handle error responses
- Orchestrate business logic
- Calculate account age
- Validate business rules
- Transform data between layers
- Define User entity and its properties
- Implement domain logic related to user data
- Implement core business logic
- Handle user data retrieval
- Calculate days since account creation
- Define contracts for data access
- Ensure loose coupling between layers
- Implement data access logic
- Handle mock data retrieval
- Implement caching if needed
- Client sends GET request with name
- Controller validates input
- Service calls manager to process request
- Manager uses repository to fetch user data
- Calculate days since account creation
- Return formatted response
- All errors must be caught and transformed into appropriate API responses
- Use custom exception classes for different error types
- Log all errors for debugging
- Return consistent error response format
- Name must not be empty
- Name must be a string
- Name maximum length: 25 characters
- Name minimum length: 1 character
- Unit tests for all business logic
- Integration tests for API endpoints
- Mock repository tests
- Error handling tests
- PSR-12 coding standard
- PHPDoc blocks for all public methods
- Type hints for all method parameters and return types
- Immutable objects where possible
- Dependency injection
- SOLID principles adherence
- Clone repository
- Install dependencies:
composer install - Copy
.env.exampleto.env - Generate app key:
php artisan key:generate - Create mock data file in
Infrastructure/Data - Run tests:
php artisan test - Start development server:
php artisan serve
- Input validation and sanitization
- Rate limiting on API endpoints
- Error messages should not expose internal details
- CORS configuration for API access
- Request validation middleware