# tustin/psn-php architecture

> Architecture diagram of the GitHub repository tustin/psn-php, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tustin/psn-php
- Repository: https://github.com/tustin/psn-php

## Overview

##### Project Type and Purpose
- This is an open-source PHP wrapper (“psn-php”) around the PlayStation Network API.
- It provides a client library that handles authentication (OAuth token), HTTP requests, response parsing, domain modeling, pagination (iterators), and error handling.
- Consumers install it via Composer and call high-level methods to fetch trophies, user data, messages, etc., from the PSN API.

##### Key Architectural Components
- External Service
– PlayStation Network API (REST/GraphQL endpoints)
- Library Core
– OAuthToken (manages access tokens, refreshes)
– Client & Api classes (constructs requests, sets headers, orchestrates calls)
– Http/ResponseParser (parses raw HTTP/JSON responses into PHP arrays)
- Domain Layer
– Model classes (User, Trophy, Message, Group, Media, etc.)
– Enums (type-safe constants for status, message types, consoles, languages, etc.)
- Factory Layer
– Factory classes map parsed data arrays into Model instances
- Iterator Layer
– AbstractApiIterator and concrete iterators (GameListIterator, TrophyIterator, MessagesIterator, etc.) to handle paginated responses and apply filters
- Exception Layer
– Custom exceptions for missing parameters, not-found, unmapped operations, no trophies, etc.
- Utility/Traits
– OperandParser trait for building filter expressions

##### Relationships and Data Flow
1. Consumer calls a high-level method on Api or Client (e.g., getUserTrophies()).
2. Client uses OAuthToken to ensure a valid access token.
3. Client sends HTTP request to PSN API (via cURL or Guzzle under the hood).
4. ResponseParser reads JSON, returns PHP array.
5. Corresponding Factory transforms array into Model instances.
6. If the endpoint is paginated, an Iterator yields each Model one by one, applying any filters.
7. Exceptions bubble up if required parameters are missing or API returns errors.

##### Architectural Patterns and Principles
- Layered architecture: separation between transport (HTTP), authentication, mapping (factories), domain models, and control (API).
- Factory pattern: centralizes object creation and mapping logic.
- Iterator pattern: abstracts pagination and lazy-loading of collections.
- Single Responsibility: each class has a clear, narrow focus (e.g., one model, one factory, one iterator).
- Interface-driven design: FactoryInterface and HTTP interfaces allow extensibility or swapping implementations.
- Immutable Enums: type-safe enumerations for cross-boundary constants.

##### Diagramming Guidance (Open-Source Tool Focus)
– Authentication      – HTTP Layer      – Response Parsing      – Factories      – Models & Enums      – Iterators      – Exceptions
– Consumer → Api/Client → HTTP Layer → PSN API
– PSN API → HTTP Layer → ResponseParser → Factory → Model/Iterator → Consumer
- Extensibility points: interfaces for Factory and HTTP so that advanced users can override behavior.
– Blue for external services (PSN API)
– Green for core transport/auth (Client, OAuthToken, HTTP)
– Yellow for domain artifacts (Models, Enums)
– Orange for mapping layers (Factories)
– Cyan for pagination logic (Iterators)
– Red for error paths (Exceptions)
– Rectangles for classes/layers

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Consumer and External
    Consumer["Consumer Application"]:::external
    PSNAPI["PlayStation Network API"]:::external

    subgraph "psn-php Library"
        direction TB

        subgraph "Authentication Layer"
            direction TB
            OAuthToken["OAuthToken"]:::core
        end

        subgraph "Core Transport & Orchestration"
            direction TB
            Api["Api (Facade)"]:::core
            Client["Client"]:::core
        end

        subgraph "HTTP & Parsing Layer"
            direction TB
            ResponseParser["ResponseParser"]:::core
        end

        subgraph "Factory Layer"
            direction TB
            FactoryInterface["FactoryInterface"]:::mapping
            ConcreteFactories["Concrete Factories"]:::mapping
        end

        subgraph "Domain Models & Enums"
            direction TB
            Models["Domain Models"]:::domain
            Enums["Enums"]:::domain
        end

        subgraph "Iterator Layer"
            direction TB
            Iterators["Iterators"]:::iterator
        end

        subgraph "Exception Layer"
            direction TB
            Exceptions["Exceptions"]:::exception
        end

        subgraph "Utility / Traits"
            direction TB
            OperandParser["OperandParser Trait"]:::utility
        end
    end

    %% Data Flow
    Consumer -->|"calls methods"| Api
    Api -->|"uses"| OAuthToken
    Api -->|"orchestrates HTTP"| Client
    Client -->|"HTTP/JSON"| PSNAPI
    PSNAPI -->|"JSON response"| Client
    Client -->|"raw response"| ResponseParser
    ResponseParser -->|"array data"| FactoryInterface
    FactoryInterface -->|"maps to"| Models
    ConcreteFactories --> Models
    Models -->|"yield via"| Iterators
    Iterators -->|"Model instances"| Consumer
    Client -->|"errors"| Exceptions
    ResponseParser -->|"errors"| Exceptions
    FactoryInterface -->|"errors"| Exceptions
    IteratorLayerNote["Exceptions bubble up"]:::exception
    Exceptions --> Consumer

    %% Extensibility
    FactoryInterface -.->|optional override| ConcreteFactories
    Client -.->|swap HTTP impl| ResponseParser

    %% Click Events
    click OAuthToken "https://github.com/tustin/psn-php/blob/master/src/OAuthToken.php"
    click Client "https://github.com/tustin/psn-php/blob/master/src/Client.php"
    click Api "https://github.com/tustin/psn-php/blob/master/src/Api.php"
    click ResponseParser "https://github.com/tustin/psn-php/blob/master/src/Http/ResponseParser.php"
    click FactoryInterface "https://github.com/tustin/psn-php/blob/master/src/Interfaces/FactoryInterface.php"
    click Models "https://github.com/tustin/psn-php/tree/master/src/Model/"
    click Enums "https://github.com/tustin/psn-php/tree/master/src/Enum/"
    click ConcreteFactories "https://github.com/tustin/psn-php/tree/master/src/Factory/"
    click Iterators "https://github.com/tustin/psn-php/tree/master/src/Iterator/"
    click Exceptions "https://github.com/tustin/psn-php/tree/master/src/Exception/"
    click OperandParser "https://github.com/tustin/psn-php/blob/master/src/Traits/OperandParser.php"

    %% Styles
    classDef external fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef core fill:#90EE90,stroke:#333,stroke-width:1px
    classDef domain fill:#FFFF99,stroke:#333,stroke-width:1px
    classDef mapping fill:#FFA500,stroke:#333,stroke-width:1px
    classDef iterator fill:#E0FFFF,stroke:#333,stroke-width:1px
    classDef exception fill:#FFCCCB,stroke:#333,stroke-width:1px
    classDef utility fill:#D3D3D3,stroke:#333,stroke-width:1px
```
