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

MessageSearch::mysqlSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1.008
1
<?php
2
/**
3
 * This file contains a class that performs message searches
4
 *
5
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
6
 */
7
8
namespace BZIon\Search;
9
10
use BZIon\Debug\Debug;
11
12
/**
13
 * Performs a search on messages
14
 */
15
class MessageSearch
16
{
17
    /**
18
     * The MySQL query builder for messages
19
     * @var \MessageQueryBuilder
20
     */
21
    private $queryBuilder;
22
23
    /**
24
     * The user to whom the returned messages will be visible
25
     * @var \Player|null
26
     */
27
    private $player;
28
29
    /**
30
     * Create a new message search
31
     *
32
     * @param \MessageQueryBuilder $queryBuilder The MySQL query builder for messages
33
     * @param \Player|null         $player       The player to make the search for
34
     */
35 1
    public function __construct(\MessageQueryBuilder $queryBuilder, \Player $player = null)
36
    {
37 1
        $this->queryBuilder = $queryBuilder;
38 1
        $this->player = $player;
39 1
    }
40
41
    /**
42
     * Perform a search on messages and get the results
43
     *
44
     * @param  string     $query The query string
45
     * @return \Message[] The results of the search
46
     */
47 1
    public function search($query)
48
    {
49 1
        Debug::startStopwatch('search.messages');
50
51 1
        $results = $this->mysqlSearch($query);
52
53
        Debug::finishStopwatch('search.messages');
54
55
        return $results;
56
    }
57
58
    /**
59
     * Perform a search on messages using the data stored in the MySQL database
60
     *
61
     * @param  string     $query The query string
62
     * @return \Message[] The results of the search
63
     */
64 1
    private function mysqlSearch($query)
65
    {
66 1
        return $this->queryBuilder
67 1
            ->search($query)
68 1
            ->forPlayer($this->player)
0 ignored issues
show
Bug introduced by
It seems like $this->player can be null; however, forPlayer() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69
            ->getModels();
70
    }
71
}
72