| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Script\Components; |
| 4 |
|
| 5 |
use Cookiez\Modules\Cookie\Components\Cookie; |
| 6 |
use Cookiez\Modules\Script\Classes\Dto\Script_DTO; |
| 7 |
use Cookiez\Modules\Script\Database\Script_Entry; |
| 8 |
use RuntimeException; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Script { |
| 15 |
public function create( Script_DTO $dto ): Script_DTO { |
| 16 |
$existing = Script_Entry::find_by_value( $dto->value ); |
| 17 |
|
| 18 |
if ( $existing && $existing->id ) { |
| 19 |
throw new RuntimeException( 'script_duplicate' ); |
| 20 |
} |
| 21 |
|
| 22 |
if ( null !== $dto->cookie_id ) { |
| 23 |
$dto->category = ( new Cookie() )->get( (int) $dto->cookie_id )->category; |
| 24 |
} |
| 25 |
|
| 26 |
$entry = Script_Entry::insert_one( $dto ); |
| 27 |
|
| 28 |
if ( ! $dto->name ) { |
| 29 |
$entry->name = sprintf( 'Script %d', (int) $entry->id ); |
| 30 |
$entry->save(); |
| 31 |
} |
| 32 |
|
| 33 |
return Script_DTO::from_entry( $entry ); |
| 34 |
} |
| 35 |
|
| 36 |
public function replace( int $id, Script_DTO $dto ): Script_DTO { |
| 37 |
return $this->write( $id, $dto ); |
| 38 |
} |
| 39 |
|
| 40 |
public function patch( int $id, Script_DTO $dto ): Script_DTO { |
| 41 |
return $this->write( $id, $dto ); |
| 42 |
} |
| 43 |
|
| 44 |
public function delete( int $id ): void { |
| 45 |
$entry = new Script_Entry( [ 'id' => $id ] ); |
| 46 |
|
| 47 |
if ( ! $entry->id ) { |
| 48 |
throw new RuntimeException( 'script_not_found' ); |
| 49 |
} |
| 50 |
|
| 51 |
$entry->delete(); |
| 52 |
} |
| 53 |
|
| 54 |
public function get( int $id ): Script_DTO { |
| 55 |
$entry = new Script_Entry( [ 'id' => $id ] ); |
| 56 |
|
| 57 |
if ( ! $entry->id ) { |
| 58 |
throw new RuntimeException( 'script_not_found' ); |
| 59 |
} |
| 60 |
|
| 61 |
return Script_DTO::from_entry( $entry ); |
| 62 |
} |
| 63 |
|
| 64 |
public function list(): array { |
| 65 |
$rows = Script_Entry::find_all(); |
| 66 |
|
| 67 |
return array_map( |
| 68 |
fn( $row ) => Script_DTO::from_entry( new Script_Entry( [ 'data' => $row ] ) ), |
| 69 |
$rows |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
public function upsert_from_scan( Script_DTO $dto ): Script_DTO { |
| 74 |
$existing = Script_Entry::find_by_value( $dto->value ); |
| 75 |
|
| 76 |
if ( $existing && $existing->id ) { |
| 77 |
return Script_DTO::from_entry( $existing ); |
| 78 |
} |
| 79 |
|
| 80 |
return $this->create( $dto ); |
| 81 |
} |
| 82 |
|
| 83 |
public function sync_category_for_cookie( int $cookie_id, string $category ): void { |
| 84 |
Script_Entry::retag_for_cookie( $cookie_id, $category ); |
| 85 |
} |
| 86 |
|
| 87 |
public function detach_from_cookie( int $cookie_id ): void { |
| 88 |
Script_Entry::detach_from_cookie( $cookie_id ); |
| 89 |
} |
| 90 |
|
| 91 |
private function write( int $id, Script_DTO $dto ): Script_DTO { |
| 92 |
$entry = new Script_Entry( [ 'id' => $id ] ); |
| 93 |
|
| 94 |
if ( ! $entry->id ) { |
| 95 |
throw new RuntimeException( 'script_not_found' ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( null !== $dto->value && '' !== $dto->value ) { |
| 99 |
$clash = Script_Entry::find_by_value( $dto->value ); |
| 100 |
|
| 101 |
if ( $clash && $clash->id && (int) $clash->id !== $id ) { |
| 102 |
throw new RuntimeException( 'script_duplicate' ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if ( null === $dto->cookie_id && null !== $entry->cookie_id ) { |
| 107 |
$dto->cookie_id = $entry->cookie_id; |
| 108 |
} |
| 109 |
|
| 110 |
if ( null !== $dto->cookie_id ) { |
| 111 |
$dto->category = ( new Cookie() )->get( $dto->cookie_id )->category; |
| 112 |
} |
| 113 |
|
| 114 |
foreach ( $dto->to_entry() as $key => $value ) { |
| 115 |
$entry->set( $key, $value ); |
| 116 |
} |
| 117 |
|
| 118 |
$entry->save(); |
| 119 |
|
| 120 |
return Script_DTO::from_entry( $entry ); |
| 121 |
} |
| 122 |
} |
| 123 |
|