GoogleCustomSearchExt   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getGoogleSiteSearchForm() 0 43 5
A GoogleCustomSearchPage() 0 4 1
1
<?php
2
/*
3
 * @author nicolaas [at] sunnysideup.co.nz
4
 * Simple extension to add Google Custom Search to SilverStripe
5
 */
6
class GoogleCustomSearchExt extends SiteTreeExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
9
    /**
10
     * @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key
11
     * @var String
12
     */
13
    private static $api_key = "";
0 ignored issues
show
Unused Code introduced by
The property $api_key is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    /**
16
     *
17
     * @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key
18
     * @var String
19
     */
20
    private static $cx_key = "";
0 ignored issues
show
Unused Code introduced by
The property $cx_key is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
22
23
    /**
24
     * returns the form
25
     * @return Form
0 ignored issues
show
Documentation introduced by
Should the return type not be Form|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
26
     */
27
    public function getGoogleSiteSearchForm($name = "GoogleSiteSearchForm")
0 ignored issues
show
Coding Style introduced by
getGoogleSiteSearchForm uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
28
    {
29
        $formIDinHTML = "Form_".$name;
30
        if ($page = GoogleCustomSearchPage::get()->first()) {
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
            Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
32
            Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearch.js');
33
            $apiKey = Config::inst()->get("GoogleCustomSearchExt", "api_key");
34
            $cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key");
35
            if ($apiKey && $cxKey) {
36
                Requirements::customScript("
37
						GoogleCustomSearch.apiKey = '".$apiKey."';
38
						GoogleCustomSearch.cxKey = '".$cxKey."';
39
						GoogleCustomSearch.formSelector = '#".$formIDinHTML."';
40
						GoogleCustomSearch.inputFieldSelector = '#".$formIDinHTML."_search';
41
						GoogleCustomSearch.resultsSelector = '#".$formIDinHTML."_Results';
42
					",
43
                    "GoogleCustomSearchExt"
44
                );
45
                $form = new Form(
46
                    $this->owner,
47
                    'GoogleSiteSearchForm',
48
                    new FieldList(
49
                        $searchField = new TextField('search'),
50
                        $resultField = new LiteralField($name."_Results", "<div id=\"".$formIDinHTML."_Results\"></div>")
51
                    ),
52
                    new FieldList(new FormAction('doSearch', _t("GoogleCustomSearchExt.GO", "Full Results")))
53
                );
54
                $form->setFormMethod('GET');
55
                if ($page = GoogleCustomSearchPage::get()->first()) {
56
                    $form->setFormAction($page->Link());
57
                }
58
                $form->disableSecurityToken();
59
                $form->loadDataFrom($_GET);
60
                $searchField->setAttribute("autocomplete", "off");
61
                $form->setAttribute("autocomplete", "off");
62
                return $form;
63
            } else {
64
                user_error("You must set an API Key and a CX key in your configs to use the Google Custom Search Form", E_USER_NOTICE);
65
            }
66
        } else {
67
            user_error("You must create a GoogleCustomSearchPage first.", E_USER_NOTICE);
68
        }
69
    }
70
71
    /**
72
     * returns the Google Search page...
73
     * @return GoogleCustomSearchPage
74
     */
75
    public function GoogleCustomSearchPage()
76
    {
77
        return GoogleCustomSearchPage::get()->first();
78
    }
79
}
80