| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Cookie\Components; |
| 4 |
|
| 5 |
use Cookiez\Modules\Cookie\Classes\Dto\Cookie_DTO; |
| 6 |
use Cookiez\Modules\Cookie\Database\Cookie_Entry; |
| 7 |
use Cookiez\Modules\Script\Components\Script; |
| 8 |
use RuntimeException; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Cookie { |
| 15 |
public function create( Cookie_DTO $dto ): Cookie_DTO { |
| 16 |
// "no domain provided" collapses to the empty-string canonical form |
| 17 |
// on insert; PATCH keeps null to mean "don't touch the column". |
| 18 |
$dto->domain = $dto->domain ?? ''; |
| 19 |
|
| 20 |
$existing = Cookie_Entry::find_by_name_domain( $dto->name, $dto->domain ); |
| 21 |
|
| 22 |
if ( $existing && $existing->id ) { |
| 23 |
throw new RuntimeException( 'cookie_duplicate' ); |
| 24 |
} |
| 25 |
|
| 26 |
return Cookie_DTO::from_entry( Cookie_Entry::insert_one( $dto ) ); |
| 27 |
} |
| 28 |
|
| 29 |
public function replace( int $id, Cookie_DTO $dto ): Cookie_DTO { |
| 30 |
return $this->write( $id, $dto ); |
| 31 |
} |
| 32 |
|
| 33 |
public function patch( int $id, Cookie_DTO $dto ): Cookie_DTO { |
| 34 |
return $this->write( $id, $dto ); |
| 35 |
} |
| 36 |
|
| 37 |
public function delete( int $id ): void { |
| 38 |
$entry = new Cookie_Entry( [ 'id' => $id ] ); |
| 39 |
|
| 40 |
if ( ! $entry->id ) { |
| 41 |
throw new RuntimeException( 'cookie_not_found' ); |
| 42 |
} |
| 43 |
|
| 44 |
( new Script() )->detach_from_cookie( $id ); |
| 45 |
|
| 46 |
$entry->delete(); |
| 47 |
} |
| 48 |
|
| 49 |
public function get( int $id ): Cookie_DTO { |
| 50 |
$entry = new Cookie_Entry( [ 'id' => $id ] ); |
| 51 |
|
| 52 |
if ( ! $entry->id ) { |
| 53 |
throw new RuntimeException( 'cookie_not_found' ); |
| 54 |
} |
| 55 |
|
| 56 |
return Cookie_DTO::from_entry( $entry ); |
| 57 |
} |
| 58 |
|
| 59 |
public function list( array $filters = [] ): array { |
| 60 |
$limit = isset( $filters['limit'] ) ? (int) $filters['limit'] : null; |
| 61 |
$offset = isset( $filters['offset'] ) ? (int) $filters['offset'] : null; |
| 62 |
|
| 63 |
unset( $filters['limit'], $filters['offset'] ); |
| 64 |
|
| 65 |
$rows = Cookie_Entry::find_all( $filters, $limit, $offset ); |
| 66 |
|
| 67 |
return array_map( |
| 68 |
fn( $row ) => Cookie_DTO::from_entry( new Cookie_Entry( [ 'data' => $row ] ) ), |
| 69 |
$rows |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
public function upsert_from_scan( Cookie_DTO $dto ): Cookie_DTO { |
| 74 |
$existing = Cookie_Entry::find_by_name_domain( $dto->name, $dto->domain ); |
| 75 |
|
| 76 |
if ( $existing && $existing->id ) { |
| 77 |
return Cookie_DTO::from_entry( $existing ); |
| 78 |
} |
| 79 |
|
| 80 |
return Cookie_DTO::from_entry( Cookie_Entry::insert_one( $dto ) ); |
| 81 |
} |
| 82 |
|
| 83 |
private function write( int $id, Cookie_DTO $dto ): Cookie_DTO { |
| 84 |
$entry = new Cookie_Entry( [ 'id' => $id ] ); |
| 85 |
|
| 86 |
if ( ! $entry->id ) { |
| 87 |
throw new RuntimeException( 'cookie_not_found' ); |
| 88 |
} |
| 89 |
|
| 90 |
$previous_category = (string) ( $entry->category ?? '' ); |
| 91 |
|
| 92 |
foreach ( $dto->to_entry() as $key => $value ) { |
| 93 |
$entry->set( $key, $value ); |
| 94 |
} |
| 95 |
|
| 96 |
$entry->save(); |
| 97 |
|
| 98 |
if ( null !== $dto->category && $dto->category !== $previous_category ) { |
| 99 |
( new Script() )->sync_category_for_cookie( $id, $dto->category ); |
| 100 |
} |
| 101 |
|
| 102 |
return Cookie_DTO::from_entry( $entry ); |
| 103 |
} |
| 104 |
} |
| 105 |
|