/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Repositories/Admin/ArrayProxyRepository.php
117 строк
3 KB
vivanenko
refactoring to stores
21 май 2025, 19:55
21 май 2025, 19:55
adcfd60
Код
Авторство
О чём код?
<?php namespace App\Repositories\Admin; use App\Contracts\Admin\ProxyRepositoryInterface; use App\DTOs\Admin\ProxyDTO; use Carbon\Carbon; use Illuminate\Pagination\LengthAwarePaginator; class ArrayProxyRepository implements ProxyRepositoryInterface { private array $proxies; public function __construct() { $this->proxies = $this->generateProxies(); } public function paginate(int $perPage = 15, array $columns = ['*']): LengthAwarePaginator { $page = request()->get('page', 1); $offset = ($page - 1) * $perPage; $items = array_slice($this->proxies, $offset, $perPage); $items = array_map(fn(array $proxy) => ProxyDTO::fromArray($proxy), $items); return new LengthAwarePaginator( items: $items, total: count($this->proxies), perPage: $perPage, currentPage: $page, options: ['path' => request()->url()] ); } public function find(int $id, array $columns = ['*']): ?ProxyDTO { $proxy = array_filter($this->proxies, fn($proxy) => $proxy['id'] === $id); if (empty($proxy)) { return null; } return ProxyDTO::fromArray(reset($proxy)); } public function create(array $data): ProxyDTO { $id = max(array_column($this->proxies, 'id')) + 1; $now = Carbon::now()->format('Y-m-d H:i:s'); $proxy = array_merge($data, [ 'id' => $id, 'created_at' => $now, 'updated_at' => $now, ]); $this->proxies[] = $proxy; return ProxyDTO::fromArray($proxy); } public function update(int $id, array $data): ?ProxyDTO { $index = array_search($id, array_column($this->proxies, 'id')); if ($index === false) { return null; } $this->proxies[$index] = array_merge($this->proxies[$index], $data, [ 'updated_at' => Carbon::now()->format('Y-m-d H:i:s') ]); return ProxyDTO::fromArray($this->proxies[$index]); } public function delete(int $id): bool { $index = array_search($id, array_column($this->proxies, 'id')); if ($index === false) { return false; } unset($this->proxies[$index]); $this->proxies = array_values($this->proxies); return true; } private function generateProxies(): array { $proxies = []; $types = ['http', 'socks4', 'socks5']; $statuses = ['active', 'inactive', 'error']; $now = Carbon::now(); for ($i = 1; $i <= 50; $i++) { $createdAt = $now->subDays(rand(1, 365))->format('Y-m-d H:i:s'); $updatedAt = $now->addDays(rand(1, 30))->format('Y-m-d H:i:s'); $proxies[] = [ 'id' => $i, 'ip' => '192.168.' . rand(1, 255) . '.' . rand(1, 255), 'port' => rand(1024, 65535), 'type' => $types[array_rand($types)], 'username' => rand(0, 1) ? 'user' . $i : null, 'password' => rand(0, 1) ? 'pass' . $i : null, 'status' => $statuses[array_rand($statuses)], 'created_at' => $createdAt, 'updated_at' => $updatedAt, ]; } return $proxies; } public function all(array $columns = ['*']) { // TODO: Implement all() method. } }