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

SubmitController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
B save() 0 43 6
A preview() 0 6 1
A create() 0 13 2
A getForm() 0 4 1
A index() 0 13 3
1
<?php
2
3
namespace Coyote\Http\Controllers\Wiki;
4
5
use Coyote\Events\WikiWasSaved;
6
use Coyote\Http\Forms\Wiki\SubmitForm;
7
use Coyote\Services\Stream\Objects\Wiki as Stream_Wiki;
8
use Coyote\Services\Stream\Activities\Create as Stream_Create;
9
use Coyote\Services\Stream\Activities\Update as Stream_Update;
10
use Coyote\Services\UrlBuilder\UrlBuilder;
11
use Illuminate\Http\Request;
12
13
class SubmitController extends BaseController
14
{
15
    /**
16
     * @param \Coyote\Wiki $wiki
17
     * @param Request $request
18
     * @return \Illuminate\View\View
19
     */
20
    public function index($wiki, Request $request)
21
    {
22
        $form = $this->getForm($wiki);
23
        $this->request->attributes->set('upload_url', route('wiki.upload'));
24
25
        if (!$wiki->exists) {
26
            $form->get('parent_id')->setValue($request->input('parentId'));
27
        }
28
29
        $this->breadcrumb->push($wiki->title, url($wiki->path));
30
        $this->breadcrumb->push($wiki->exists ? 'Edycja strony' : 'Dodaj nową stronę');
31
32
        return $this->view('wiki.submit')->with(compact('form', 'wiki'));
33
    }
34
35
    /**
36
     * @param string $path
37
     * @return \Illuminate\View\View
38
     */
39
    public function create($path)
40
    {
41
        $form = $this->getForm($this->wiki->newInstance());
0 ignored issues
show
Bug introduced by
The method newInstance() does not exist on Coyote\Repositories\Cont...WikiRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Coyote\Repositories\Cont...WikiRepositoryInterface. ( Ignorable by Annotation )

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

41
        $form = $this->getForm($this->wiki->/** @scrutinizer ignore-call */ newInstance());
Loading history...
42
        $segments = explode('/', trim($path, '/'));
43
44
        $form->get('title')->setValue(str_replace('_', ' ', array_pop($segments)));
45
        $parent = $this->wiki->findByPath(implode('/', $segments));
46
47
        if ($parent) {
48
            $form->get('parent_id')->setValue($parent->id);
49
        }
50
51
        return $this->view('wiki.submit')->with(compact('form', 'wiki'));
52
    }
53
54
    /**
55
     * @param \Coyote\Wiki $wiki
56
     * @return \Illuminate\Http\RedirectResponse
57
     */
58
    public function save($wiki)
59
    {
60
        $form = $this->getForm($wiki);
61
        $form->validate();
62
63
        $request = $form->getRequest();
64
65
        $path = $this->transaction(function () use ($wiki, $request) {
66
            $subscribe = auth()->user()->allow_subscribe && !$wiki->wasUserInvolved($this->userId);
67
            $this->wiki->save($wiki, $request);
68
69
            $subscribersId = $wiki->subscribers()->pluck('user_id')->toArray();
70
71
            app('notification.wiki.subscriber')
72
                ->with([
73
                    'subject' => $wiki->title,
74
                    'users_id' => $subscribersId,
75
                    'url' => UrlBuilder::wiki($wiki),
76
                    'sender_id' => $this->userId,
77
                    'sender_name' => auth()->user()->name,
78
                    'excerpt' => excerpt($wiki->excerpt)
79
                ])
80
                ->notify();
81
82
            // we DO NOT want to add another row into the table. we MUST check whether user is already
83
            // on subscribers list or not.
84
            if ($subscribe && !in_array($this->userId, $subscribersId)) {
85
                $wiki->subscribers()->create(['user_id' => $this->userId]);
86
            }
87
88
            app('reputation.wiki.' . ($wiki->wasRecentlyCreated ? 'create' : 'update'))->map($wiki)->save();
89
90
            stream(
91
                $wiki->wasRecentlyCreated ? Stream_Create::class : Stream_Update::class,
92
                (new Stream_Wiki())->map($wiki)
93
            );
94
            // add to elasticsearch index and pages table...
95
            event(new WikiWasSaved($wiki));
96
97
            return $wiki->path;
98
        });
99
100
        return redirect()->to($path)->with('success', 'Zmiany zostały zapisane.');
101
    }
102
103
    /**
104
     * @param Request $request
105
     * @return \Symfony\Component\HttpFoundation\Response
106
     */
107
    public function preview(Request $request)
108
    {
109
        $parser = $this->getParser();
110
        $parser->cache->setEnable(false);
111
112
        return response($parser->parse((string) $request->input('text')));
0 ignored issues
show
Bug Best Practice introduced by
The expression return response($parser-...equest->input('text'))) also could return the type Illuminate\Contracts\Routing\ResponseFactory which is incompatible with the documented return type Symfony\Component\HttpFoundation\Response.
Loading history...
113
    }
114
115
    /**
116
     * @param \Coyote\Wiki $wiki
117
     * @return \Coyote\Services\FormBuilder\Form
118
     */
119
    protected function getForm($wiki)
120
    {
121
        return $this->createForm(SubmitForm::class, $wiki, [
122
            'url' => route('wiki.submit', [$wiki->id])
123
        ]);
124
    }
125
}
126