# akinojoey/socialnetworkingservice architecture

> Architecture diagram of the GitHub repository akinojoey/socialnetworkingservice, 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/akinojoey/socialnetworkingservice
- Repository: https://github.com/akinojoey/socialnetworkingservice

## Overview

##### Main Components
- Frontend Client:
- Represented by static assets in the “public” directory (bundled JS files like login.bundle.js, main.bundle.js, etc.) and assets in “src/assets.”
- Responsive design powered by Tailwind, Flowbite, and daisyUI.
- Backend Web Server:
- The main entry point (public/index.php) routes requests via the “src/routing” module.
- Within “src,” you have Models (business logic), Views (presentation layer in “src/views”), and Helpers/Middleware for authentication, session handling, and security.
- Controllers are likely part of the routing and middleware files.
- Database:
- MySQL is used, with migrations (in “src/database/migrations”), seeders, and the DAO pattern found in “src/database/data_access” for structured database operations.
- Real-Time Communication Server:
- A dedicated chat server implemented in “bin/chat_server.php” using Ratchet to support private WebSocket-based messaging.
- Cron Jobs and Scheduled Tasks:
- Several cron scripts (in “cron”) handle scheduled posts, deletion of expired tokens, and seeding. Also, there is a command-based CLI architecture in “src/commands” for tasks like migrations, deploying, and seeding.
- External Dependencies & Services:
- PHPMailer for email, ImageMagick and ffmpeg for media processing, Carbon for date handling, along with third-party APIs (e.g., Pixabay for dummy images).
- NGINX acting as the web server and reverse proxy (especially for WSS).
- Amazon EC2 for hosting and Certbot for SSL/TLS certificate management.

##### Relationships and Interactions
- User Interactions:
- End users interact with the application via a browser. Their requests hit the NGINX web server and are served the static frontend assets.
- Client-side JavaScript handles interactions (post submissions, login, chat) and makes API calls.
- Backend Request Flow:
- Incoming requests are processed by the router (in “src/routing/routes.php”) and pass through middleware (authentication, CSRF, email verification, etc.) before calling the appropriate business logic (models, commands).
- Responses are rendered through HTML/JSON renderers (in “src/response/render”).
- Database Interactions:
- The backend invokes DAO methods (interfaces and implementations found in “src/database/data_access”) to interact with the MySQL database, using migrations and seeders to manage the schema and test data.
- Scheduled & Asynchronous Tasks:
- Cron jobs periodically trigger commands (e.g., PostScheduled for moving scheduled posts to published, seed_prototype for generating dummy data).
- Real-Time Chat:
- The WebSocket server (chat_server.php) serves direct messaging, with communication facilitated between a connected client and backend services.
- Security & Validation:
- Middleware intercepts requests to verify sessions, validate CSRF tokens, process signed URLs (via SignatureValidationMiddleware), and ensure data integrity using Helpers like ValidationHelper and CipherHelper.

##### Architectural Patterns and Design Principles
- MVC Architecture:
- Clearly demarcate models, views, and routing logic (akin to controllers) – even if controllers are interwoven with the router.
- DAO Pattern:
- All database operations funnel through defined interfaces and their implementations.
- Middleware Chain:
- A layered approach to security and request validation.
- Microservices-Like Separation:
- Though not a microservices application per se, there is separation between web requests (HTTP), background jobs (cron/commands), and real-time socket communications.

##### Relevant Technologies and Frameworks
- Frontend:
- HTML, CSS (Tailwind), JavaScript (bundled via webpack)
- Third-party libraries: flowbite, daisyUI, Flatpickr for date selection.
- Backend:
- PHP, Composer for dependency management, PHP’s built-in session handling, and PHPMailer.
- Ratchet library for WebSockets.
- Database:
- MySQL.
- Tools/Deployment:
- NGINX, Amazon EC2, cron jobs, Supervisor for process management, Certbot for TLS.

## Diagram (Mermaid)

```mermaid
flowchart TD
    U["User Browser"]:::user
    NG["NGINX Web Server"]:::infrastructure

    subgraph "Frontend Client"
        FS["Static Assets (public)"]:::frontend
        AS["Assets (src/assets)"]:::frontend
    end

    subgraph "Backend API Server"
        BE["Entry Point (public/index.php)"]:::backend
        RTG["Routing (src/routing)"]:::backend
        MDW["Middleware (src/middleware)"]:::backend
        MOD["Models (src/models)"]:::backend
        VWS["Views (src/views)"]:::backend
    end

    subgraph "Database Layer"
        DAO["DAO (src/database/data_access)"]:::database
        MIG["Migrations (src/database/migrations)"]:::database
        DB["MySQL Database"]:::database
    end

    subgraph "Real-Time Communication"
        CS["Chat Server (bin/chat_server.php)"]:::realtime
    end

    subgraph "Scheduled Tasks"
        CR["Cron Scripts (cron)"]:::cron
        CMD["Command Tasks (src/commands)"]:::cron
    end

    U -->|"HTTP_Request"| NG
    NG -->|"ServesStaticFiles"| FS
    NG -->|"API_Requests"| BE

    BE -->|"Routes_To"| RTG
    RTG -->|"Executes"| MDW
    MDW -->|"InvokesBusinessLogic"| MOD
    MOD -->|"RendersResponse"| VWS
    MOD -->|"DB_Operations"| DAO
    DAO -->|"PersistsData"| DB
    MIG -->|"ManagesSchema"| DB

    BE ---|"RealtimeComm"| CS

    BE -->|"SchedulesTask"| CR
    CR -->|"RunsCommands"| CMD

    classDef user fill:#FFE0B2,stroke:#E65100,stroke-width:2px;
    classDef infrastructure fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef frontend fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef backend fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef database fill:#FFCCBC,stroke:#BF360C,stroke-width:2px;
    classDef realtime fill:#D1C4E9,stroke:#673AB7,stroke-width:2px;
    classDef cron fill:#F8BBD0,stroke:#C2185B,stroke-width:2px;

    click FS "https://github.com/akinojoey/socialnetworkingservice/tree/main/public"
    click AS "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/assets"
    click BE "https://github.com/akinojoey/socialnetworkingservice/blob/main/public/index.php"
    click RTG "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/routing"
    click MDW "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/middleware"
    click MOD "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/models"
    click VWS "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/views"
    click DAO "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/database/data_access"
    click MIG "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/database/migrations"
    click CS "https://github.com/akinojoey/socialnetworkingservice/blob/main/bin/chat_server.php"
    click CR "https://github.com/akinojoey/socialnetworkingservice/tree/main/cron"
    click CMD "https://github.com/akinojoey/socialnetworkingservice/tree/main/src/commands"
```
