Completed
Pull Request — master (#185)
by Janne
03:01
created

Commenting::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * Helper Class for storing the configuration options. Retains the mapping between
5
 * objects which have comments attached and the related configuration options.
6
 *
7
 * Also handles adding the Commenting extension to the {@link DataObject} on behalf
8
 * of the user.
9
 *
10
 * For documentation on how to use this class see docs/en/Configuration.md
11
 *
12
 * @deprecated since version 2.0
13
 *
14
 * @package comments
15
 */
16
class Commenting
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...
17
{
18
19
    /**
20
     * Adds commenting to a {@link DataObject}
21
     *
22
     * @deprecated since version 2.0
23
     *
24
     * @param string classname to add commenting to
25
     * @param array $settings Settings. See {@link self::$default_config} for
0 ignored issues
show
Documentation introduced by
Should the type for parameter $settings not be false|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     *			available settings
27
     * 
28
     * @throws InvalidArgumentException
29
     */
30
    public static function add($class, $settings = false)
31
    {
32
        Deprecation::notice('2.0', 'Using Commenting::add is deprecated. Please use the config API instead');
33
        Config::inst()->update($class, 'extensions', array('CommentsExtension'));
34
35
        // Check if settings must be customised
36
        if ($settings === false) {
37
            return;
38
        }
39
        if (!is_array($settings)) {
40
            throw new InvalidArgumentException('$settings needs to be an array or null');
41
        }
42
        Config::inst()->update($class, 'comments', $settings);
43
    }
44
45
    /**
46
     * Removes commenting from a {@link DataObject}. Does not remove existing comments
47
     * but does remove the extension.
48
     *
49
     * @deprecated since version 2.0
50
     *
51
     * @param string $class Class to remove {@link CommentsExtension} from
52
     */
53
    public static function remove($class)
54
    {
55
        Deprecation::notice('2.0', 'Using Commenting::remove is deprecated. Please use the config API instead');
56
        $class::remove_extension('CommentsExtension');
57
    }
58
59
    /**
60
     * Returns whether a given class name has commenting enabled
61
     *
62
     * @deprecated since version 2.0
63
     *
64
     * @return bool
65
     */
66
    public static function has_commenting($class)
67
    {
68
        Deprecation::notice('2.0', 'Using Commenting::has_commenting is deprecated. Please use the config API instead');
69
        return $class::has_extension('CommentsExtension');
70
    }
71
72
    /**
73
     * Sets a value for a class of a given config setting. Passing 'all' as the class
74
     * sets it for everything
75
     *
76
     * @deprecated since version 2.0
77
     *
78
     * @param string $class Class to set the value on. Passing 'all' will set it to all 
79
     *			active mappings
80
     * @param string $key setting to change
81
     * @param mixed $value value of the setting
82
     */
83
    public static function set_config_value($class, $key, $value = false)
84
    {
85
        Deprecation::notice('2.0', 'Commenting::set_config_value is deprecated. Use the config api instead');
86
        if ($class === "all") {
87
            $class = 'CommentsExtension';
88
        }
89
        Config::inst()->update($class, 'comments', array($key => $value));
90
    }
91
92
    /**
93
     * Returns a given config value for a commenting class
94
     *
95
     * @deprecated since version 2.0
96
     * 
97
     * @param string $class
98
     * @param string $key config value to return
99
     *
100
     * @throws Exception
101
     * @return mixed
102
     */
103
    public static function get_config_value($class, $key)
104
    {
105
        Deprecation::notice(
106
            '2.0',
107
            'Using Commenting::get_config_value is deprecated. Please use $parent->getCommentsOption() or '
108
            . 'CommentingController::getOption() instead'
109
        );
110
111
        // Get settings
112
        if (!$class) {
113
            $class = 'CommentsExtension';
114
        } elseif (!$class::has_extension('CommentsExtension')) {
115
            throw new InvalidArgumentException("$class does not have commenting enabled");
116
        }
117
        return singleton($class)->getCommentsOption($key);
118
    }
119
120
    /**
121
     * Determines whether a config value on the commenting extension
122
     * matches a given value.
123
     *
124
     * @deprecated since version 2.0
125
     *
126
     * @param string $class
127
     * @param string $key
128
     * @param string $value Expected value
129
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|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...
130
     */
131
    public static function config_value_equals($class, $key, $value)
132
    {
133
        $check = self::get_config_value($class, $key);
0 ignored issues
show
Deprecated Code introduced by
The method Commenting::get_config_value() has been deprecated with message: since version 2.0

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...
134
        if ($check && ($check == $value)) {
135
            return true;
136
        }
137
    }
138
139
    /**
140
     * Return whether a user can post on a given commenting instance
141
     *
142
     * @deprecated since version 2.0
143
     * 
144
     * @param string $class
145
     * @return boolean true
146
     */
147
    public static function can_member_post($class)
148
    {
149
        Deprecation::notice('2.0',  'Use $instance->canPostComment() directly instead');
150
        $member = Member::currentUser();
151
152
        // Check permission
153
        $permission = self::get_config_value($class, 'required_permission');
0 ignored issues
show
Deprecated Code introduced by
The method Commenting::get_config_value() has been deprecated with message: since version 2.0

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...
154
        if ($permission && !Permission::check($permission)) {
155
            return false;
156
        }
157
158
        // Check login required
159
        $requireLogin = self::get_config_value($class, 'require_login');
0 ignored issues
show
Deprecated Code introduced by
The method Commenting::get_config_value() has been deprecated with message: since version 2.0

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...
160
        return !$requireLogin || $member;
161
    }
162
}
163