ForumDatabaseSearch::getResults()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.6588
c 0
b 0
f 0
cc 5
eloc 30
nc 8
nop 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Basic Forum Database Search. For a better search try the {@link ForumSphinxSearch}
5
 *
6
 * @package forum
7
 */
8
9
class ForumDatabaseSearch implements ForumSearchProvider
10
{
11
    
12
    /**
13
     * Get the results from the database
14
     *
15
     * @param Int $forumHolderID ForumHolderID to limit it too
16
     * @param String $query need to make real escape for data safe
17
     * @param String $order
18
     * @param Int Offset
19
     * @param Int Limit
20
     *
21
     * @return DataSet Results of matching posts or empty DataSet if no results.
22
     */
23
    public function getResults($forumHolderID, $query, $order = null, $offset = 0, $limit = 10)
24
    {
25
26
        //sanitise the query string to avoid XSS (Using the ORM will also help avoid this too).
27
        $query = Convert::raw2sql(trim($query));
28
29
        //sanitise the order/sorting as it can be changed by the user in quesry string.
30
        $order = Convert::raw2sql(trim($order));
31
32
        //explode the query into the multiple terms to search, supply as an array to pass into the ORM filter.
33
        $terms = explode(' ', $query);
34
        //Add the original full query as one of the keywords.
35
        $terms[] = $query;
36
37
        //Get  posts (limitation is that it picks up the whole phase rather than a FULLTEXT SEARCH.
38
        //We are aiming to keep this as simple as possible). More complex impementations acheived with Solr.
39
        //Rquires the post be moderated, then Checks for any match of Author name or Content partial match.
40
        //Author name checks the full query whereas Content checks each term for matches.
41
        $posts = Post::get()
42
            ->filter(array(
43
                'Status' => 'Moderated', //posts my be moderated/visible.
44
                'Forum.ParentID' => $forumHolderID //posts must be from a particular forum section.
45
                ))
46
            ->filterAny(array(
47
                'Author.Nickname:PartialMatch:nocase' => $query,
48
                'Author.FirstName:PartialMatch:nocase' => $query,
49
                'Author.Surname:PartialMatch:nocase' => $query,
50
                'Content:PartialMatch:nocase' => $terms
51
            ))
52
            ->leftJoin('ForumThread', 'Post.ThreadID = ForumThread.ID');
53
54
        // Work out what sorting method
55
        switch ($order) {
56
            case 'newest':
57
                $posts = $posts->sort('Created', 'DESC');
58
                break;
59
            case 'oldest':
60
                break;
61
            case 'title':
62
                $posts = $posts->sort(array('Thread.Title'=>'ASC'));
63
                break;
64
            default:
65
                $posts = $posts->sort(array(
66
                    'Thread.Title'=>'ASC',
67
                    'Created' => 'DESC'
68
                ));
69
                break;
70
        }
71
        
72
        return $posts ? $posts: new DataList();
0 ignored issues
show
Bug introduced by
The call to DataList::__construct() misses a required argument $dataClass.

This check looks for function calls that miss required arguments.

Loading history...
73
    }
74
    
75
    /**
76
     * Callback when this Provider is loaded. For dealing with background processes
77
     */
78
    public function load()
79
    {
80
        return true;
81
    }
82
}
83