/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Models/Proxy.php
226 строк
5 KB
vivanenko
buy -> connect -> create taps
27 май 2025, 22:40
27 май 2025, 22:40
6209a9a
Код
Авторство
О чём код?
<?php namespace App\Models; use App\Enums\ProxyType; use App\Enums\ProxyStatus; use DateTime; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; /** * @property string $ip The proxy server IP address * @property int $port The proxy server port number * @property string|null $username The proxy authentication username * @property string|null $password The proxy authentication password * @property ProxyType $type The proxy type (http, https, socks4, socks5) * @property bool $is_active Whether the proxy is active and available for use * @property DateTime|null $last_check_at When the proxy was last checked * @property bool|null $last_check_status The status of the last proxy check * @property int $fail_count Number of consecutive failed checks * @property DateTime $created_at When the proxy was created * @property DateTime $updated_at When the proxy was last updated * @property DateTime|null $deleted_at When the proxy was soft deleted */ class Proxy extends Model { use HasFactory, SoftDeletes; protected $fillable = [ 'ip', 'port', 'username', 'password', 'type', 'status', 'is_active', 'last_check_at', 'last_check_status', 'fail_count', ]; protected $casts = [ 'port' => 'integer', 'is_active' => 'boolean', 'last_check_at' => 'datetime', 'last_check_status' => 'boolean', 'fail_count' => 'integer', 'type' => ProxyType::class, 'status' => ProxyStatus::class, ]; public function getIp(): string { return $this->ip; } public function getPort(): int { return $this->port; } public function getUsername(): ?string { return $this->username; } public function getPassword(): ?string { return $this->password; } public function getType(): ProxyType { return $this->type; } public function getIsActive(): bool { return $this->is_active; } public function getLastCheckAt(): ?DateTime { return $this->last_check_at; } public function getLastCheckStatus(): ?bool { return $this->last_check_status; } public function getFailCount(): int { return $this->fail_count; } public function setIp(string $ip): self { if (!filter_var($ip, FILTER_VALIDATE_IP)) { throw new \InvalidArgumentException('Invalid IP address format'); } $this->ip = $ip; return $this; } public function setPort(int $port): self { if ($port < 1 || $port > 65535) { throw new \InvalidArgumentException('Port number must be between 1 and 65535'); } $this->port = $port; return $this; } public function setUsername(?string $username): self { $this->username = $username; return $this; } public function setPassword(?string $password): self { $this->password = $password; return $this; } public function setType(ProxyType $type): self { $this->type = $type; return $this; } public function setIsActive(bool $isActive): self { $this->is_active = $isActive; return $this; } public function setLastCheckAt(?DateTime $lastCheckAt): self { $this->last_check_at = $lastCheckAt; return $this; } public function setLastCheckStatus(?bool $status): self { $this->last_check_status = $status; return $this; } public function setFailCount(int $count): self { if ($count < 0) { throw new \InvalidArgumentException('Fail count cannot be negative'); } $this->fail_count = $count; return $this; } public function incrementFailCount(): self { $this->fail_count++; return $this; } public function resetFailCount(): self { $this->fail_count = 0; return $this; } /** * Mark proxy as in use */ public function markAsInUse(): self { $this->update(['status' => ProxyStatus::USE]); return $this; } /** * Mark proxy as free */ public function markAsFree(): self { $this->update(['status' => ProxyStatus::FREE]); return $this; } /** * Mark proxy as having error */ public function markAsError(): self { $this->update([ 'status' => ProxyStatus::ERROR, 'fail_count' => $this->fail_count + 1 ]); return $this; } /** * Reset error count */ public function resetErrors(): self { $this->update([ 'fail_count' => 0, 'status' => ProxyStatus::FREE ]); return $this; } /** * Check if proxy is available */ public function isAvailable(): bool { return $this->is_active && $this->status === ProxyStatus::FREE && $this->last_check_status && ($this->fail_count < 3 || $this->fail_count === null); } }