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

MoveController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A move() 0 55 2
A __construct() 0 12 1
A index() 0 15 2
1
<?php
2
3
namespace Coyote\Http\Controllers\Job;
4
5
use Coyote\Events\JobDeleting;
6
use Coyote\Events\PostWasSaved;
7
use Coyote\Events\TopicWasSaved;
8
use Coyote\Http\Controllers\Controller;
9
use Coyote\Repositories\Contracts\ForumRepositoryInterface as ForumRepository;
10
use Coyote\Repositories\Contracts\PostRepositoryInterface as PostRepository;
11
use Coyote\Repositories\Contracts\StreamRepositoryInterface as StreamRepository;
12
use Coyote\Repositories\Contracts\TopicRepositoryInterface as TopicRepository;
13
use Coyote\Services\Forum\TreeBuilder;
14
use Coyote\Services\UrlBuilder\UrlBuilder;
15
use Illuminate\Http\Request;
16
use Coyote\Job;
17
use Coyote\Services\Stream\Activities\Move as Stream_Move;
18
use Coyote\Services\Stream\Objects\Job as Stream_Job;
19
use Coyote\Services\Stream\Objects\Forum as Stream_Forum;
20
21
class MoveController extends Controller
22
{
23
    /**
24
     * @var ForumRepository
25
     */
26
    private $forum;
27
28
    /**
29
     * @var TopicRepository
30
     */
31
    private $topic;
32
33
    /**
34
     * @var PostRepository
35
     */
36
    private $post;
37
38
    /**
39
     * @var StreamRepository
40
     */
41
    private $stream;
42
43
    /**
44
     * @param ForumRepository $forum
45
     * @param TopicRepository $topic
46
     * @param PostRepository $post
47
     * @param StreamRepository $stream
48
     */
49
    public function __construct(
50
        ForumRepository $forum,
51
        TopicRepository $topic,
52
        PostRepository $post,
53
        StreamRepository $stream
54
    ) {
55
        parent::__construct();
56
57
        $this->forum = $forum;
58
        $this->topic = $topic;
59
        $this->post = $post;
60
        $this->stream = $stream;
61
    }
62
63
    /**
64
     * @param Job $job
65
     * @return \Illuminate\View\View
66
     */
67
    public function index($job)
68
    {
69
        $this->breadcrumb->push([
70
            'Praca' => route('job.home'),
71
            $job->title => route('job.offer', [$job->id, $job->slug]),
72
            'Przenieś ofertę pracy' => ''
73
        ]);
74
75
        $treeBuilder = new TreeBuilder();
76
77
        return $this->view('job.move')->with([
78
            'forumList'         => $treeBuilder->listById($this->forum->list()),
79
            'preferred'         => $this->forum->findBy('name', 'Ogłoszenia drobne', ['id']),
80
            'job'               => $job,
81
            'subscribed'        => $this->userId ? $job->subscribers()->forUser($this->userId)->exists() : false
82
        ]);
83
    }
84
85
    /**
86
     * @param Job $job
87
     * @param Request $request
88
     * @return \Illuminate\Http\RedirectResponse
89
     */
90
    public function move($job, Request $request)
91
    {
92
        $this->validate($request, [
93
            'forum_id' => 'required|integer|exists:forums,id'
94
        ]);
95
96
        $forum = $this->forum->find($request->input('forum_id'));
97
        /** @var \Coyote\Topic $topic */
98
        $topic = $this->topic->newInstance();
0 ignored issues
show
Bug introduced by
The method newInstance() does not exist on Coyote\Repositories\Cont...opicRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Coyote\Repositories\Cont...opicRepositoryInterface. ( Ignorable by Annotation )

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

98
        /** @scrutinizer ignore-call */ 
99
        $topic = $this->topic->newInstance();
Loading history...
99
        /** @var \Coyote\Post $post */
100
        $post = $this->post->newInstance();
0 ignored issues
show
Bug introduced by
The method newInstance() does not exist on Coyote\Repositories\Cont...PostRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Coyote\Repositories\Cont...PostRepositoryInterface. ( Ignorable by Annotation )

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

100
        /** @scrutinizer ignore-call */ 
101
        $post = $this->post->newInstance();
Loading history...
101
102
        $topic->fill(['subject' => $job->title]);
103
        $topic->forum()->associate($forum);
104
105
        $log = $this->stream->findWhere(['object.objectType' => 'job', 'object.id' => $job->id, 'verb' => 'create'])->first();
106
107
        $post->forceFill([
108
            'user_id'   => $job->user_id,
109
            'text'      => $job->description,
110
            'ip'        => $log->ip,
0 ignored issues
show
Bug introduced by
The property ip 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...
111
            'browser'   => $log->browser,
0 ignored issues
show
Bug introduced by
The property browser 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...
112
            'host'      => gethostbyaddr($log->ip)
113
        ]);
114
115
        $this->transaction(function () use ($job, $forum, $topic, $post) {
116
            $topic->save();
117
118
            $post->forum()->associate($forum);
119
            $post->topic()->associate($topic);
120
121
            $post->save();
122
123
            if ($job->user_id !== $job->user->id) {
124
                $post->subscribers()->create(['user_id' => $job->user_id]);
125
            }
126
127
            $log = new \Coyote\Post\Log();
128
            $log->fillWithPost($post)->fill(['subject' => $topic->subject]);
129
130
            event(new JobDeleting($job));
131
132
            $job->delete();
133
134
            // fire the event. it can be used to index a content and/or add page path to "pages" table
135
            event(new TopicWasSaved($topic));
136
            // add post to elasticsearch
137
            event(new PostWasSaved($post));
138
139
            stream(Stream_Move::class, (new Stream_Job())->map($job), (new Stream_Forum())->map($forum));
140
        });
141
142
        return redirect()
143
            ->to(UrlBuilder::post($post))
144
            ->with('success', 'Ogłoszenie zostało przeniesione.');
145
    }
146
}
147