# pierreminiggio/url-shortener architecture

> Architecture diagram of the GitHub repository pierreminiggio/url-shortener, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pierreminiggio/url-shortener
- Repository: https://github.com/pierreminiggio/url-shortener

## Overview

##### Project Type and Purpose
- The project is a full-stack web application (server-side only) primarily focused on URL redirection/shortening functionality.
- Its purpose is to receive incoming URL requests, perform lookups against stored mappings in a SQL database, and then redirect users accordingly.

##### Analyzing the File Structure
- Top-level Files/Directories:
⁃ .env.example.json, .htaccess, README.md, composer.json/lock: Configuration files and project metadata.
⁃ public/index.php: Acts as the entry point for HTTP requests, handling incoming requests and delegating to the application.
⁃ src: Contains the core PHP source code structured into various sub-directories.
- Key Directories and Their Roles:
⁃ src/App.php: Likely the central application class that bootstraps the system.
⁃ src/Connection: Contains DatabaseConnectionFactory.php, which is responsible for establishing connections to the SQL database.
⁃ src/Controller: Contains RedirectionController.php that manages the core logic for URL redirections.
⁃ src/Entity: Contains Redirection.php, representing the data model for redirection entries.
⁃ src/Repository: Contains repository classes like ManagedLinksRepository.php and RedirectionRepository.php which abstract database operations.
⁃ src/Template: Contains ErrorTemplate.php and RedirectionTemplate.php, used for rendering responses (error pages, redirection pages).

a. Main Components:
- Client (Web Browser): Initiates requests to the application.
- Front Controller (public/index.php): Entry point that receives HTTP requests.
- Application Core (src/App.php): Manages routing, settings, and overall application flow.
- Controller (src/Controller/RedirectionController.php): Processes requests related to redirection.
- Database Connection Factory (src/Connection/DatabaseConnectionFactory.php): Creates and manages database connections.
- Repository Layer (src/Repository/RedirectionRepository.php and ManagedLinksRepository.php): Interacts with the database, performing CRUD operations.
- Entity (src/Entity/Redirection.php): Models the redirection data.
- Templating Layer (src/Template/RedirectionTemplate.php and ErrorTemplate.php): Renders output pages.
- SQL Database: Stores redirection data as per the provided SQL table definition.

b. Relationships and Interactions:
- An HTTP request from a client hits public/index.php.
- The index.php bootstraps the application (via src/App.php) which then routes the request to the appropriate controller (RedirectionController).
- RedirectionController uses the Repository layer (RedirectionRepository) to fetch the relevant redirection data from the database.
- The Repository uses the DatabaseConnectionFactory to establish a connection to the SQL database.
- Once data is retrieved, the Controller selects the correct Template (RedirectionTemplate for success, ErrorTemplate in case of issues) to generate the output.
- The generated response is then returned to the client.

c. Architectural Patterns and Design Principles:
- Separation of Concerns: Clear divisions between controllers, models/entities, repositories (data-access), and presentation (templates).
- MVC-like structure: Although not strictly an MVC framework, the usage of Controllers, Entities, and Templates mirrors the pattern’s separation of responsibilities.
- Dependency Injection/Factory Pattern: The DatabaseConnectionFactory encapsulates connection logic, promoting modularity.

d. Relevant Technologies:
- PHP 8 as the programming language.
- SQL database engine (as referenced in the README; likely MySQL using the MyISAM engine).
- Composer for dependency management.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Client (Web Browser)"]:::client
    B["public/index.php"]:::web
    C["src/App.php"]:::app
    D["src/Controller/RedirectionController.php"]:::controller
    E["src/Connection/DatabaseConnectionFactory.php"]:::connection
    F["src/Repository/RedirectionRepository.php"]:::repository
    G["src/Repository/ManagedLinksRepository.php"]:::repository
    H["src/Entity/Redirection.php"]:::repository
    I["src/Template/RedirectionTemplate.php"]:::template
    J["src/Template/ErrorTemplate.php"]:::template
    K["SQL Database"]:::database

    %% Data flow from Client to Web Entry Point
    A -->|"request"| B
    %% Bootstrap and application initialization
    B -->|"bootstraps"| C
    C -->|"routes"| D
    
    %% Controller interactions with repositories and database
    D -->|"fetches_data"| F
    D -->|"fetches_data"| G
    F -->|"uses"| E
    G -->|"uses"| E
    E -->|"connects_to"| K
    F -->|"maps_to"| H

    %% Controller selects templates for response rendering
    D -->|"renders_success"| I
    D -->|"renders_error"| J
    I -->|"response"| A
    J -->|"response"| A

    %% Click Events
    click B "https://github.com/pierreminiggio/url-shortener/blob/main/public/index.php"
    click C "https://github.com/pierreminiggio/url-shortener/blob/main/src/App.php"
    click D "https://github.com/pierreminiggio/url-shortener/blob/main/src/Controller/RedirectionController.php"
    click E "https://github.com/pierreminiggio/url-shortener/blob/main/src/Connection/DatabaseConnectionFactory.php"
    click F "https://github.com/pierreminiggio/url-shortener/blob/main/src/Repository/RedirectionRepository.php"
    click G "https://github.com/pierreminiggio/url-shortener/blob/main/src/Repository/ManagedLinksRepository.php"
    click H "https://github.com/pierreminiggio/url-shortener/blob/main/src/Entity/Redirection.php"
    click I "https://github.com/pierreminiggio/url-shortener/blob/main/src/Template/RedirectionTemplate.php"
    click J "https://github.com/pierreminiggio/url-shortener/blob/main/src/Template/ErrorTemplate.php"

    %% Styles
    classDef client fill:#fdd,stroke:#333,stroke-width:2px;
    classDef web fill:#bbf,stroke:#333,stroke-width:2px;
    classDef app fill:#ccf,stroke:#333,stroke-width:2px;
    classDef controller fill:#cfc,stroke:#333,stroke-width:2px;
    classDef connection fill:#9cf,stroke:#333,stroke-width:2px;
    classDef repository fill:#fcf,stroke:#333,stroke-width:2px;
    classDef template fill:#ffc,stroke:#333,stroke-width:2px;
    classDef database fill:#f99,stroke:#333,stroke-width:2px;
```
