Completed
Push — try/capabilities ( a2fcc0...6ae562 )
by
unknown
08:11
created

Builder::require_any_blog_sticker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * A class that can be used to build capability objects composed of many rules
4
 *
5
 * @package automattic/jetpack-capabilities
6
 */
7
8
namespace Automattic\Jetpack\Capabilities;
9
10
use \Automattic\Jetpack\Capabilities;
11
12
// phpcs:ignore Squiz.Commenting.ClassComment.Missing
13
class Builder {
14
	/**
15
	 * The aggregate object under construction
16
	 *
17
	 * @var AggregateRule aggregate_rule
18
	 */
19
	public $aggregate_rule;
20
21
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
22
	public function create() {
23
		$this->aggregate_rule = new AllRule();
24
		return $this;
25
	}
26
27
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
28
	public function create_any() {
29
		$this->aggregate_rule = new AtLeastOneRule();
30
		return $this;
31
	}
32
33
	/**
34
	 * Register a capability globally
35
	 *
36
	 * @param string $name the name used to register and look up the capability.
37
	 */
38
	public function register( $name ) {
39
		$this->aggregate_rule->register( $name );
40
		return $this;
41
	}
42
43
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
44
	public function get() {
45
		return $this->aggregate_rule;
46
	}
47
48
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
49
	public function add_rule( Rule $rule ) {
50
		$this->aggregate_rule->add_rule( $rule );
51
		return $this;
52
	}
53
54
	/**
55
	 * The following functions are basically just shortcuts to the above ->add_rule method
56
	 */
57
58
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
59
	public function require_wp_role( $wp_role ) {
60
		return $this->add_rule( new WPRoleRule( $wp_role ) );
61
	}
62
63
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
64
	public function require_wp_capability( $wp_capability ) {
65
		return $this->add_rule( new WPCapabilityRule( $wp_capability ) );
66
	}
67
68
	/**
69
	 * For traditional Jetpack plans (free, personal, premium, professional ) this
70
	 * specifies the minimum plan required in required to perform the action
71
	 *
72
	 * @param string $jetpack_plan_level The Jetpack plan level.
73
	 */
74
	public function require_minimum_jetpack_plan( $jetpack_plan_level ) {
75
		return $this->add_rule( new JetpackPlanRule( $jetpack_plan_level ) );
76
	}
77
78
	/**
79
	 * Adapter for legacy 'supports' API
80
	 *
81
	 * @param string $jetpack_plan_supports The slug of the feature we are checking support for.
82
	 */
83
	public function require_jetpack_plan_supports( $jetpack_plan_supports ) {
84
		return $this->add_rule( new JetpackPlanSupportsRule( $jetpack_plan_supports ) );
85
	}
86
87
	/**
88
	 * Requires that the output of running a certain filter is a certain value
89
	 *
90
	 * @param string $filter_name The name of the filter to apply.
91
	 * @param mixed  $required_value The value that is required for the rule to pass.
92
	 */
93
	public function require_filter( $filter_name, $required_value ) {
94
		return $this->add_rule( new WPFilterRule( $filter_name, $required_value ) );
95
	}
96
97
	/**
98
	 * Requires that Jetpack is connected
99
	 */
100
	public function require_jetpack_is_active() {
101
		return $this->add_rule( new JetpackActiveRule() );
102
	}
103
104
	public function require_any_blog_sticker( $stickers ) {
105
		return $this->add_rule( new BlogStickersRule( $stickers ) );
106
	}
107
108
	/**
109
	 * Allows chaining optional inner dependencies together
110
	 *
111
	 * @param function $callback A function reference to call back with the nested builder.
112
	 */
113
	public function require_any( $callback ) {
114
		$builder = new Builder();
115
		$this->add_rule( $builder->create_any()->get() );
116
		// the callback adds nested rules to the object created above.
117
		$callback( $builder );
118
		return $this;
119
	}
120
}
121