Skip to content

Commit

Permalink
Refactor ActiveRecords, fix caching issues, typos
Browse files Browse the repository at this point in the history
  • Loading branch information
zenn1989 committed Nov 27, 2018
1 parent c57047f commit e9f9463
Show file tree
Hide file tree
Showing 29 changed files with 327 additions and 202 deletions.
33 changes: 12 additions & 21 deletions Apps/ActiveRecord/Blacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class Blacklist extends ActiveModel

/**
* Check if current user have in blacklist target_id user
* @param int $target_id
* @param int $currentId
* @param int $targetId
* @return bool
*/
public static function have($user_id, $target_id)
public static function have($currentId, $targetId): bool
{
$query = self::where('user_id', '=', $user_id)
->where('target_id', '=', $target_id);
return $query->count() > 0;
return self::where('user_id', $currentId)
->where('target_id', $targetId)
->count() > 0;
}

/**
Expand All @@ -43,14 +44,14 @@ public static function have($user_id, $target_id)
* @param int $user2
* @return bool
*/
public static function check($user1, $user2)
public static function check($user1, $user2): bool
{
$query = self::where(function ($query) use ($user1, $user2) {
$query->where('user_id', '=', $user1)
->where('target_id', '=', $user2);
$query->where('user_id', $user1)
->where('target_id', $user2);
})->orWhere(function ($query) use ($user1, $user2) {
$query->where('user_id', '=', $user2)
->where('target_id', '=', $user1);
$query->where('user_id', $user2)
->where('target_id', $user1);
});

return $query->count() < 1;
Expand All @@ -62,16 +63,6 @@ public static function check($user1, $user2)
*/
public function targetUser()
{
return $this->belongsTo('Apps\ActiveRecord\User', 'target_id');
}

/**
* Get target user for current record
* @return bool|\Illuminate\Support\Collection|null|static
* @deprecated
*/
public function getUser()
{
return $this->targetUser();
return $this->belongsTo(User::class, 'target_id');
}
}
24 changes: 2 additions & 22 deletions Apps/ActiveRecord/CommentAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CommentAnswer extends ActiveModel
*/
public function user()
{
return $this->belongsTo('Apps\ActiveRecord\User', 'user_id');
return $this->belongsTo(User::class, 'user_id');
}

/**
Expand All @@ -48,26 +48,6 @@ public function user()
*/
public function post()
{
return $this->belongsTo('Apps\ActiveRecord\CommentPost', 'comment_id');
}

/**
* Get user identity
* @return User|null
* @deprecated
*/
public function getUser()
{
return User::identity($this->user_id);
}

/**
* Get comment post object
* @return CommentPost|null
* @deprecated
*/
public function getCommentPost()
{
return CommentPost::where('id', '=', $this->comment_id)->first();
return $this->belongsTo(CommentPost::class, 'comment_id');
}
}
28 changes: 4 additions & 24 deletions Apps/ActiveRecord/CommentPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CommentPost extends ActiveModel
*/
public function user()
{
return $this->belongsTo('Apps\ActiveRecord\User', 'user_id');
return $this->belongsTo(User::class, 'user_id');
}

/**
Expand All @@ -58,27 +58,7 @@ public function user()
*/
public function answers()
{
return $this->hasMany('Apps\ActiveRecord\CommentAnswer', 'comment_id');
}

/**
* Get user identity
* @return User|null
* @deprecated
*/
public function getUser()
{
return User::identity($this->user_id);
}

/**
* Get comment post->answers relation
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @deprecated
*/
public function getAnswer()
{
return $this->answers();
return $this->hasMany(CommentAnswer::class, 'comment_id');
}

/**
Expand All @@ -92,8 +72,8 @@ public function getAnswerCount()
return MainApp::$Memory->get('commentpost.answer.count.' . $this->id);
}
// get count from db
$count = CommentAnswer::where('comment_id', '=', $this->id)
->where('moderate', '=', 0)
$count = CommentAnswer::where('comment_id', $this->id)
->where('moderate', 0)
->count();
// save in cache
MainApp::$Memory->set('commentpost.answer.count.' . $this->id, $count);
Expand Down
8 changes: 4 additions & 4 deletions Apps/ActiveRecord/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Content extends ActiveModel
*/
public function category()
{
return $this->belongsTo('Apps\ActiveRecord\ContentCategory', 'category_id');
return $this->belongsTo(ContentCategory::class, 'category_id');
}

/**
Expand All @@ -81,7 +81,7 @@ public function category()
*/
public function ratings()
{
return $this->hasMany('Apps\ActiveRecord\ContentRating', 'content_id');
return $this->hasMany(ContentRating::class, 'content_id');
}

/**
Expand All @@ -90,7 +90,7 @@ public function ratings()
*/
public function tags()
{
return $this->hasMany('Apps\ActiveRecord\ContentTag', 'content_id');
return $this->hasMany(ContentTag::class, 'content_id');
}

/**
Expand All @@ -99,7 +99,7 @@ public function tags()
*/
public function user()
{
return $this->belongsTo('Apps\ActiveRecord\User', 'author_id');
return $this->belongsTo(User::class, 'author_id');
}

