Passed
Push — master ( 146a05...11c4cf )
by Adam
09:00
created

CommentController::target()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Controllers\Forum;
4
5
use Coyote\Events\CommentDeleted;
6
use Coyote\Events\CommentSaved;
7
use Coyote\Events\PostSaved;
8
use Coyote\Events\TopicWasSaved;
9
use Coyote\Http\Requests\Forum\PostCommentRequest;
10
use Coyote\Http\Resources\PostCommentResource;
11
use Coyote\Http\Resources\PostResource;
12
use Coyote\Post;
13
use Coyote\Repositories\Contracts\TopicRepositoryInterface;
14
use Coyote\Http\Controllers\Controller;
15
use Coyote\Services\Forum\Tracker;
16
use Coyote\Services\Stream\Activities\Create as Stream_Create;
17
use Coyote\Services\Stream\Activities\Update as Stream_Update;
18
use Coyote\Services\Stream\Activities\Delete as Stream_Delete;
19
use Coyote\Services\Stream\Activities\Move as Stream_Move;
20
use Coyote\Services\Stream\Objects\Comment as Stream_Comment;
21
use Coyote\Services\Stream\Objects\Topic as Stream_Topic;
22
use Coyote\Stream;
23
use Illuminate\Contracts\Notifications\Dispatcher;
24
25
class CommentController extends Controller
26
{
27
    /**
28
     * @param PostCommentRequest $request
29
     * @param Dispatcher $dispatcher
30
     * @param Post\Comment|null $comment
31
     * @return PostCommentResource
32
     * @throws \Illuminate\Auth\Access\AuthorizationException
33
     */
34
    public function save(PostCommentRequest $request, Dispatcher $dispatcher, ?Post\Comment $comment)
0 ignored issues
show
Unused Code introduced by
The parameter $dispatcher is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public function save(PostCommentRequest $request, /** @scrutinizer ignore-unused */ Dispatcher $dispatcher, ?Post\Comment $comment)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        if (!$comment->exists) {
37
            $comment->user()->associate($this->auth);
0 ignored issues
show
Bug introduced by
The method user() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $comment->/** @scrutinizer ignore-call */ 
38
                      user()->associate($this->auth);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
            $comment->post_id = $request->input('post_id');
39
        } else {
40
            $this->authorize('update', [$comment, $comment->post->forum]);
41
        }
42
43
        // Maybe user does not have an access to this category?
44
        $this->authorize('access', [$comment->post->forum]);
45
        // Only moderators can post comment if topic (or forum) was locked
46
        $this->authorize('write', [$comment]);
47
48
        $comment->fill($request->only(['text']));
49
50
        $this->transaction(function () use ($comment) {
51
            $comment->save();
52
53
            stream($comment->wasRecentlyCreated ? Stream_Create::class : Stream_Update::class, ...$this->target($comment));
0 ignored issues
show
Bug introduced by
$this->target($comment) is expanded, but the parameter $object of stream() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            stream($comment->wasRecentlyCreated ? Stream_Create::class : Stream_Update::class, /** @scrutinizer ignore-type */ ...$this->target($comment));
Loading history...
Bug introduced by
It seems like $comment can also be of type null; however, parameter $comment of Coyote\Http\Controllers\...entController::target() does only seem to accept Coyote\Post\Comment, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            stream($comment->wasRecentlyCreated ? Stream_Create::class : Stream_Update::class, ...$this->target(/** @scrutinizer ignore-type */ $comment));
Loading history...
54
55
            if ($comment->wasRecentlyCreated) {
56
                // subscribe post. notify about all future comments to this post
57
                $comment->post->subscribe($this->userId, true);
58
            }
59
        });
60
61
        $comment->setRelation('forum', $comment->post->forum);
62
63
        broadcast(new CommentSaved($comment))->toOthers();
0 ignored issues
show
Bug introduced by
It seems like $comment can also be of type null; however, parameter $comment of Coyote\Events\CommentSaved::__construct() does only seem to accept Coyote\Post\Comment, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        broadcast(new CommentSaved(/** @scrutinizer ignore-type */ $comment))->toOthers();
Loading history...
64
65
        PostCommentResource::withoutWrapping();
66
67
        return (new PostCommentResource($comment))->additional(['is_subscribed' => $comment->post->subscribers()->forUser($this->userId)->exists()]);
68
    }
