CheckNeedTrait::is_needed()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 12
1
<?php
2
/**
3
 * Bright Nucleus Shortcode Component.
4
 *
5
 * @package   BrightNucleus\Shortcode
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Shortcode;
13
14
/**
15
 * Check Need Trait.
16
 *
17
 * Provides the `is_needed()` method.
18
 *
19
 * @since   0.1.0
20
 *
21
 * @package BrightNucleus\Shortcode
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
trait CheckNeedTrait {
25
26
	/**
27
	 * Check whether an element is needed.
28
	 *
29
	 * @since 0.2.0
30
	 *
31
	 * @param mixed $context Data about the context in which the call is made.
32
	 * @return boolean Whether the element is needed or not.
33
	 */
34
	protected function is_needed( $context = null ) {
35
36
		$is_needed = $this->hasConfigKey( 'is_needed' )
37
			? $this->getConfigKey( 'is_needed' )
38
			: true;
39
40
		if ( is_callable( $is_needed ) ) {
41
			return $is_needed( $context );
42
		}
43
44
		return (bool) $is_needed;
45
	}
46
47
	/**
48
	 * Check whether the Config has a specific key.
49
	 *
50
	 * This needs to be implemented in a class that wants to use CheckNeedTrait.
51
	 *
52
	 * @since 0.2.10
53
	 *
54
	 * @param string|array $_ List of keys.
55
	 * @return bool Whether the key is known.
56
	 */
57
	abstract protected function hasConfigKey( $_ );
58
59
	/**
60
	 * Get the Config value for a specific key.
61
	 *
62
	 * This needs to be implemented in a class that wants to use CheckNeedTrait.
63
	 *
64
	 * @since 0.2.10
65
	 *
66
	 * @param string|array $_ List of keys.
67
	 * @return mixed Value of the key.
68
	 */
69
	abstract protected function getConfigKey( $_ );
70
}
71