6225e772f8
CI / Coding Standards (pull_request) Successful in 1m0s
CI / PHPStan (pull_request) Successful in 1m4s
CI / Tests (PHP 8.1) (pull_request) Successful in 59s
CI / Tests (PHP 8.2) (pull_request) Successful in 56s
CI / Tests (PHP 8.3) (pull_request) Successful in 57s
CI / No Debug Code (pull_request) Successful in 3s
CI / Build Plugin Zip (pull_request) Has been skipped
Implements #6: studio admins draft, version, and publish policies; the public registration gate reads the current published version of each, and acceptance is recorded against the exact version so a new version must be re-accepted at the next booking. - src/Policy/: Policy, PolicyVersion, PolicyAcceptance value objects; PolicyRepository, PolicyVersionRepository, AcceptanceRepository; PolicyService (orchestrates create/add-draft/publish across the policies and versions tables); PolicyEndpoint (REST); PolicyController + templates/admin/policies.php (Policies admin menu, manage_policies) - us_policies, us_policy_versions, us_policy_acceptances tables in Schema - REST: public GET /policies (current published versions); manage_policies for create, add version, edit draft, and publish - Wiring in Plugin, RestRegistrar, AdminMenu AcceptanceRepository is built now and consumed by the booking/enrolment gate in #3/#4. Also bump PHPStan to --memory-limit=1G in the composer lint script; the default 128M now crashes the analysis as the codebase has grown. Tests: tests/Unit/Policy/ (value objects, repositories, service). composer test (90 total), cs, and PHPStan level 6 all pass. Refs #6 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
70 lines
1.6 KiB
PHP
70 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Policy;
|
|
|
|
class PolicyRepository {
|
|
|
|
private string $table;
|
|
|
|
public function __construct( private \wpdb $db ) {
|
|
$this->table = $db->prefix . 'us_policies';
|
|
}
|
|
|
|
public function insert( Policy $policy ): int {
|
|
$this->db->insert(
|
|
$this->table,
|
|
[
|
|
'title' => $policy->title,
|
|
'slug' => $policy->slug,
|
|
'current_version_id' => $policy->currentVersionId,
|
|
'created_at' => current_time( 'mysql' ),
|
|
],
|
|
[ '%s', '%s', '%d', '%s' ]
|
|
);
|
|
|
|
return $this->db->insert_id;
|
|
}
|
|
|
|
public function updateCurrentVersion( int $policyId, int $versionId ): bool {
|
|
return false !== $this->db->update(
|
|
$this->table,
|
|
[ 'current_version_id' => $versionId ],
|
|
[ 'id' => $policyId ],
|
|
[ '%d' ],
|
|
[ '%d' ]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* All policies, ordered by title.
|
|
*
|
|
* @return list<Policy>
|
|
*/
|
|
public function findAll(): array {
|
|
$rows = $this->db->get_results( "SELECT * FROM {$this->table} ORDER BY title ASC" );
|
|
|
|
return array_map( Policy::fromRow( ... ), $rows ?? [] );
|
|
}
|
|
|
|
public function findById( int $id ): ?Policy {
|
|
$row = $this->db->get_row(
|
|
$this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id )
|
|
);
|
|
|
|
return $row ? Policy::fromRow( $row ) : null;
|
|
}
|
|
|
|
public function findBySlug( string $slug ): ?Policy {
|
|
$row = $this->db->get_row(
|
|
$this->db->prepare( "SELECT * FROM {$this->table} WHERE slug = %s", $slug )
|
|
);
|
|
|
|
return $row ? Policy::fromRow( $row ) : null;
|
|
}
|
|
|
|
public function delete( int $id ): bool {
|
|
return (bool) $this->db->delete( $this->table, [ 'id' => $id ], [ '%d' ] );
|
|
}
|
|
}
|