Skip to content
acc-create-policy logo

Policy Pattern Generator

acc-create-policy

Generates Policy pattern for PHP 8.5. Creates encapsulated business rules for authorization, validation, and domain constraints. Includes unit tests.

SKILL.md

Full skill instructions

Policy Pattern Generator

Creates Policy pattern infrastructure for encapsulating business rules and authorization logic.

When to Use

ScenarioExample
Authorization checksCan user cancel order?
Business rule validationIs discount applicable?
Complex conditionsMultiple rules combined
Auditable decisionsLog why access denied

Component Characteristics

PolicyInterface

  • Single responsibility rule
  • Returns authorization result
  • Provides denial reasons

Policy Implementation

  • Encapsulates one business rule
  • Stateless evaluation
  • Composable with other policies

PolicyResult

  • Success/​failure status
  • Denial reasons
  • Metadata for logging

Generation Process

Step 1: Generate Shared Components

Path: src/​Domain/​Shared/​Policy/

  1. PolicyResult.php — Result value object with and/​or composition
  2. CompositionMode.php — Enum for AllMustPass/​AnyMustPass

Step 2: Generate Policy Interface

Path: src/​Domain/​{BoundedContext}/​Policy/

  1. {Name}PolicyInterface.php — Policy contract

Step 3: Generate Concrete Policies

Path: src/​Domain/​{BoundedContext}/​Policy/

  1. {Rule1}Policy.php — First rule implementation
  2. {Rule2}Policy.php — Second rule implementation
  3. {Name}Policy.php — Composite policy combining rules

Step 4: Generate Exception

Path: src/​Domain/​Shared/​Exception/

  1. PolicyViolationException.php — Exception with policy context

Step 5: Generate Tests

  1. {Rule}PolicyTest.php — Individual rule tests
  2. {Name}PolicyTest.php — Composite policy tests
  3. PolicyResultTest.php — Result composition tests

File Placement

ComponentPath
Policy Interfacesrc/​Domain/​{BoundedContext}/​Policy/
Policy Implementationsrc/​Domain/​{BoundedContext}/​Policy/
PolicyResultsrc/​Domain/​Shared/​Policy/
Exceptionsrc/​Domain/​Shared/​Exception/
Unit Teststests/​Unit/​Domain/​{BoundedContext}/​Policy/

Naming Conventions

ComponentPatternExample
Interface{Name}PolicyInterfaceOrderCancellationPolicyInterface
Implementation{Rule}PolicyOrderOwnershipPolicy
Composite{Name}PolicyOrderCancellationPolicy
ResultPolicyResultPolicyResult
ExceptionPolicyViolationExceptionPolicyViolationException
Test{ClassName}TestOrderOwnershipPolicyTest

Quick Template Reference

PolicyInterface

interface {Name}PolicyInterface
{
    public function evaluate({SubjectType} $subject, {ResourceType} $resource): PolicyResult;
    public function getRuleName(): string;
}

PolicyResult

final readonly class PolicyResult
{
    public static function allow(): self;
    public static function deny(string $reason, array $metadata = []): self;
    public function isAllowed(): bool;
    public function isDenied(): bool;
    public function and(self $other): self; // Both must pass
    public function or(self $other): self;  // Either can pass
}

Policy Implementation

final readonly class {Rule}Policy implements {Name}PolicyInterface
{
    public function evaluate({Subject} $subject, {Resource} $resource): PolicyResult
    {
        if ({condition}) {
            return PolicyResult::allow();
        }
        return PolicyResult::deny('{reason}', ['context' => 'data']);
    }

    public function getRuleName(): string
    {
        return '{rule_name}';
    }
}

Composite Policy

final readonly class {Name}Policy implements {Name}PolicyInterface
{
    public function evaluate({Subject} $subject, {Resource} $resource): PolicyResult
    {
        return $this->rule1Policy->evaluate($subject, $resource)
            ->and($this->rule2Policy->evaluate($subject, $resource))
            ->and($this->rule3Policy->evaluate($subject, $resource));
    }
}

Usage Example

// In UseCase
$result = $this->cancellationPolicy->evaluate($user, $order);

if ($result->isDenied()) {
    throw new PolicyViolationException(
        $this->cancellationPolicy->getRuleName(),
        $result->getReason(),
        $result->metadata
    );
}

$order->cancel($reason);

Anti-patterns to Avoid

Anti-patternProblemSolution
Side EffectsPolicy modifies stateKeep evaluation pure
Boolean ReturnsNo denial reasonUse PolicyResult
Fat PoliciesToo many rulesSplit into composable policies
Hardcoded ValuesCan't configureInject thresholds
No Logging ContextCan't debugInclude metadata

References

For complete PHP templates and examples, see:

  • references/​templates.md — Policy, composite, result templates
  • references/​examples.md — Order cancellation policies and tests

More DevOps & CI/CD skills

Project scaffolding, deployment configuration, and CI/CD setup for Google ADK agents.

6K 472.6K
View

Set up tracing, logging, and monitoring for deployed ADK agents across Cloud Trace, BigQuery, and third-party platforms.

6K 472.6K
View

Enterprise Azure infrastructure architect generating Bicep or Terraform from workload descriptions.

1.5K 454.3K
View
azure-kubernetes logo
DevOps & CI/CD

azure-kubernetes

Plan and configure production-ready Azure Kubernetes Service clusters with Day-0 and Day-1 best practices.

1.5K 447.1K
View

Raw mechanical interfaces fusing Swiss typographic print with military terminal aesthetics. Rigid grids, extreme type scale contrast, utilitarian color, analog degradation effects. For data-heavy dashboards, portfolios, or editorial sites that need to feel like declassified blueprints.

92.7K 354.9K
View
just-scrape logo
DevOps & CI/CD

just-scrape

Web search, scraping, extraction, crawling, and monitoring via ScrapeGraph AI CLI.

69 245K
View

Skill for working with Firebase Hosting (Classic). Use this when you want to deploy static web apps, Single Page Apps (SPAs), or simple microservices. Do NOT use for Firebase App Hosting.

462 159.7K
View

Deploy and manage web apps with Firebase App Hosting. Use this skill when deploying Next.js/Angular apps with backends.

462 159.1K
View
deploy-to-vercel logo
DevOps & CI/CD

deploy-to-vercel

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

31.9K 146.6K
View
programmatic-seo logo
DevOps & CI/CD

programmatic-seo

Build SEO-optimized pages at scale using templates, data, and proven playbook patterns.

53.3K 140.1K
View

Design and build isolated, reusable Convex backend components with clear boundaries and app-facing wrappers.

63 123.5K
View

Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. "deploy to vercel", "set up vercel", "add environment variables to vercel".

31.9K 116.1K
View

Coding & apps AI tools

Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View
Imagica logo
Coding & apps

Imagica

Build a no-code AI app in minutes.

Freemium
View
E
Coding & apps

Emergent.sh

An IDE for code migration from legacy to modern frameworks through coding agents.

Freemium
View
Wonder Dynamics logo
Coding & apps

Wonder Dynamics

Automate CGI animation in live-action scenes

Paid
View
M
Coding & apps

Mixo

Launch a website in seconds with AI.

Paid
View
AI Code Convert logo
Coding & apps

AI Code Convert

Streamline Your Coding Experience with AI Code Helper

Free
View