69
70
    /**
71
     * @param Post\Comment $comment
72
     * @throws \Illuminate\Auth\Access\AuthorizationException
73
     */
74
    public function delete(Post\Comment $comment)
75
    {
76
        abort_if(!$comment->post, 404);
77
78
        $this->authorize('delete', [$comment, $comment->post->forum]);
79
80
        $this->transaction(function () use ($comment) {
81
            $comment->delete();
82
83
            stream(Stream_Delete::class, ...$this->target($comment));
0 ignored issues
show
Bug introduced by
$this->target($comment) is expanded, but the parameter $object of stream() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
            stream(Stream_Delete::class, /** @scrutinizer ignore-type */ ...$this->target($comment));
Loading history...
84
        });
85
86
        event(new CommentDeleted($comment));
87
    }
88
89
    /**
90
     * @param Post $post
91
     * @return mixed
92
     * @throws \Illuminate\Auth\Access\AuthorizationException
93
     */
94
    public function show(Post $post)
95
    {
96
        $this->authorize('access', [$post->forum]);
97
98
        PostCommentResource::withoutWrapping();
99
100
        $post->load('comments.user');
101
102
        $post->comments->each(function (Post\Comment $comment) use ($post) {
103
            $comment->setRelation('forum', $post->forum);
104
        });
105
106
        return PostCommentResource::collection($post->comments)->keyBy('id');
107
    }
108
109
    public function migrate(Post\Comment $comment, TopicRepositoryInterface $repository)
110
    {
111
        $topic = $comment->post->topic;
112
113
        // Maybe user does not have an access to this category?
114
        $this->authorize('access', [$comment->post->forum]);
115
        // Only moderators can post comment if topic (or forum) was locked
116
        $this->authorize('write', [$comment]);
117
        $this->authorize('delete', [$comment, $comment->post->forum]);
118
119
        /** @var Post $post */
120
        $post = $this->transaction(function () use ($topic, $comment, $repository) {
121
            $stream = Stream::where('object->objectType', 'comment')->where('object->id', $comment->id)->first();
122
123
            $post = $topic->posts()->forceCreate(
124
                array_merge(
125
                    ['forum_id' => $topic->forum_id, 'topic_id' => $topic->id, 'ip' => $stream->ip, 'browser' => $stream->browser],
0 ignored issues
show
Bug introduced by
The property ip does not seem to exist on Coyote\Stream. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property browser does not seem to exist on Coyote\Stream. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
126
                    $comment->only(['created_at', 'text', 'user_id'])
127
                )
128
            );
129
130
            $comment->delete();
131
            $repository->adjustReadDate($topic->id, $comment->created_at->subSecond());
132
133
            stream(Stream_Move::class, ...$this->target($comment));
0 ignored issues
show
Bug introduced by
$this->target($comment) is expanded, but the parameter $object of stream() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
            stream(Stream_Move::class, /** @scrutinizer ignore-type */ ...$this->target($comment));
Loading history...
134
135
            return $post;
136
        });
137
138
        $post->load('assets');
139
        $tracker = Tracker::make($topic);
140
141
        // fire the event. it can be used to index a content and/or add page path to "pages" table
142
        event(new TopicWasSaved($topic));
143
        // add post to elasticsearch
144
        broadcast(new PostSaved($post))->toOthers();
145
146
        PostResource::withoutWrapping();
147
148
        return (new PostResource($post))->setTracker($tracker)->resolve($this->request);
149
    }
150
151
    private function target(Post\Comment $comment): array
152
    {
153
        $target = (new Stream_Topic())->map($comment->post->topic);
154
155
        // it is IMPORTANT to parse text first, and then put information to activity stream.
156
        // so that we will save plan text (without markdown)
157
        $object = (new Stream_Comment())->map($comment->post, $comment, $comment->post->topic);
158
159
        return [$object, $target];
160
    }
161
}
162