GoogleDfpSlotHelper::enabled()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 0
1
<?php
2
3
/**
4
 * Google DFP Slot Helper class
5
 *
6
 *
7
 * @package SilverStripe-Google-DFP
8
 * @copyright 2017 Fractas Labs
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
10
 * @link https://github.com/fractaslabs/silverstripe-google-dfp
11
 */
12
class GoogleDfpSlotHelper extends Object
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...
13
{
14
    private static $id;
0 ignored issues
show
Unused Code introduced by
The property $id 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...
15
16
    private static $enable_in_dev = false;
0 ignored issues
show
Unused Code introduced by
The property $enable_in_dev 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...
17
18
    public function GoogleDfpID()
19
    {
20
        return Config::inst()->get('GoogleDfpSlotHelper', 'publisher_id');
21
    }
22
23
    public static function enable_in_dev()
24
    {
25
        return Config::inst()->get('GoogleDfpSlotHelper', 'enable_in_dev');
26
    }
27
28
    /**
29
     * Check if ads should be enabled
30
     * By default they are enabled in "live" enviroment
31
     *
32
     * @return boolean
33
     */
34
    public static function enabled()
35
    {
36
        if (Director::isLive()) {
37
            return true;
38
        } elseif (Director::isDev() && self::enable_in_dev()) {
39
            return true;
40
        }
41
42
        return false;
43
    }
44
45
    /**
46
     *
47
     * ID - unique identifier of banner
48
     * Layout - Controller ClassName where banner should be rendered
49
     * Alias - name of banner (used to distinguish )
50
     * AdUnitPath - Full path of the ad unit with the network code and ad unit code.
51
     * Size - An exact size of banner creative
52
     * OutOfPage - if banner is "wallpaper" or "floater" type specify as "true"
53
     * DfpTargetPage - current Page ID
54
     * DfpTargetCategory - Parent Title of current Page ID
55
     * DfpTargetCategoryParent - GrandParent Title of current Page ID (if has)
56
     *
57
     * @return mixed
58
     */
59
    public static function slot_list()
60
    {
61
        $list = Config::inst()->get('GoogleDfpSlotHelper', 'layouts');
62
        if (!is_array($list)) {
63
            return false;
64
        }
65
66
        $out = ArrayList::create();
67
        $controller = Controller::curr();
68
        $dfpTargetPage = $controller->ID;
69
        $dfpTargetCategory = $controller->Parent()->Title ? $controller->Parent()->Title : $controller->Title;
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on Controller. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
70
        $dfpTargetCategoryParent = $controller->Parent()->Parent()->Title ? $controller->Parent()->Parent()->Title : null;
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on Controller. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
71
72
        foreach ($list as $key => $item) {
73
            foreach ($item as $slotKey => $slot) {
74
                $out->add(ArrayData::create(array(
75
                    'ID' => $slotKey,
76
                    'Layout' => $key,
77
                    'Alias' => isset($slot['alias']) ? $slot['alias'] : null,
78
                    'AdUnitPath' => isset($slot['adUnitPath']) ? $slot['adUnitPath'] : null,
79
                    'Size' => isset($slot['size']) ? $slot['size'] : null,
80
                    'OutOfPage' => isset($slot['outOfPage']) ? $slot['outOfPage'] : false,
81
                    'DfpTargetPage' => $dfpTargetPage,
82
                    'DfpTargetCategory' => $dfpTargetCategory,
83
                    'DfpTargetCategoryParent' => $dfpTargetCategoryParent,
84
                    'DfpTargetRos' => $key,
85
                )));
86
            }
87
        }
88
89
        return $out;
90
    }
91
92
    /**
93
     * Get one banner by ID
94
     *
95
     *
96
     * @param $id
97
     */
98
    public static function slot_by_id($id = false)
99
    {
100
        return self::slot_list()
101
                ->filter(array('ID' => $id, 'Layout' => Controller::curr()->ClassName))
102
                ->first();
103
    }
104
105
    /**
106
     * Get one banner by Alias name
107
     *
108
     *
109
     * @param $alias
110
     */
111
    public static function slot_by_alias($alias = false)
112
    {
113
        return self::slot_list()
114
                ->filter(array('Alias' => $alias, 'Layout' => Controller::curr()->ClassName))
115
                ->first();
116
    }
117
}
118