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

LogController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B rollback() 0 46 4
B log() 0 26 2
1
<?php
2
3
namespace Coyote\Http\Controllers\Forum;
4
5
use Coyote\Repositories\Contracts\ForumRepositoryInterface as Forum;
6
use Coyote\Repositories\Contracts\Post\LogRepositoryInterface as LogRepository;
7
use Coyote\Repositories\Contracts\PostRepositoryInterface as Post;
8
use Coyote\Repositories\Contracts\TagRepositoryInterface;
9
use Coyote\Repositories\Contracts\TopicRepositoryInterface as Topic;
10
use Coyote\Services\Stream\Objects\Topic as Stream_Topic;
11
use Coyote\Services\Stream\Objects\Post as Stream_Post;
12
use Coyote\Services\Stream\Activities\Rollback as Stream_Rollback;
13
use Coyote\Post\Log;
14
use Coyote\Services\UrlBuilder\UrlBuilder;
15
16
class LogController extends BaseController
17
{
18
    /**
19
     * @var LogRepository
20
     */
21
    private $log;
22
23
    /**
24
     * LogController constructor.
25
     * @param Forum $forum
26
     * @param Topic $topic
27
     * @param Post $post
28
     * @param LogRepository $log
29
     */
30
    public function __construct(Forum $forum, Topic $topic, Post $post, LogRepository $log)
31
    {
32
        parent::__construct($forum, $topic, $post);
33
34
        $this->log = $log;
35
    }
36
37
    /**
38
     * Show post history
39
     *
40
     * @param \Coyote\Post $post
41
     * @return mixed
42
     */
43
    public function log($post)
44
    {
45
        $topic = $post->topic;
46
        $forum = $topic->forum;
47
48
        $this->authorize('update', $forum);
49
50
        $this->breadcrumb($forum);
51
        $this->breadcrumb->push([
52
            $topic->subject => route('forum.topic', [$forum->slug, $topic->id, $topic->slug]),
53
            'Historia postu' => route('forum.post.log', [$post->id])
54
        ]);
55
56
        $logs = $this->log->takeForPost($post->id);
57
58
        $raw = $logs->pluck('text')->toJson();
59
60
        /** @var \Coyote\Services\Parser\Factories\AbstractFactory $parser */
61
        $parser = app('parser.post');
62
        $parser->cache->setEnable(false);
63
64
        foreach ($logs as &$log) {
65
            $log->text = $parser->parse($log->text);
66
        }
67
68
        return $this->view('forum.log')->with(compact('logs', 'post', 'forum', 'topic', 'raw'));
69
    }
70
71
    /**
72
     * Rollback post to $logId version
73
     *
74
     * @param \Coyote\Post $post
75
     * @param int $logId
76
     * @return \Illuminate\Http\RedirectResponse
77
     */
78
    public function rollback($post, $logId)
79
    {
80
        $this->authorize('update', $post->forum);
81
82
        $topic = $post->topic;
83
        $log = $this->log->findWhere(['id' => $logId, 'post_id' => $post->id])->first();
84
85
        $post->fill(['text' => $log->text, 'edit_count' => $post->edit_count + 1, 'editor_id' => $this->userId]);
0 ignored issues
show
Bug introduced by
The property text does not seem to exist on Coyote\Country. 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...
86
87
        $this->transaction(function () use ($post, $log, $topic) {
88
            $post->save();
89
90
            if ($post->id === $topic->first_post_id) {
91
                // w starej wersji nie logowalismy zmian w temacie watku
92
                if ($log->subject) {
0 ignored issues
show
Bug introduced by
The property subject does not seem to exist on Coyote\Country. 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...
93
                    $topic->fill(['subject' => $log->subject]);
94
                }
95
96
                if ($log->tags) {
0 ignored issues
show
Bug introduced by
The property tags does not seem to exist on Coyote\Country. 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...
97
                    // assign tags to topic
98
                    $topic->tags()->sync(app(TagRepositoryInterface::class)->multiInsert($log->tags));
99
                }
100
101
                $topic->save();
102
            }
103
104
            $log = (new Log())->fill($log->toArray());
105
            $log->user_id = $this->userId;
106
            $log->ip = $this->request->ip();
107
            $log->browser = $this->request->browser();
108
            $log->host = $this->request->getClientHost();
109
110
            $log->save();
111
112
            $url = UrlBuilder::post($post);
113
114
            stream(
115
                Stream_Rollback::class,
116
                (new Stream_Post(['url' => $url]))->map($post),
117
                (new Stream_Topic())->map($topic)
118
            );
119
        });
120
121
        return redirect()
122
            ->to(UrlBuilder::topic($topic))
123
            ->with('success', 'Post został przywrócony.');
124
    }
125
}
126