/**
Expand Down
20 changes: 6 additions & 14 deletions Apps/ActiveRecord/ContentCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ffcms\Core\Helper\Type\Any;
use Ffcms\Core\Helper\Type\Obj;
use Ffcms\Core\Helper\Type\Str;
use Illuminate\Support\Collection;

/**
* Class ContentCategory. Active record model for content category nesting
Expand Down Expand Up @@ -39,7 +40,7 @@ public static function all($columns = ['*'])
{
$cacheName = 'activerecord.contentcategory.all.' . implode('.', $columns);
$records = MemoryObject::instance()->get($cacheName);
if ($records === null) {
if (!$records) {
$records = parent::all($columns);
MemoryObject::instance()->set($cacheName, $records);
}
Expand All @@ -49,15 +50,15 @@ public static function all($columns = ['*'])
/**
* Get record via category path address
* @param string $path
* @return self|object|null
* @return self|ActiveModel|Collection
*/
public static function getByPath($path = '')
{
if (MainApp::$Memory->get('cache.content.category.path.' . $path) !== null) {
return MainApp::$Memory->get('cache.content.category.path.' . $path);
}

$record = self::where('path', '=', $path)->first();
$record = self::where('path', $path)->first();
MainApp::$Memory->set('cache.content.category.path.' . $path, $record);
return $record;
}
Expand All @@ -78,20 +79,11 @@ public static function getById($id)
return $record;
}

/**
* @deprecated
* @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
*/
public static function getAll()
{
return self::all();
}

/**
* Build id-title array of sorted by nesting level categories
* @return array
*/
public static function getSortedCategories()
public static function getSortedCategories(): array
{
$response = [];
$tmpData = self::getSortedAll();
Expand Down Expand Up @@ -119,7 +111,7 @@ public static function getSortedCategories()
* Get all categories sorted by pathway
* @return array
*/
public static function getSortedAll()
public static function getSortedAll(): array
{
$list = self::all();
$response = [];
Expand Down
1 change: 1 addition & 0 deletions Apps/ActiveRecord/FeedbackAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class FeedbackAnswer extends ActiveModel

/**
* Get post relation
* @todo: refactor me to ->belongsTo() on rework feedback system
* @return FeedbackPost|null
*/
public function getFeedbackPost()
Expand Down
14 changes: 2 additions & 12 deletions Apps/ActiveRecord/FeedbackPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FeedbackPost extends ActiveModel
*/
public function answers()
{
return $this->hasMany('Apps\ActiveRecord\FeedbackAnswer', 'feedback_id');
return $this->hasMany(FeedbackAnswer::class, 'feedback_id');
}

/**
Expand All @@ -50,16 +50,6 @@ public function answers()
*/
public function user()
{
return $this->hasOne('Apps\ActiveRecord\User', 'id', 'user_id');
}

/**
* Get all answers for this feedback post id
* @return \Illuminate\Database\Eloquent\Relations\HasMany|null
* @deprecated
*/
public function getAnswers()
{
return $this->answers();
return $this->hasOne(User::class, 'id', 'user_id');
}
}
8 changes: 4 additions & 4 deletions Apps/ActiveRecord/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static function identity($userId = null)
}

$object = $profile->first();

MainApp::$Memory->set('profile.object.cache.' . $userId, $object);

return $object;
}

Expand Down Expand Up @@ -103,10 +103,10 @@ public function getAvatarUrl($type = 'small')
* Get user nickname. If is empty - return 'id+userId'
* @return string
*/
public function getNickname()
public function getNickname(): ?string
{
$userNick = $this->nick;
if ($userNick === null || Str::likeEmpty($userNick)) {
if (!$userNick || Str::likeEmpty($userNick)) {
$userNick = 'id' . $this->id;
}

Expand All @@ -119,6 +119,6 @@ public function getNickname()
*/
public function user()
{
return $this->belongsTo('Apps\ActiveRecord\User', 'user_id');
return $this->belongsTo(User::class, 'user_id');
}
}
13 changes: 2 additions & 11 deletions Apps/ActiveRecord/ProfileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ public static function all($columns = ['*'])
return $records;
}

/**
* @deprecated
* @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
*/
public static function getAll()
{
return self::all();
}

/**
* Get field name locale by field id
* @param int $id
Expand All @@ -62,7 +53,7 @@ public static function getNameById($id)
$all = self::all();

$record = $all->find($id);
if ($record === null || $record === false) {
if (!$record) {
return null;
}

Expand All @@ -79,7 +70,7 @@ public static function getTypeById($id)
$all = self::all();

$record = $all->find($id);
if ($record === null || $record === false) {
if (!$record) {
return null;
}

Expand Down
21 changes: 6 additions & 15 deletions Apps/ActiveRecord/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,26 @@ public static function all($columns = ['*'])

/**
* Get role object via id
* @param int $role_id
* @param int $roleId
* @return object|null
*/
public static function get($role_id)
public static function get($roleId)
{
$role = MainApp::$Memory->get('user.role.cache.' . $role_id);
$role = MainApp::$Memory->get('user.role.cache.' . $roleId);

// not founded in cache
if ($role === null) {
$role = self::find($role_id);
MainApp::$Memory->set('user.role.cache.' . $role_id, $role);
$role = self::find($roleId);
MainApp::$Memory->set('user.role.cache.' . $roleId, $role);
}
return $role;
}

/**
* @deprecated
* @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
*/
public static function getAll()
{
return self::all();
}

/**
* Get all roles as array [id=>name]
* @return null|array
*/
public static function getIdNameAll()
public static function getIdNameAll(): ?array
{
$all = self::all();

Expand Down

0 comments on commit e9f9463

Please sign in to comment.