Passed
Branch master (249862)
by Adam
07:51
created

SubmitController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 191
rs 10
c 0
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 11 1
A edit() 0 5 1
A delete() 0 12 1
A paste() 0 15 1
A thumbnail() 0 3 1
C save() 0 80 11
A __construct() 0 5 1
1
<?php
2
3
namespace Coyote\Http\Controllers\Microblog;
4
5
use Coyote\Events\MicroblogWasDeleted;
6
use Coyote\Events\MicroblogWasSaved;
7
use Coyote\Http\Controllers\Controller;
8
use Coyote\Http\Factories\MediaFactory;
9
use Coyote\Services\Parser\Helpers\Login as LoginHelper;
10
use Coyote\Services\Parser\Helpers\Hash as HashHelper;
11
use Coyote\Repositories\Contracts\UserRepositoryInterface as User;
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\Microblog as Stream_Microblog;
16
use Coyote\Services\UrlBuilder\UrlBuilder;
17
use Illuminate\Http\Request;
18
19
/**
20
 * Class SubmitController
21
 * @package Coyote\Http\Controllers\Microblog
22
 */
23
class SubmitController extends Controller
24
{
25
    use MediaFactory;
26
27
    /**
28
     * @var User
29
     */
30
    private $user;
31
32
    /**
33
     * @param User $user
34
     */
35
    public function __construct(User $user)
36
    {
37
        parent::__construct();
38
39
        $this->user = $user;
40
    }
41
42
    /**
43
     * Publikowanie wpisu na mikroblogu
44
     *
45
     * @param Request $request
46
     * @param \Coyote\Microblog $microblog
47
     * @return \Illuminate\View\View
48
     */
49
    public function save(Request $request, $microblog)
50
    {
51
        $this->validate($request, [
52
            'text'          => 'required|string|max:10000|throttle:' . $microblog->id
53
        ]);
54
55
        $data = $request->only(['text']);
56
57
        if (!$microblog->exists) {
58
            $user = $this->auth;
59
            $data['user_id'] = $user->id;
60
        } else {
61
            $this->authorize('update', $microblog);
62
63
            $user = $this->user->find($microblog->user_id, ['id', 'name', 'is_blocked', 'is_active', 'photo']);
64
        }
65
66
        if ($request->has('thumbnail') || count($microblog->media) > 0) {
67
            /** @var \Coyote\Services\Media\MediaInterface $media */
68
            foreach ($microblog->media as $media) {
69
                if (!in_array($media->getFilename(), $request->get('thumbnail', []))) {
70
                    $media->delete();
71
                }
72
            }
73
74
            $microblog->media = $request->get('thumbnail');
75
        }
76
77
        $microblog->fill($data);
78
79
        $this->transaction(function () use (&$microblog, $user) {
80
            $microblog->save();
81
            $object = (new Stream_Microblog())->map($microblog);
82
83
            if ($microblog->wasRecentlyCreated) {
84
                // increase reputation points
85
                app('reputation.microblog.create')->map($microblog)->save();
86
87
                // put this to activity stream
88
                stream(Stream_Create::class, $object);
89
90
                $helper = new LoginHelper();
91
                // get id of users that were mentioned in the text
92
                $usersId = $helper->grab($microblog->html);
93
94
                if (!empty($usersId)) {
95
                    app('notification.microblog.login')->with([
96
                        'users_id'    => $usersId,
97
                        'sender_id'   => $user->id,
98
                        'sender_name' => $user->name,
99
                        'subject'     => excerpt($microblog->html),
100
                        'text'        => $microblog->html,
101
                        'url'         => UrlBuilder::microblog($microblog)
102
                    ])->notify();
103
                }
104
105
                if ($this->auth->allow_subscribe) {
106
                    // enable subscribe button
107
                    $microblog->subscribe_on = true;
108
                    $microblog->subscribers()->create(['user_id' => $user->id]);
109
                }
110
            } else {
111
                stream(Stream_Update::class, $object);
112
            }
113
114
            $helper = new HashHelper();
115
            $microblog->setTags($helper->grab($microblog->html));
116
117
            event(new MicroblogWasSaved($microblog));
118
        });
119
120
        // do przekazania do widoku...
121
        foreach (['name', 'is_blocked', 'is_active', 'photo'] as $key) {
122
            $microblog->{$key} = $user->{$key};
123
        }
124
125
        // passing html version of the entry...
126
        $microblog->text = $microblog->html;
127
128
        return view(!$microblog->wasRecentlyCreated ? 'microblog.partials.text' : 'microblog.partials.microblog')->with('microblog', $microblog);
129
    }
130
131
    /**
132
     * Edycja wpisu na mikroblogu. Odeslanie formularza zawierajacego tresc + zalaczniki
133
     *
134
     * @param \Coyote\Microblog $microblog
135
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
136
     */
137
    public function edit($microblog)
138
    {
139
        $this->authorize('update', $microblog);
140
141
        return view('microblog.partials.edit')->with('microblog', $microblog);
142
    }
143
144
    /**
145
     * Return small piece of code (thumbnail container)
146
     *
147
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
148
     */
149
    public function thumbnail()
150
    {
151
        return view('microblog.partials.thumbnail');
152
    }
153
154
    /**
155
     * Usuniecie wpisu z mikrobloga
156
     *
157
     * @param \Coyote\Microblog $microblog
158
     */
159
    public function delete($microblog)
160
    {
161
        $this->authorize('delete', $microblog);
162
163
        $this->transaction(function () use ($microblog) {
164
            $microblog->delete();
165
            // cofniecie pkt reputacji
166
            app('reputation.microblog.create')->undo($microblog->id);
167
168
            event(new MicroblogWasDeleted($microblog));
169
            // put this to activity stream
170
            stream(Stream_Delete::class, (new Stream_Microblog())->map($microblog));
171
        });
172
    }
173
174
    /**
175
     * Upload pliku na serwer wraz z wczesniejsza walidacja
176
     *
177
     * @param Request $request
178
     * @return \Illuminate\Http\JsonResponse
179
     */
180
    public function upload(Request $request)
181
    {
182
        $this->validate($request, [
183
            'photo'             => 'required|mimes:jpeg,jpg,png,gif|max:' . (config('filesystems.upload_max_size') * 1024)
184
        ]);
185
186
        $media = $this->getMediaFactory()->make('attachment')->upload($request->file('photo'));
187
188
        return response()->json([
189
            'url' => (string) $media->url(),
190
            'name' => $media->getFilename()
191
        ]);
192
    }
193
194
    /**
195
     * Paste image from clipboard
196
     *
197
     * @return \Illuminate\Http\JsonResponse
198
     */
199
    public function paste()
200
    {
201
        $input = file_get_contents("php://input");
202
203
        $validator = $this->getValidationFactory()->make(
204
            ['length' => strlen($input)],
205
            ['length' => 'max:' . config('filesystems.upload_max_size') * 1024 * 1024]
206
        );
207
208
        $this->validateWith($validator);
209
        $media = $this->getMediaFactory()->make('attachment')->put(file_get_contents('data://' . substr($input, 7)));
210
211
        return response()->json([
212
            'name' => $media->getFilename(),
213
            'url' => (string) $media->url()
214
        ]);
215
    }
216
}
217