| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Scanner\Database; |
| 4 |
|
| 5 |
use Cookiez\Classes\Database\Entry; |
| 6 |
use Cookiez\Modules\Scanner\Classes\Dto\Scan_Url_DTO; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Scan_Url_Entry extends Entry { |
| 13 |
protected static function get_helper_class(): string { |
| 14 |
return Scan_Url_Table::class; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* @param Scan_Url_DTO[] $dtos |
| 19 |
* @return false|int |
| 20 |
*/ |
| 21 |
public static function insert_many( array $dtos ) { |
| 22 |
if ( empty( $dtos ) ) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
$values = array_map( function ( Scan_Url_DTO $dto ) { |
| 27 |
$scan_id = Scan_Url_Table::prepare_value( (int) $dto->scan_id, '%d' ); |
| 28 |
$url = Scan_Url_Table::prepare_value( esc_url_raw( $dto->url ) ); |
| 29 |
$status = Scan_Url_Table::prepare_value( $dto->status ); |
| 30 |
$error_code = Scan_Url_Table::prepare_value( $dto->error_code ); |
| 31 |
|
| 32 |
return "({$scan_id}, {$url}, {$status}, {$error_code}, NOW())"; |
| 33 |
}, $dtos ); |
| 34 |
|
| 35 |
$columns = sprintf( |
| 36 |
'(`%s`, `%s`, `%s`, `%s`, `%s`)', |
| 37 |
Scan_Url_Table::SCAN_ID, |
| 38 |
Scan_Url_Table::URL, |
| 39 |
Scan_Url_Table::SCAN_STATUS, |
| 40 |
Scan_Url_Table::ERROR_CODE, |
| 41 |
Scan_Url_Table::CREATED_AT |
| 42 |
); |
| 43 |
|
| 44 |
return Scan_Url_Table::insert_many( $values, $columns ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return false|int Rows updated, or false on error. |
| 49 |
*/ |
| 50 |
public static function update_many( array $set, array $where ) { |
| 51 |
return Scan_Url_Table::update( $set, $where ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return Scan_Url_DTO[] |
| 56 |
*/ |
| 57 |
public static function find_many( array $where = [] ): array { |
| 58 |
$rows = Scan_Url_Table::select( |
| 59 |
'*', |
| 60 |
! empty( $where ) ? $where : '1', |
| 61 |
null, |
| 62 |
null, |
| 63 |
'', |
| 64 |
[ Scan_Url_Table::ID => 'ASC' ] |
| 65 |
); |
| 66 |
|
| 67 |
if ( ! is_array( $rows ) ) { |
| 68 |
return []; |
| 69 |
} |
| 70 |
|
| 71 |
return array_map( fn( $row ) => Scan_Url_DTO::from_entry( $row ), $rows ); |
| 72 |
} |
| 73 |
} |
| 74 |
|