CustomerRepository.php
2 weeks ago
ProviderRepository.php
4 weeks ago
UserRepository.php
2 months ago
WPUserRepository.php
3 months ago
WPUserRepository.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\User; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\Database\ConnectionInterface; |
| 6 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 7 | use AmeliaBooking\Infrastructure\Connection; |
| 8 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 9 | |
| 10 | /** |
| 11 | * Class WPUserRepository |
| 12 | * |
| 13 | * @package AmeliaBooking\Infrastructure\Repository\User |
| 14 | */ |
| 15 | class WPUserRepository |
| 16 | { |
| 17 | /** @var ConnectionInterface */ |
| 18 | protected $connection; |
| 19 | |
| 20 | /** @var string */ |
| 21 | protected $table; |
| 22 | |
| 23 | /** @var string */ |
| 24 | protected $metaTable; |
| 25 | |
| 26 | /** @var string */ |
| 27 | protected $prefix; |
| 28 | |
| 29 | /** |
| 30 | * WPUserRepository constructor. |
| 31 | * |
| 32 | * @param Connection $connection |
| 33 | * @param $table |
| 34 | * @param $metaTable |
| 35 | * @param $prefix |
| 36 | */ |
| 37 | public function __construct(Connection $connection, $table, $metaTable, $prefix) |
| 38 | { |
| 39 | $this->connection = $connection(); |
| 40 | $this->table = $table; |
| 41 | $this->metaTable = $metaTable; |
| 42 | $this->prefix = $prefix; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param array $params |
| 47 | * @param array $excludeUserIds |
| 48 | * |
| 49 | * @return array |
| 50 | * @throws QueryExecutionException |
| 51 | */ |
| 52 | public function getAllNonRelatedWPUsers($params, $excludeUserIds) |
| 53 | { |
| 54 | $excludeIdParams = []; |
| 55 | |
| 56 | try { |
| 57 | $params = [ |
| 58 | ':id' => $params['id'], |
| 59 | ':role' => '%wpamelia-' . $params['role'] . '%', |
| 60 | ]; |
| 61 | |
| 62 | foreach ((array)$excludeUserIds as $key => $id) { |
| 63 | $excludeIdParams[":id$key"] = $id; |
| 64 | } |
| 65 | |
| 66 | $whereExcludeIds = $excludeIdParams ? |
| 67 | ' AND wp_user.ID NOT IN (' . implode(',', $excludeIdParams) . ')' : ''; |
| 68 | |
| 69 | $usersTable = UsersTable::getTableName(); |
| 70 | |
| 71 | global $wpdb; |
| 72 | |
| 73 | $statement = $this->connection->prepare( |
| 74 | "SELECT wp_user.ID AS value, wp_user.display_name AS label |
| 75 | FROM {$this->table} AS wp_user |
| 76 | INNER JOIN {$this->metaTable} AS wp_usermeta |
| 77 | ON wp_user.ID = wp_usermeta.user_id |
| 78 | WHERE |
| 79 | wp_user.ID NOT IN ( |
| 80 | SELECT externalId |
| 81 | FROM $usersTable |
| 82 | WHERE externalId IS NOT NULL |
| 83 | AND externalId != :id |
| 84 | ) |
| 85 | {$whereExcludeIds} |
| 86 | AND wp_usermeta.meta_key = '{$wpdb->prefix}capabilities' |
| 87 | AND wp_usermeta.meta_value LIKE :role |
| 88 | GROUP BY wp_user.ID" |
| 89 | ); |
| 90 | |
| 91 | $statement->execute($params); |
| 92 | |
| 93 | $rows = $statement->fetchAll(); |
| 94 | } catch (\Exception $e) { |
| 95 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 96 | } |
| 97 | |
| 98 | $items = []; |
| 99 | foreach ($rows as $row) { |
| 100 | $row['value'] = (int)$row['value']; |
| 101 | $items[] = $row; |
| 102 | } |
| 103 | |
| 104 | return $items; |
| 105 | } |
| 106 | } |
| 107 |