DiscussPostRepository   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 22
rs 10
c 1
b 0
f 0
wmc 2

1 Method

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