Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

CommentController::save()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 25
nc 4
nop 3
dl 0
loc 45
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Controllers;
4
5
use Coyote\Http\Requests\CommentRequest;
6
use Coyote\Http\Resources\CommentResource;
7
use Coyote\Comment;
8
use Coyote\Notifications\Job\CommentedNotification;
9
use Coyote\Notifications\Job\RepliedNotification;
10
use Coyote\Services\Stream\Actor as Stream_Actor;
11
use Illuminate\Contracts\Notifications\Dispatcher;
12
use Coyote\Services\Stream\Activities\Create as Stream_Create;
13
use Coyote\Services\Stream\Activities\Update as Stream_Update;
14
use Coyote\Services\Stream\Activities\Delete as Stream_Delete;
15
use Coyote\Services\Stream\Objects\Comment as Stream_Comment;
16
17
class CommentController extends Controller
18
{
19
    /**
20
     * @param CommentRequest $request
21
     * @param Dispatcher $dispatcher
22
     * @param Comment|null $comment
23
     * @return CommentResource
24
     * @throws \Illuminate\Auth\Access\AuthorizationException
25
     */
26
    public function save(CommentRequest $request, Dispatcher $dispatcher, Comment $comment = null)
27
    {
28
        $this->authorize('update', $comment);
29
30
        $comment->fill($request->all())->creating(function (Comment $model) use ($request) {
0 ignored issues
show
Bug introduced by
The method fill() 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

30
        $comment->/** @scrutinizer ignore-call */ 
31
                  fill($request->all())->creating(function (Comment $model) use ($request) {

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...
31
            $model->user_id = $this->userId;
32
            $model->forceFill($request->only(['resource_id', 'resource_type']));
33
        });
34
35
        if ($comment->parent_id) {
36
            $comment->forceFill($comment->parent->only(['resource_id', 'resource_type']));
37
        }
38
39
        $actor = new Stream_Actor($this->auth);
40
41
        $this->transaction(function () use ($comment, $dispatcher, $actor) {
0 ignored issues
show
Unused Code introduced by
The import $dispatcher is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
42
            $comment->save();
43
            $target = $this->target($comment);
0 ignored issues
show
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\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

43
            $target = $this->target(/** @scrutinizer ignore-type */ $comment);
Loading history...
44
45
            stream(
46
                $comment->wasRecentlyCreated ? new Stream_Create($actor) : new Stream_Update($actor),
47
                (new Stream_Comment())->comment($comment),
0 ignored issues
show
Bug introduced by
It seems like $comment can also be of type null; however, parameter $comment of Coyote\Services\Stream\Objects\Comment::comment() does only seem to accept Coyote\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

47
                (new Stream_Comment())->comment(/** @scrutinizer ignore-type */ $comment),
Loading history...
48
                (new $target)->map($comment->resource)
49
            );
50
        });
51
52
        if ($comment->wasRecentlyCreated) {
53
            $subscribers = $comment
54
                ->resource
55
                ->subscribers()
56
                ->with('user')
57
                ->get()
58
                ->pluck('user') // get all subscribers
59
                ->exceptUser($this->auth); // exclude current logged user
60
61
            $dispatcher->send($subscribers, new CommentedNotification($comment));
0 ignored issues
show
Bug introduced by
It seems like $comment can also be of type null; however, parameter $comment of Coyote\Notifications\Job...fication::__construct() does only seem to accept Coyote\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

61
            $dispatcher->send($subscribers, new CommentedNotification(/** @scrutinizer ignore-type */ $comment));
Loading history...
62
63
//            if ($comment->parent_id && $comment->user_id !== $comment->parent->user_id) {
64
//                $comment->parent->notify(new RepliedNotification($comment));
65
//            }
66
        }
67
68
        CommentResource::withoutWrapping();
69
70
        return new CommentResource($comment->load(['user', 'children']));
71
    }
72
73
    public function delete(Comment $comment)
74
    {
75
        $this->authorize('delete', $comment);
76
77
        $this->transaction(function () use ($comment) {
78
            $comment->children->each(function ($child) {
79
                $child->delete();
80
            });
81
82
            $comment->delete();
83
            $target = $this->target($comment);
84
85
            stream(
86
                Stream_Delete::class,
87
                (new Stream_Comment())->comment($comment),
88
                (new $target)->map($comment->resource)
89
            );
90
        });
91
    }
92
93
    private function target(Comment $comment): string
94
    {
95
        return 'Coyote\\Services\\Stream\\Objects\\' . class_basename($comment->resource_type);
96
    }
97
}
98