BlogPostFilter::augmentLoadLazyFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Security\Permission;
14
use SilverStripe\Versioned\Versioned;
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
 */
21
class BlogPostFilter extends DataExtension
22
{
23
    /**
24
     * Augment queries so that we don't fetch unpublished articles.
25
     *
26
     * @param SQLSelect $query
27
     * @param DataQuery $query
28
     */
29
    public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
30
    {
31
        $stage = Versioned::get_stage();
32
33
        if (Controller::has_curr() && Controller::curr() instanceof LeftAndMain) {
34
            return;
35
        }
36
37
        if ($stage == 'Live' || !Permission::check('VIEW_DRAFT_CONTENT')) {
38
            $query->addWhere(sprintf(
39
                '"PublishDate" < \'%s\'',
40
                Convert::raw2sql(DBDatetime::now())
41
            ));
42
        }
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     *
48
     * This is a fix so that when we try to fetch subclasses of BlogPost, lazy loading includes the
49
     * BlogPost table in its query. Leaving this table out means the default sort order column
50
     * PublishDate causes an error.
51
     *
52
     * @see https://github.com/silverstripe/silverstripe-framework/issues/1682
53
     *
54
     * @param SQLSelect $query
55
     * @param DataQuery $dataQuery
56
     * @param DataObject $dataObject
57
     */
58
    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...
59
    {
60
        $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...
61
            DataObject::getSchema()->tableName(BlogPost::class),
62
            '"SiteTree"."ID" = "BlogPost"."ID"'
63
        );
64
    }
65
}
66