/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Repositories/Admin/ArrayUserRepository.php
152 строки
4 KB
vivanenko
refactoring to stores
21 май 2025, 19:55
21 май 2025, 19:55
adcfd60
Код
Авторство
О чём код?
<?php namespace App\Repositories\Admin; use App\Contracts\Admin\UserRepositoryInterface; use App\DTOs\Admin\UserDTO; use Carbon\Carbon; class ArrayUserRepository implements UserRepositoryInterface { private array $users = []; public function __construct() { // Add some mock data $this->users = [ 1 => [ 'id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com', 'telegram_username' => 'johndoe', 'tariff_level' => 'Pro', 'is_active' => true, 'telegram_data' => null, 'last_login' => '2024-03-01 10:00:00', 'created_at' => '2024-03-01 10:00:00', 'updated_at' => '2024-03-01 10:00:00', ], 2 => [ 'id' => 2, 'name' => 'Jane Smith', 'email' => 'jane@example.com', 'telegram_username' => 'janesmith', 'tariff_level' => 'Basic', 'is_active' => true, 'telegram_data' => null, 'last_login' => null, 'created_at' => '2024-03-15 15:30:00', 'updated_at' => '2024-03-15 15:30:00', ], ]; } public function all(array $columns = ['*']): array { return array_map(fn ($user) => UserDTO::fromArray($user), $this->users); } public function paginate(int $perPage = 15, array $columns = ['*']): array { $page = request()->get('page', 1); $total = count($this->users); $offset = ($page - 1) * $perPage; $items = array_slice($this->users, $offset, $perPage); return [ 'data' => array_map(fn ($user) => UserDTO::fromArray($user), $items), 'total' => $total, 'per_page' => $perPage, 'current_page' => $page, 'last_page' => ceil($total / $perPage), ]; } public function find(int $id, array $columns = ['*']): ?UserDTO { return isset($this->users[$id]) ? UserDTO::fromArray($this->users[$id]) : null; } public function findByEmail(string $email): ?UserDTO { $user = array_filter($this->users, fn($u) => $u['email'] === $email); return !empty($user) ? UserDTO::fromArray(reset($user)) : null; } public function findByTelegramUsername(string $username): ?UserDTO { $user = array_filter($this->users, fn($u) => $u['telegram_username'] === $username); return !empty($user) ? UserDTO::fromArray(reset($user)) : null; } public function create(array $data): UserDTO { $id = count($this->users) + 1; $now = Carbon::now()->format('Y-m-d H:i:s'); $user = array_merge($data, [ 'id' => $id, 'created_at' => $now, 'updated_at' => $now, ]); $this->users[$id] = $user; return UserDTO::fromArray($user); } public function update(int $id, array $data): ?UserDTO { if (!isset($this->users[$id])) { return null; } $this->users[$id] = array_merge($this->users[$id], $data, [ 'updated_at' => Carbon::now()->format('Y-m-d H:i:s'), ]); return UserDTO::fromArray($this->users[$id]); } public function delete(int $id): bool { if (!isset($this->users[$id])) { return false; } unset($this->users[$id]); return true; } public function updateTelegramData(int $id, array $telegramData): bool { if (!isset($this->users[$id])) { return false; } $this->users[$id]['telegram_data'] = $telegramData; $this->users[$id]['updated_at'] = Carbon::now()->format('Y-m-d H:i:s'); return true; } public function updateTariffLevel(int $id, string $tariffLevel): bool { if (!isset($this->users[$id])) { return false; } $this->users[$id]['tariff_level'] = $tariffLevel; $this->users[$id]['updated_at'] = Carbon::now()->format('Y-m-d H:i:s'); return true; } public function updateLastLogin(int $id): bool { if (!isset($this->users[$id])) { return false; } $this->users[$id]['last_login'] = Carbon::now()->format('Y-m-d H:i:s'); $this->users[$id]['updated_at'] = Carbon::now()->format('Y-m-d H:i:s'); return true; } }