Completed
Push — master ( 1d7363...6e2aa4 )
by Daniel
05:45
created

AkismetProcessor::isDBReady()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 8.9197
cc 4
eloc 11
nc 4
nop 0
1
<?php
2
3
/**
4
 * Allows akismet to be configured via siteconfig instead of hard-coded configuration
5
 */
6
class AkismetProcessor implements RequestFilter
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
    public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model)
9
    {
10
    }
11
12
    public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
13
    {
14
        // Skip if database isn't ready
15
        if (!$this->isDBReady()) {
16
            return;
17
        }
18
19
        // Skip if SiteConfig doesn't have this extension
20
        if (!SiteConfig::has_extension('AkismetConfig')) {
21
            return;
22
        }
23
24
        // Check if key exists
25
        $akismetKey = SiteConfig::current_site_config()->AkismetKey;
26
        if ($akismetKey) {
27
            AkismetSpamProtector::set_api_key($akismetKey);
28
        }
29
    }
30
31
    /**
32
     * Make sure the DB is ready before accessing siteconfig db field
33
     *
34
     * @return bool
35
     */
36
    protected function isDBReady()
37
    {
38
        if (!DB::isActive()) {
0 ignored issues
show
Deprecated Code introduced by
The method DB::isActive() has been deprecated with message: since version 4.0 Use DB::is_active instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
39
            return false;
40
        }
41
42
        // Require table
43
        if (!DB::getConn()->hasTable('SiteConfig')) {
0 ignored issues
show
Deprecated Code introduced by
The method DB::getConn() has been deprecated with message: since version 4.0 Use DB::get_conn instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method SS_Database::hasTable() has been deprecated with message: since version 4.0 Use DB::get_schema()->hasTable() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
44
            return false;
45
        }
46
47
        // Ensure siteconfig has all fields necessary
48
        $dbFields = DB::fieldList('SiteConfig');
0 ignored issues
show
Deprecated Code introduced by
The method DB::fieldList() has been deprecated with message: since version 4.0 Use DB::field_list instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
49
        if (empty($dbFields)) {
50
            return false;
51
        }
52
53
        // Ensure that SiteConfig has all fields
54
        $objFields = DataObject::database_fields('SiteConfig', false);
55
        $missingFields = array_diff_key($objFields, $dbFields);
56
        return empty($missingFields);
57
    }
58
}
59