Completed
Push — master ( c75724...87d358 )
by Razon
03:04
created

Article::getHasLiked()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
namespace App\Model;
3
4
use App\Behavior\ArticleBehavior;
5
use App\Behavior\CreatorBehavior;
6
use App\Behavior\TimestampBehavior;
7
use App\Validator\UrlValidator;
8
use Yii;
9
use yii\behaviors\OptimisticLockBehavior;
10
11
/**
12
 * This is the model class for table "{{%article}}".
13
 *
14
 * @property int $id
15
 * @property int $creator Creator ID
16
 * @property string $title Title
17
 * @property string $summary Description
18
 * @property string $author Author
19
 * @property string $cover Cover
20
 * @property int $release_time Release Time
21
 * @property int $status Status: 0.Inactive 1.Active
22
 * @property int $is_deleted Is Deleted
23
 * @property int $create_time Create Time
24
 * @property int $update_time Update Time
25
 *
26
 * @property ArticleMeta $meta
27
 * @property ArticleCategory $category
28
 * @property ArticleLike $like
29
 * @property ArticleLike[] $likes
30
 * @property int $likesCount
31
 * @property bool $hasLiked
32
 */
33
class Article extends ActiveRecord implements SoftDeleteInterface, StatusInterface
34
{
35
    use SoftDeleteTrait, StatusTrait;
36
37
    public $content;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public static function tableName()
43
    {
44
        return '{{%article}}';
45
    }
46
47
    public function behaviors()
48
    {
49
        return [
50
            TimestampBehavior::class,
51
            OptimisticLockBehavior::class,
52
            CreatorBehavior::class,
53
            ArticleBehavior::class,
54
        ];
55
    }
56
    
57
    public function optimisticLock()
58
    {
59
        return 'version';
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function rules()
66
    {
67
        return [
68
            [['author', 'cover'], 'default', 'value' => ''],
69
            [['title', 'content', 'summary', 'status', 'release_time', 'category_id'], 'required'],
70
            [['release_time', 'status', 'is_deleted', 'create_time', 'update_time', 'category_id'], 'integer'],
71
            [['content'], 'string'],
72
            [['title', 'author'], 'string', 'max' => 255],
73
            [['cover'], UrlValidator::class],
74
            ['category_id', 'exist', 'targetClass' => ArticleCategory::class, 'targetAttribute' => 'id'],
75
        ];
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function attributeLabels()
82
    {
83
        return [
84
            'id' => Yii::t('app', 'ID'),
85
            'creator' => Yii::t('app', 'Creator'),
86
            'cover' => Yii::t('app', 'Cover'),
87
            'title' => Yii::t('app', 'Title'),
88
            'summary' => Yii::t('app', 'Summary'),
89
            'content' => Yii::t('app', 'Content'),
90
            'author' => Yii::t('app', 'Author'),
91
            'release_time' => Yii::t('app', 'Release Time'),
92
            'status' => Yii::t('app', 'Status: 0.Inactive 1.Active'),
93
            'is_deleted' => Yii::t('app', 'Is Deleted'),
94
            'create_time' => Yii::t('app', 'Create Time'),
95
            'update_time' => Yii::t('app', 'Update Time'),
96
        ];
97
    }
98
99
    public function getMeta()
100
    {
101
        return $this->hasOne(ArticleMeta::class, ['article_id' => 'id']);
102
    }
103
104
    public function getCategory()
105
    {
106
        return $this->hasOne(ArticleCategory::class, ['id' => 'category_id']);
107
    }
108
109
    public function getLike()
110
    {
111
        return $this->hasOne(ArticleLike::class, ['article_id' => 'id'])
112
            ->andWhere(['user_id' => (int) Yii::$app->getUser()->getId()]);
113
    }
114
115
    public function getHasLiked(): bool
116
    {
117
        return $this->like ? true : false;
118
    }
119
120
    public function getLikes()
121
    {
122
        return $this->hasMany(ArticleLike::class, ['article_id' => 'id']);
123
    }
124
125
    public function getLikesCount(): int
126
    {
127
        return $this->getLikes()->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLikes()->count() could return the type string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
128
    }
129
}
130