Completed
Push — master ( c7e879...390e33 )
by Fèvre
02:26
created

DiscussPostRepository::create()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 30
Code Lines 17

Duplication

Lines 6
Ratio 20 %

Importance

Changes 0
Metric Value
dl 6
loc 30
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 17
nop 1
1
<?php
2
namespace Xetaravel\Models\Repositories;
3
4
use Illuminate\Support\Facades\Auth;
5
use Xetaravel\Events\Discuss\ConversationWasLockedEvent;
6
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent;
7
use Xetaravel\Models\DiscussPost;
8
use Xetaravel\Models\DiscussConversation;
9
10
class DiscussPostRepository
11
{
12
    /**
13
     * Create a new post instance after a valid validation.
14
     *
15
     * @param array $data The data used to create the post.
16
     *
17
     * @return \Xetaravel\Models\DiscussPost
18
     */
19
    public static function create(array $data): DiscussPost
20
    {
21
        $post = DiscussPost::create([
22
            'conversation_id' => $data['conversation_id'],
23
            'content' => $data['content']
24
        ]);
25
26
        if (Auth::user()->hasPermission('manage.discuss.conversations')) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method hasPermission() does only exist in the following implementations of said interface: Xetaravel\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
27
            $conversation = DiscussConversation::find($data['conversation_id']);
28
29
            $data['is_pinned'] = isset($data['is_pinned']) ? true : false;
30
            $data['is_locked'] = isset($data['is_locked']) ? true : false;
31
32 View Code Duplication
            if ($conversation->is_pinned != $data['is_pinned'] && $data['is_pinned'] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
                event(new ConversationWasPinnedEvent($conversation));
34
            }
35
36 View Code Duplication
            if ($conversation->is_locked != $data['is_locked'] && $data['is_locked'] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
                event(new ConversationWasLockedEvent($conversation));
38
            }
39
40
            $conversation->is_locked = $data['is_locked'];
41
            $conversation->is_pinned = $data['is_pinned'];
42
            $conversation->last_post_id = $post->getKey();
43
44
            $conversation->save();
45
        }
46
47
        return $post;
48
    }
49
50
    /**
51
     * Find the previous post related to the given post.
52
     *
53
     * @param \Xetaravel\Models\DiscussPost $post
54
     *
55
     * @return \Xetaravel\Models\DiscussPost|null
56
     */
57
    public static function findPreviousPost(DiscussPost $post)
58
    {
59
        return DiscussPost::where('id', '!=', $post->conversation->solved_post_id)
60
                ->where('conversation_id', $post->conversation->getKey())
61
                ->where('created_at', '<', $post->created_at)
62
                ->orderBy('created_at', 'desc')
63
                ->first();
64
    }
65
}
66