Completed
Push — feature/player-list ( 8e5225...c69c4b )
by Vladimir
05:41
created

MessageQueryBuilder::forPlayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1.064

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 3
cts 5
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1.064
1
<?php
2
/**
3
 * This file contains a class to quickly generate database queries for message
4
 *
5
 * @package    BZiON\Models\QueryBuilder
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * This class can be used to search for messages with specific characteristics
11
 * in the database.
12
 *
13
 * @package    BZiON\Models\QueryBuilder
14
 */
15
class MessageQueryBuilder extends QueryBuilder
16
{
17
    /**
18
     * Only include messages
19
     *
20
     * @return self
21
     */
22 1
    public function messagesOnly()
23
    {
24 1
        $this->whereConditions[] = 'event_type IS NULL';
25
26 1
        return $this;
27
    }
28
29
    /**
30
     * Only include conversation events
31
     *
32
     * @return self
33
     */
34
    public function eventsOnly()
35
    {
36
        $this->whereConditions[] = 'event_type IS NOT NULL';
37
38
        return $this;
39
    }
40
41
    /**
42
     * Only return messages that are sent from/to a specific player
43
     *
44
     * @param  Player $player The player related to the messages
45
     * @return self
46
     */
47 1 View Code Duplication
    public function forPlayer($player)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
48
    {
49 1
        $this->extras .= '
50
            LEFT JOIN `conversations` ON conversations.id = messages.conversation_to
51
            LEFT JOIN `player_conversations` ON player_conversations.conversation=conversations.id
52
        ';
53
54 1
        $this->column('player_conversations.player')->is($player);
0 ignored issues
show
Bug introduced by
The call to column() misses a required argument $mode.

This check looks for function calls that miss required arguments.

Loading history...
55
        $this->column('conversations.status')->isOneOf(Conversation::getActiveStatuses());
0 ignored issues
show
Bug introduced by
The call to column() misses a required argument $mode.

This check looks for function calls that miss required arguments.

Loading history...
56
57
        return $this;
58
    }
59
60
    /*
61
    * Locate messages that contain keywords in a search string
62
    *
63
    * @param  string $query The search query
64
    * @return self
65
    */
66 1
    public function search($query)
67
    {
68 1
        $keywords = preg_split('/\s+/', trim($query));
69
70 1
        $query = "";
71
72 1
        $first = true;
73 1
        foreach ($keywords as $keyword) {
74 1
            if (!$first) {
75
                $query .= ' AND ';
76
            } else {
77 1
                $first = false;
78
            }
79
80 1
            $query .= "(message LIKE CONCAT('%', ?, '%'))";
81 1
            $this->parameters[] = $keyword;
82
        }
83
84 1
        $this->whereConditions[] = $query;
85
86 1
        return $this;
87
    }
88
}
89