diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f41280..de4d933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ ### Perf - **Component:** View Become a component to perf load time (660c1f1) - **Tracker:** Reduce Redis Calls for get User and Torrent Info in Tracker (e813435) +- **User:** Sort class User and create UserFactory (8fced36) - **User:** Add `Entity\User\AbstractUserInterface` (1d8e9e4) - **User:** Simple sql to get user real_transfer from table `snatched` (547c772) diff --git a/application/Components/Site.php b/application/Components/Site.php index 80a8965..f1b2efa 100644 --- a/application/Components/Site.php +++ b/application/Components/Site.php @@ -27,11 +27,13 @@ class Site extends Component const LOG_LEVEL_LEADER = 'leader'; protected Entity\User\UserFactory $user_factory; + protected Entity\Torrent\TorrentFactory $torrent_factory; public function __construct($config = []) { parent::__construct($config); $this->user_factory = new Entity\User\UserFactory(); + $this->torrent_factory = new Entity\Torrent\TorrentFactory(); } public function onRequestBefore() @@ -49,6 +51,14 @@ public function getUserFactory(): Entity\User\UserFactory return $this->user_factory; } + /** + * @return Entity\Torrent\TorrentFactory + */ + public function getTorrentFactory(): Entity\Torrent\TorrentFactory + { + return $this->torrent_factory; + } + protected function getCacheNameSpace(): string { return 'Site:hash:runtime_value'; @@ -56,13 +66,7 @@ protected function getCacheNameSpace(): string public function getTorrent($tid) { - if (array_key_exists($tid, $this->torrents)) { - $torrent = $this->torrents[$tid]; - } else { - $torrent = new Entity\Torrent($tid); // TODO Handing if this torrent id does not exist - $this->torrents[$tid] = $torrent; - } - return $torrent; + return $this->torrent_factory->getTorrentById($tid); } /** diff --git a/application/Entity/Torrent/AbstractTorrent.php b/application/Entity/Torrent/AbstractTorrent.php deleted file mode 100644 index f10158b..0000000 --- a/application/Entity/Torrent/AbstractTorrent.php +++ /dev/null @@ -1,28 +0,0 @@ -loadTorrentContentById($id); - if ($this->id == null) { - throw new NotFoundException('Not Found'); // FIXME - } - } - - public function loadTorrentContentById($id) - { - $self = app()->redis->hGetAll(Constant::torrentContent($id)); - if (empty($self)) { - $self = app()->pdo->createCommand("SELECT * FROM `torrents` WHERE id=:id LIMIT 1;")->bindParams([ - "id" => $id - ])->queryOne() ?? []; - app()->redis->hMSet(Constant::torrentContent($id), $self); - app()->redis->expire(Constant::torrentContent($id), 1800); - } - $this->importAttributes($self); + $this->importAttributes($config); } protected function getCacheNameSpace(): string @@ -95,9 +81,9 @@ public function getOwner(): User return app()->site->getUser($this->owner_id); } - public function getInfoHash(): string + public function getInfoHash($hex = true): string { - return bin2hex($this->info_hash); + return $hex ? bin2hex($this->info_hash) : $this->info_hash; } public function getStatus(): string @@ -150,26 +136,11 @@ public function getCategory() return app()->site->CategoryDetail($this->category); } - public function getTorrentName(): string - { - return $this->torrent_name; - } - - public function getTorrentType(): string - { - return $this->torrent_type; - } - public function getTorrentSize(): int { return $this->torrent_size; } - public function getTorrentStructure(): array - { - return json_decode($this->torrent_structure, true); - } - public function getTeamId() { return $this->team; @@ -238,6 +209,21 @@ public function getHr(): bool return (boolean)$this->hr; } + public function getTorrentName(): string + { + return $this->torrent_name; + } + + public function getTorrentType(): string + { + return $this->torrent_type; + } + + public function getTorrentStructure(): array + { + return json_decode($this->torrent_structure, true); + } + public function hasNfo(): bool { return (boolean)$this->nfo; diff --git a/application/Entity/Torrent/TorrentFactory.php b/application/Entity/Torrent/TorrentFactory.php new file mode 100644 index 0000000..4d18926 --- /dev/null +++ b/application/Entity/Torrent/TorrentFactory.php @@ -0,0 +1,43 @@ +pdo->createCommand('SELECT * FROM `torrents` WHERE id=:id LIMIT 1;')->bindParams([ + 'id' => $tid + ])->queryOne(); + + if (false === $self) { + throw new NotFoundException('Not Found'); // FIXME + } + + return new Torrent($self); + } + + public function getTorrentBySearch(array $search_field, int $offset = 0, int $limit = 50): array + { + $fetch = app()->pdo->createCommand([ + ['SELECT `id`, `owner_id`,`info_hash`,`status`,`added_at`,`complete`,`incomplete`,`downloaded`,`comments`,`title`,`subtitle`,`category`,`torrent_size`,`team`,`quality_audio`,`quality_codec`,`quality_medium`,`quality_resolution`,`tags`,`uplver`,`hr` FROM `torrents` WHERE 1=1 '], + ...$search_field, + ['ORDER BY `added_at` DESC '], + ['LIMIT :offset, :rows', 'params' => ['offset' => $offset, 'rows' => $limit]] + ])->queryAll(); + + return array_map(function ($self) { + return new Torrent($self); + }, $fetch); + } +} diff --git a/application/Repository/Torrent/TorrentStatus.php b/application/Entity/Torrent/TorrentStatus.php similarity index 87% rename from application/Repository/Torrent/TorrentStatus.php rename to application/Entity/Torrent/TorrentStatus.php index 6ccadc7..021899e 100644 --- a/application/Repository/Torrent/TorrentStatus.php +++ b/application/Entity/Torrent/TorrentStatus.php @@ -2,13 +2,13 @@ /** * Created by PhpStorm. * User: Rhilip - * Date: 1/11/2020 + * Date: 1/27/2020 * Time: 2020 */ declare(strict_types=1); -namespace App\Repository\Torrent; +namespace App\Entity\Torrent; class TorrentStatus { diff --git a/application/Repository/Torrent/TorrentType.php b/application/Entity/Torrent/TorrentType.php similarity index 85% rename from application/Repository/Torrent/TorrentType.php rename to application/Entity/Torrent/TorrentType.php index 3f50411..247e0c6 100644 --- a/application/Repository/Torrent/TorrentType.php +++ b/application/Entity/Torrent/TorrentType.php @@ -8,7 +8,7 @@ declare(strict_types=1); -namespace App\Repository\Torrent; +namespace App\Entity\Torrent; class TorrentType { diff --git a/application/Entity/User/User.php b/application/Entity/User/User.php index 246af6a..2de04ec 100644 --- a/application/Entity/User/User.php +++ b/application/Entity/User/User.php @@ -66,6 +66,7 @@ protected function getCacheNameSpace(): string return $this->cache_key_extra; } + /** @noinspection PhpMissingParentConstructorInspection */ public function __construct($id = 0) { $this->id = $id; @@ -79,8 +80,7 @@ public function __construct($id = 0) if (false === $self) { return; // It means this user id is invalid } - - parent::__construct($self); + $this->importAttributes($self); } public function getId(): int @@ -201,7 +201,8 @@ public function getTimeRatio() return $this->ratioHelper($this->seedtime, $this->leechtime); } - private function loadExtendProp() { + private function loadExtendProp() + { if (false === $this->extended_info_hit) { if (false === $self = app()->redis->get($this->cache_key_extended)) { $self = app()->pdo->createCommand('SELECT `create_at`,`register_ip`,`last_login_at`,`last_access_at`,`last_upload_at`,`last_download_at`,`last_connect_at`,`last_login_ip`,`last_access_ip`,`last_tracker_ip` FROM `users` WHERE id = :uid')->bindParams([ @@ -372,10 +373,10 @@ public function getTempInvitesSum(): int public function getTempInviteDetails(): array { return $this->getCacheValue('temp_invites_details', function () { - return app()->pdo->createCommand('SELECT * FROM `user_invitations` WHERE `user_id` = :uid AND (`total`-`used`) > 0 AND `expire_at` > NOW() ORDER BY `expire_at` ASC')->bindParams([ + return app()->pdo->createCommand('SELECT * FROM `user_invitations` WHERE `user_id` = :uid AND (`total`-`used`) > 0 AND `expire_at` > NOW() ORDER BY `expire_at` ASC')->bindParams([ "uid" => app()->auth->getCurUser()->getId() ])->queryAll() ?: []; - }) ?? []; + }) ?? []; } public function getPendingInvites() diff --git a/application/Entity/User/UserFactory.php b/application/Entity/User/UserFactory.php index af40238..16c9e60 100644 --- a/application/Entity/User/UserFactory.php +++ b/application/Entity/User/UserFactory.php @@ -10,7 +10,6 @@ namespace App\Entity\User; - class UserFactory { protected array $user_instances = []; diff --git a/application/Models/Form/Torrent/EditForm.php b/application/Models/Form/Torrent/EditForm.php index 65cb432..c9450a5 100644 --- a/application/Models/Form/Torrent/EditForm.php +++ b/application/Models/Form/Torrent/EditForm.php @@ -10,9 +10,9 @@ use App\Libraries\Constant; use App\Models\Form\Traits\isValidTorrentTrait; -use App\Entity\Torrent; +use App\Entity\Torrent\Torrent; -use App\Repository\Torrent\TorrentStatus; +use App\Entity\Torrent\TorrentStatus; use Rid\Http\UploadFile; use Rid\Validators\Validator; diff --git a/application/Models/Form/Torrent/UploadForm.php b/application/Models/Form/Torrent/UploadForm.php index 5332581..fba9cce 100644 --- a/application/Models/Form/Torrent/UploadForm.php +++ b/application/Models/Form/Torrent/UploadForm.php @@ -9,8 +9,8 @@ namespace App\Models\Form\Torrent; use App\Libraries\Constant; -use App\Repository\Torrent\TorrentStatus; -use App\Repository\Torrent\TorrentType; +use App\Entity\Torrent\TorrentStatus; +use App\Entity\Torrent\TorrentType; use Rid\Http\UploadFile; diff --git a/application/Models/Form/Torrents/SearchForm.php b/application/Models/Form/Torrents/SearchForm.php index d11bab3..5ee9064 100644 --- a/application/Models/Form/Torrents/SearchForm.php +++ b/application/Models/Form/Torrents/SearchForm.php @@ -171,15 +171,6 @@ protected function getRemoteTotal(): int protected function getRemoteData(): array { - $fetch = app()->pdo->createCommand(array_merge(array_merge([ - ['SELECT `id`, `added_at` FROM `torrents` WHERE 1=1 '] - ], $this->getSearchField()), [ - ['ORDER BY `added_at` DESC '], - ['LIMIT :offset, :rows', 'params' => ['offset' => $this->offset, 'rows' => $this->limit]] - ]))->queryColumn(); - - return array_map(function ($id) { - return app()->site->getTorrent($id); - }, $fetch); + return app()->site->getTorrentFactory()->getTorrentBySearch($this->getSearchField(), $this->offset, $this->limit); } } diff --git a/application/Models/Form/Traits/isValidTorrentTrait.php b/application/Models/Form/Traits/isValidTorrentTrait.php index c33ae7c..064dada 100644 --- a/application/Models/Form/Traits/isValidTorrentTrait.php +++ b/application/Models/Form/Traits/isValidTorrentTrait.php @@ -8,7 +8,7 @@ namespace App\Models\Form\Traits; -use App\Entity\Torrent; +use App\Entity\Torrent\Torrent; trait isValidTorrentTrait { diff --git a/templates/torrent/edit.php b/templates/torrent/edit.php index a292f07..bb16a20 100644 --- a/templates/torrent/edit.php +++ b/templates/torrent/edit.php @@ -11,7 +11,7 @@ $torrent = $edit->getTorrent(); -use App\Repository\Torrent\TorrentStatus; +use App\Entity\Torrent\TorrentStatus; ?>