Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(Torrent): Add TorrentFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Feb 2, 2020
1 parent d786243 commit e7dc26e
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 144 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 11 additions & 7 deletions application/Components/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -49,20 +51,22 @@ 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';
}

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);
}

/**
Expand Down
28 changes: 0 additions & 28 deletions application/Entity/Torrent/AbstractTorrent.php

This file was deleted.

17 changes: 0 additions & 17 deletions application/Entity/Torrent/AbstractTorrentInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,59 @@
* Time: 10:10
*/

namespace App\Entity;
namespace App\Entity\Torrent;

use App\Entity\Torrent\AbstractTorrent;
use App\Entity\User\User;
use App\Libraries\Constant;

use Rid\Utils;
use Rid\Exceptions\NotFoundException;
use Rid\Base\BaseObject;

class Torrent extends AbstractTorrent
class Torrent extends BaseObject
{
use Utils\ClassValueCacheUtils;

protected $id = null;

protected $owner_id;
protected $info_hash;

protected $status;

protected $added_at;

protected $complete;
protected $incomplete;
protected $downloaded;
protected $comments;
//-- Torrent Base Info --//
protected int $id;
protected int $owner_id;
protected string $info_hash;
protected string $status = TorrentStatus::PENDING;
protected string $added_at;

protected int $complete = 0;
protected int $incomplete = 0;
protected int $downloaded = 0;
protected int $comments = 0;

protected string $title = '';
protected string $subtitle = '';
protected int $category = 0;
protected string $descr = '';
protected bool $uplver = false;
protected bool $hr = false;
protected $tags;

protected $title;
protected $subtitle;
protected $category;
protected $descr;
protected $uplver;
protected $hr;
protected int $team = 0;
protected int $quality_audio = 0;
protected int $quality_codec = 0;
protected int $quality_medium = 0;
protected int $quality_resolution = 0;

protected $nfo;
protected int $torrent_size;

protected $tags;
protected $pinned_tags;
//-- Torrent Extend Info --//
protected string $torrent_name;
protected string $torrent_type;
protected ?string $nfo;
protected ?string $torrent_structure;

protected $torrent_name;
protected $torrent_type;
protected $torrent_size;
protected $torrent_structure;

protected $team;

protected $comment_perpage = 10; // FIXME

public function __construct($id = null)
/** @noinspection PhpMissingParentConstructorInspection */
public function __construct($config)
{
$this->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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions application/Entity/Torrent/TorrentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 1/27/2020
* Time: 2020
*/

declare(strict_types=1);

namespace App\Entity\Torrent;

use Rid\Exceptions\NotFoundException;

class TorrentFactory
{
public function getTorrentById($tid): Torrent
{
$self = app()->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

declare(strict_types=1);

namespace App\Repository\Torrent;
namespace App\Entity\Torrent;

class TorrentType
{
Expand Down
11 changes: 6 additions & 5 deletions application/Entity/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected function getCacheNameSpace(): string
return $this->cache_key_extra;
}

/** @noinspection PhpMissingParentConstructorInspection */
public function __construct($id = 0)
{
$this->id = $id;
Expand All @@ -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
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion application/Entity/User/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace App\Entity\User;


class UserFactory
{
protected array $user_instances = [];
Expand Down
4 changes: 2 additions & 2 deletions application/Models/Form/Torrent/EditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit e7dc26e

Please sign in to comment.