Passed
Push — master ( c8af94...5b2262 )
by Adam
12:06
created

SubmitController::restore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Controllers\Microblog;
4
5
use Coyote\Events\MicroblogDeleted;
6
use Coyote\Events\MicroblogSaved;
7
use Coyote\Http\Controllers\Controller;
8
use Coyote\Http\Requests\MicroblogRequest;
9
use Coyote\Http\Resources\MicroblogResource;
10
use Coyote\Microblog;
11
use Coyote\Notifications\Microblog\DeletedNotification;
12
use Coyote\Repositories\Contracts\MicroblogRepositoryInterface;
13
use Coyote\Repositories\Criteria\WithTrashed;
14
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository;
15
use Coyote\Services\Stream\Activities\Create as Stream_Create;
16
use Coyote\Services\Stream\Activities\Update as Stream_Update;
17
use Coyote\Services\Stream\Activities\Delete as Stream_Delete;
18
use Coyote\Services\Stream\Activities\Restore as Stream_Restore;
19
use Coyote\Services\Stream\Objects\Microblog as Stream_Microblog;
20
use Illuminate\Http\Request;
21
22
/**
23
 * Class SubmitController
24
 * @package Coyote\Http\Controllers\Microblog
25
 */
26
class SubmitController extends Controller
27
{
28
    /**
29
     * @var UserRepository
30
     */
31
    private $user;
32
33
    /**
34
     * @param UserRepository $user
35
     */
36
    public function __construct(UserRepository $user)
37
    {
38
        parent::__construct();
39
40
        $this->user = $user;
41
    }
42
43
    /**
44
     * @param MicroblogRequest $request
45
     * @param $microblog
46
     * @return MicroblogResource
47
     * @throws \Illuminate\Auth\Access\AuthorizationException
48
     */
49
    public function save(MicroblogRequest $request, ?Microblog $microblog)
50
    {
51
        $this->user->pushCriteria(new WithTrashed());
52
53
        if (!$microblog->exists) {
54
            $microblog->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

54
            $microblog->/** @scrutinizer ignore-call */ 
55
                        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...
55
        } else {
56
            $this->authorize('update', $microblog);
57
        }
58
59
        $microblog->fill($request->only(['text']));
60
61
        $this->transaction(function () use ($microblog, $request) {
62
            $microblog->save();
63
            $microblog->assets()->sync($request->input('assets'));
64
65
            $object = (new Stream_Microblog())->map($microblog);
0 ignored issues
show
Bug introduced by
It seems like $microblog can also be of type null; however, parameter $microblog of Coyote\Services\Stream\Objects\Microblog::map() does only seem to accept Coyote\Microblog, 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

65
            $object = (new Stream_Microblog())->map(/** @scrutinizer ignore-type */ $microblog);
Loading history...
66
67
            if ($microblog->wasRecentlyCreated) {
68
                // increase reputation points
69
                app('reputation.microblog.create')->map($microblog)->save();
0 ignored issues
show
Bug introduced by
The method map() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

69
                app('reputation.microblog.create')->/** @scrutinizer ignore-call */ map($microblog)->save();

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...
70
71
                // put this to activity stream
72
                stream(Stream_Create::class, $object);
73
74
                if ($this->auth->allow_subscribe) {
75
                    // enable subscribe button
76
                    $microblog->is_subscribed = true;
0 ignored issues
show
Bug introduced by
The property is_subscribed does not seem to exist on Coyote\Microblog. 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...
77
                    $microblog->subscribers()->create(['user_id' => $this->auth->id]);
78
                }
79
            } else {
80
                stream(Stream_Update::class, $object);
81
            }
82
83
            $microblog->setTags(array_pluck($request->input('tags', []), 'name'));
84
        });
85
86
        broadcast(new MicroblogSaved($microblog))->toOthers();
0 ignored issues
show
Bug introduced by
It seems like $microblog can also be of type null; however, parameter $microblog of Coyote\Events\MicroblogSaved::__construct() does only seem to accept Coyote\Microblog, 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

86
        broadcast(new MicroblogSaved(/** @scrutinizer ignore-type */ $microblog))->toOthers();
Loading history...
87
88
        MicroblogResource::withoutWrapping();
89
90
        $microblog->unsetRelation('assets');
91
        $microblog->load(['assets', 'tags']);
92
93
        return new MicroblogResource($microblog);
94
    }
95
96
    /**
97
     * Usuniecie wpisu z mikrobloga
98
     *
99
     * @param \Coyote\Microblog $microblog
100
     */
101
    public function delete($microblog)
102
    {
103
        abort_if(!$microblog->exists, 404);
104
        $this->authorize('delete', $microblog);
105
106
        $this->transaction(function () use ($microblog) {
107
            $microblog->delete();
108
            // cofniecie pkt reputacji
109
            app('reputation.microblog.create')->undo($microblog->id);
0 ignored issues
show
Bug introduced by
The method undo() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

109
            app('reputation.microblog.create')->/** @scrutinizer ignore-call */ undo($microblog->id);

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...
110
111
            // put this to activity stream
112
            stream(Stream_Delete::class, (new Stream_Microblog())->map($microblog));
113
        });
114
115
        if ($this->userId !== $microblog->user_id) {
116
            $microblog->user->notify(new DeletedNotification($microblog, $this->auth));
117
        }
118
119
        event(new MicroblogDeleted($microblog));
120
    }
121
122
    public function restore(int $id, MicroblogRepositoryInterface $repository)
0 ignored issues
show
Unused Code introduced by
The parameter $repository 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

122
    public function restore(int $id, /** @scrutinizer ignore-unused */ MicroblogRepositoryInterface $repository)

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...
123
    {
124
        $microblog = Microblog::withTrashed()->findOrFail($id);
125
126
        $this->authorize('delete', $microblog);
127
128
        $this->transaction(function () use ($microblog) {
129
            $microblog->restore();
130
131
            // put this to activity stream
132
            stream(Stream_Restore::class, (new Stream_Microblog())->map($microblog));
133
        });
134
135
        event(new MicroblogSaved($microblog));
136
    }
137
138
    /**
139
     * @param Request $request
140
     * @return \Symfony\Component\HttpFoundation\Response
141
     */
142
    public function preview(Request $request)
143
    {
144
        return response($this->getParser()->parse((string) $request->get('text')));
145
    }
146
147
    /**
148
     * @param Microblog $microblog
149
     * @throws \Illuminate\Auth\Access\AuthorizationException
150
     */
151
    public function toggleSponsored(Microblog $microblog)
152
    {
153
        $this->authorize('moderate', $microblog);
154
155
        $microblog->is_sponsored = !$microblog->is_sponsored;
156
        $microblog->save();
157
    }
158
159
    /**
160
     * @return \Coyote\Services\Parser\Factories\PmFactory
161
     */
162
    private function getParser()
163
    {
164
        return app('parser.microblog');
0 ignored issues
show
Bug Best Practice introduced by
The expression return app('parser.microblog') returns the type Coyote\Services\Parser\Factories\MicroblogFactory which is incompatible with the documented return type Coyote\Services\Parser\Factories\PmFactory.
Loading history...
165
    }
166
}
167