Completed
Pull Request — master (#421)
by Robbie
02:06
created

BlogPostFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 9
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A augmentSQL() 0 15 4
A augmentLoadLazyFields() 0 7 1
1
<?php
2
3
namespace SilverStripe\Blog\Model;
4
5
use SilverStripe\Admin\LeftAndMain;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\DataQuery;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverStripe\ORM\Queries\SQLSelect;
13
use SilverStripe\ORM\Versioning\Versioned;
14
use SilverStripe\Security\Permission;
15
16
/**
17
 * This is responsible for filtering only published posts to users who do not have permission to
18
 * view non-published posts.
19
 *
20
 * @package silverstripe
21
 * @subpackage blog
22
 */
23
class BlogPostFilter extends DataExtension
24
{
25
    /**
26
     * Augment queries so that we don't fetch unpublished articles.
27
     *
28
     * @param SQLQuery $query
29
     */
30
    public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
31
    {
32
        $stage = Versioned::get_stage();
33
34
        if (Controller::curr() instanceof LeftAndMain) {
35
            return;
36
        }
37
38
        if ($stage == 'Live' || !Permission::check('VIEW_DRAFT_CONTENT')) {
39
            $query->addWhere(sprintf(
40
                '"PublishDate" < \'%s\'',
41
                Convert::raw2sql(DBDatetime::now())
42
            ));
43
        }
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     *
49
     * This is a fix so that when we try to fetch subclasses of BlogPost, lazy loading includes the
50
     * BlogPost table in its query. Leaving this table out means the default sort order column
51
     * PublishDate causes an error.
52
     *
53
     * @see https://github.com/silverstripe/silverstripe-framework/issues/1682
54
     *
55
     * @param SQLSelect $query
56
     * @param DataQuery $dataQuery
57
     * @param DataObject $dataObject
58
     */
59
    public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $dataObject is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        $dataQuery->innerJoin(
0 ignored issues
show
Bug introduced by
It seems like $dataQuery is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
62
            DataObject::getSchema()->tableName(BlogPost::class),
63
            '"SiteTree"."ID" = "BlogPost"."ID"'
64
        );
65
    }
66
}
67