ReplyRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 94
rs 10
c 1
b 0
f 0
wmc 6
lcom 2
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A model() 0 4 1
A boot() 0 4 1
A byTopicId() 0 6 1
A store() 0 21 2
A byUserId() 0 6 1
1
<?php
2
3
namespace PHPHub\Repositories\Eloquent;
4
5
use Auth;
6
use PHPHub\Events\NewReply;
7
use PHPHub\Repositories\Criteria\ReplyCriteria;
8
use PHPHub\Repositories\Eloquent\Traits\IncludeUserTrait;
9
use PHPHub\Repositories\ReplyRepositoryInterface;
10
use PHPHub\Reply;
11
use Prettus\Validator\Contracts\ValidatorInterface;
12
13
/**
14
 * Class ReplyRepositoryEloquent.
15
 */
16
class ReplyRepository extends BaseRepository implements ReplyRepositoryInterface
17
{
18
    use IncludeUserTrait;
19
20
    /**
21
     * Specify Validator Rules.
22
     *
23
     * @var array
24
     */
25
    protected $rules = [
26
        ValidatorInterface::RULE_CREATE => [
27
            'body'     => 'required|min:2',
28
            'topic_id' => 'required|integer',
29
        ],
30
        ValidatorInterface::RULE_UPDATE => [
31
32
        ],
33
    ];
34
35
    /**
36
     * 发布新的评论.
37
     *
38
     * @param array $attributes
39
     *
40
     * @return Reply
41
     */
42
    public function store(array $attributes)
43
    {
44
        if (! is_null($this->validator)) {
45
            $this->validator->with($attributes)
46
                ->passesOrFail(ValidatorInterface::RULE_CREATE);
47
        }
48
49
        $reply = new Reply($attributes);
50
51
        $reply->user_id = Auth::id();
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<PHPHub\Reply>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52
        $reply->body = app('markdown')->text($attributes['body']);
0 ignored issues
show
Documentation introduced by
The property body does not exist on object<PHPHub\Reply>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
        $reply->body_original = $attributes['body'];
0 ignored issues
show
Bug introduced by
The property body_original does not seem to exist. Did you mean original?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
55
        $reply->save();
56
57
        $reply->topic()->getQuery()->increment('reply_count');
58
59
        event(new NewReply($reply, Auth::id(), $reply->topic()->getQuery()->pluck('user_id')));
0 ignored issues
show
Deprecated Code introduced by
The method Illuminate\Database\Eloquent\Builder::pluck() has been deprecated with message: since version 5.1.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60
61
        return $reply;
62
    }
63
64
    /**
65
     * Specify Model class name.
66
     *
67
     * @return string
68
     */
69
    public function model()
70
    {
71
        return Reply::class;
72
    }
73
74
    /**
75
     * Boot up the repository, pushing criteria.
76
     */
77
    public function boot()
78
    {
79
        $this->pushCriteria(app(ReplyCriteria::class));
80
    }
81
82
    /**
83
     * 通过 TopicId 过滤.
84
     *
85
     * @param $topic_id
86
     *
87
     * @return $this
88
     */
89
    public function byTopicId($topic_id)
90
    {
91
        $this->model = $this->model->where('topic_id', $topic_id);
92
93
        return $this;
94
    }
95
96
    /**
97
     * 通过 UserId 过滤.
98
     *
99
     * @param $user_id
100
     *
101
     * @return $this
102
     */
103
    public function byUserId($user_id)
104
    {
105
        $this->model = $this->model->where('user_id', $user_id);
106
107
        return $this;
108
    }
109
}
110