Completed
Push — master ( eedf5c...8f238b )
by Konstantinos
08:47
created

MessageSearch::elasticSearch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
ccs 0
cts 13
cp 0
rs 9.0856
c 1
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
crap 6
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
    public function __construct(\MessageQueryBuilder $queryBuilder, \Player $player = null)
36
    {
37
        $this->queryBuilder = $queryBuilder;
38
        $this->player = $player;
39 1
    }
40
41 1
    /**
42 1
     * Perform a search on messages and get the results
43 1
     *
44
     * @param  string    $query The query string
45
     * @return Message[] The results of the search
46
     */
47
    public function search($query)
48
    {
49
        Debug::startStopwatch('search.messages');
50
51 1
        $results = $this->mysqlSearch($query);
52
53 1
        Debug::finishStopwatch('search.messages');
54
55 1
        return $results;
56
    }
57
58 1
    /**
59
     * Perform a search on messages using the data stored in the MySQL database
60
     *
61 1
     * @param  string    $query The query string
0 ignored issues
show
Bug introduced by
There is no parameter named $query. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     * @return Message[] The results of the search
63 1
     */
64
    private function mysqlSearch($search)
65
    {
66
        return $this->queryBuilder
67
            ->search($search)
68
            ->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