PHP code example of php-architecture-kit / actor

1. Go to this page and download the library: Download php-architecture-kit/actor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

php-architecture-kit / actor example snippets


use PhpArchitecture\Actor\IdentifiedActor;
use PhpArchitecture\Actor\NamedActor;
use PhpArchitecture\Actor\UnknownActor;
use PhpArchitecture\Actor\Identity\ActorId;

// User actor (with UUID identity)
$userId = ActorId::fromString('df516cba-fb13-4f45-8335-00252f1b87e2');
$userActor = new IdentifiedActor($userId);
echo $userActor->identifier(); // 'df516cba-fb13-4f45-8335-00252f1b87e2'

// System actor (cron job, worker, etc.)
$NamedActor = new NamedActor('order-processor');
echo $NamedActor->identifier(); // 'order-processor'

// Unknown actor (anonymous, unidentified)
$unknownActor = new UnknownActor();
echo $unknownActor->identifier(); // 'unknown'

use PhpArchitecture\Actor\IdentifiedActor;
use PhpArchitecture\Actor\Identity\ActorId;
use PhpArchitecture\Uuid\Uuid;

// Domain-specific actor ID
final class UserId extends ActorId
{
    public static function new(): static
    {
        return static::v7();
    }
}

// Domain-specific actor
class UserActor extends IdentifiedActor
{
    private string $email;
    private array $roles;

    public function __construct(UserId $id, string $email, array $roles = [])
    {
        parent::__construct($id);
        $this->email = $email;
        $this->roles = $roles;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function hasRole(string $role): bool
    {
        return in_array($role, $this->roles, true);
    }
}

use PhpArchitecture\Actor\Actor;
use PhpArchitecture\DomainCore\AggregateRoot;
use PhpArchitecture\DomainCore\Exception\InsufficientPrivilegeException;

class Order extends AggregateRoot
{
    private string $ownerId;

    public function cancel(Actor $actor): void
    {
        if ($actor->identifier() !== $this->ownerId) {
            throw new InsufficientPrivilegeException('Only owner can cancel order');
        }

        $this->status = 'cancelled';
        $this->recordEvent(new OrderCancelled($this->id, $actor->identifier()));
    }
}

use PhpArchitecture\DomainCore\DomainEvent;

class OrderCancelled implements DomainEvent
{
    public function __construct(
        public readonly string $orderId,
        public readonly string $cancelledBy, // Actor identifier
        public readonly \DateTimeImmutable $cancelledAt,
    ) {}
}

// Symfony Controller
use PhpArchitecture\Actor\Actor;
use PhpArchitecture\Actor\IdentifiedActor;
use PhpArchitecture\Actor\UnknownActor;
use PhpArchitecture\Actor\Identity\ActorId;

class OrderController
{
    public function cancel(Request $request, OrderService $service): Response
    {
        $actor = $this->resolveActor($request);
        $service->cancelOrder($request->get('orderId'), $actor);
        
        return new JsonResponse(['status' => 'cancelled']);
    }

    private function resolveActor(Request $request): Actor
    {
        $userId = $request->attributes->get('user_id');
        
        if ($userId) {
            return new IdentifiedActor(ActorId::fromString($userId));
        }
        
        return new UnknownActor();
    }
}