Passed
Push — master ( 649a90...a29da8 )
by Adam
10:01
created

SubmitController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 123
rs 10
c 1
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A preview() 0 3 1
A delete() 0 19 2
A save() 0 45 4
A getParser() 0 3 1
A toggleSponsored() 0 6 1
1
<?php
2
3
namespace Coyote\Http\Controllers\Microblog;
4
5
use Coyote\Events\MicroblogWasDeleted;
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\Criteria\WithTrashed;
13
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository;
14
use Coyote\Services\Stream\Activities\Create as Stream_Create;
15
use Coyote\Services\Stream\Activities\Update as Stream_Update;
16
use Coyote\Services\Stream\Activities\Delete as Stream_Delete;
17
use Coyote\Services\Stream\Objects\Microblog as Stream_Microblog;
18
use Illuminate\Http\Request;
19
20
/**
21
 * Class SubmitController
22
 * @package Coyote\Http\Controllers\Microblog
23
 */
24
class SubmitController extends Controller
25
{
26
    /**
27
     * @var UserRepository
28
     */
29
    private $user;
30
31
    /**
32
     * @param UserRepository $user
33
     */
34
    public function __construct(UserRepository $user)
35
    {
36
        parent::__construct();
37
38
        $this->user = $user;
39
    }
40
41
    /**
42
     * @param MicroblogRequest $request
43
     * @param $microblog
44
     * @return MicroblogResource
45
     * @throws \Illuminate\Auth\Access\AuthorizationException
46
     */
47
    public function save(MicroblogRequest $request, ?Microblog $microblog)
48
    {
49
        $this->user->pushCriteria(new WithTrashed());
50
51
        if (!$microblog->exists) {
52
            $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

52
            $microblog->/** @scrutinizer ignore-call */ 
53
                        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...
53
        } else {
54
            $this->authorize('update', $microblog);
55
        }
56
57
        $microblog->fill($request->only(['text']));
58
59
        $this->transaction(function () use ($microblog, $request) {
60
            $microblog->save();
61
            $microblog->assets()->sync($request->input('assets'));
62
63
            $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

63
            $object = (new Stream_Microblog())->map(/** @scrutinizer ignore-type */ $microblog);
Loading history...
64
65
            if ($microblog->wasRecentlyCreated) {
66
                // increase reputation points
67
                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

67
                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...
68
69
                // put this to activity stream
70
                stream(Stream_Create::class, $object);
71
72
                if ($this->auth->allow_subscribe) {
73
                    // enable subscribe button
74
                    $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...
75
                    $microblog->subscribers()->create(['user_id' => $this->auth->id]);
76
                }
77
            } else {
78
                stream(Stream_Update::class, $object);
79
            }
80
81
            $microblog->setTags(array_pluck($request->input('tags', []), 'name'));
82
        });
83
84
        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

84
        broadcast(new MicroblogSaved(/** @scrutinizer ignore-type */ $microblog))->toOthers();
Loading history...
85
86
        MicroblogResource::withoutWrapping();
87
88
        $microblog->unsetRelation('assets');
89
        $microblog->load(['assets', 'tags']);
90
91
        return new MicroblogResource($microblog);
92
    }
93
94
    /**
95
     * Usuniecie wpisu z mikrobloga
96
     *
97
     * @param \Coyote\Microblog $microblog
98
     */
99
    public function delete($microblog)
100
    {
101
        abort_if(!$microblog->exists, 404);
102
        $this->authorize('delete', $microblog);
103
104
        $this->transaction(function () use ($microblog) {
105
            $microblog->delete();
106
            // cofniecie pkt reputacji
107
            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

107
            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...
108
109
            // put this to activity stream
110
            stream(Stream_Delete::class, (new Stream_Microblog())->map($microblog));
111
        });
112
113
        if ($this->userId !== $microblog->user_id) {
114
            $microblog->user->notify(new DeletedNotification($microblog, $this->auth));
115
        }
116
117
        event(new MicroblogWasDeleted($microblog));
118
    }
119
120
    /**
121
     * @param Request $request
122
     * @return \Symfony\Component\HttpFoundation\Response
123
     */
124
    public function preview(Request $request)
125
    {
126
        return response($this->getParser()->parse((string) $request->get('text')));
127
    }
128
129
    /**
130
     * @param Microblog $microblog
131
     * @throws \Illuminate\Auth\Access\AuthorizationException
132
     */
133
    public function toggleSponsored(Microblog $microblog)
134
    {
135
        $this->authorize('moderate', $microblog);
136
137
        $microblog->is_sponsored = !$microblog->is_sponsored;
138
        $microblog->save();
139
    }
140
141
    /**
142
     * @return \Coyote\Services\Parser\Factories\PmFactory
143
     */
144
    private function getParser()
145
    {
146
        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...
147
    }
148
}
149