Passed
Push — 5.0.0 ( 9d1037...fbd7bb )
by Fèvre
05:16
created

DiscussPostRepository::create()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
nc 17
nop 1
dl 0
loc 30
rs 8.4444
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A DiscussPostRepository::findPreviousPost() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Models\Repositories;
6
7
use Illuminate\Support\Facades\Auth;
8
use Xetaravel\Events\Discuss\ConversationWasLockedEvent;
9
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent;
10
use Xetaravel\Models\DiscussPost;
11
use Xetaravel\Models\DiscussConversation;
12
13
class DiscussPostRepository
14
{
15
    /**
16
     * Find the previous post related to the given post.
17
     *
18
     * @param DiscussPost $post
19
     * @param bool $withSolved
20
     *
21
     * @return DiscussPost|null
22
     */
23
    public static function findPreviousPost(DiscussPost $post, bool $withSolved = false): ?DiscussPost
24
    {
25
        $previousPost = DiscussPost::where('id', '!=', $post->getKey())
26
                ->where('conversation_id', $post->conversation->getKey())
27
                ->where('created_at', '<=', $post->created_at);
28
29
        if (!$withSolved) {
30
            $previousPost = $previousPost->where('id', '!=', $post->conversation->solved_post_id);
31
        }
32
33
        return $previousPost->orderBy('created_at', 'desc')
34
                ->first();
35
    }
36
}
37