Completed
Branch barista (5d11bd)
by
unknown
52:14 queued 42:26
created

FeatureFlags::getAllowedFeatures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\capabilities;
4
5
use EventEspresso\core\exceptions\InsufficientPermissionsException;
6
7
/**
8
 * Class FeatureFlags
9
 *
10
 * @author  Brent Christensen
11
 * @package EventEspresso\core\domain\services\capabilities
12
 * @since   $VID:$
13
 */
14
class FeatureFlags
15
{
16
17
    /**
18
     * @var CapabilitiesChecker $capabilities_checker
19
     */
20
    private $capabilities_checker;
21
22
    /**
23
     * array of key value pairs where the key is the feature flag in question
24
     * and the value is either a boolean or a CapCheck object defining the required permissions
25
     * example:
26
     *       [
27
     *          'use_bulk_edit' => true,
28
     *          'use_death_ray' => new CapCheck( 'ee-death-ray-cap', 'context-desc' )
29
     *      ]
30
     * array is filterable via FHEE__EventEspresso_core_domain_services_capabilities_FeatureFlags
31
     *
32
     * @var boolean[]|CapCheck[]
33
     */
34
    private $feature_flags = [];
35
36
37
    /**
38
     * FeatureFlags constructor.
39
     *
40
     * @param CapabilitiesChecker $capabilities_checker
41
     */
42
    public function __construct(CapabilitiesChecker $capabilities_checker)
43
    {
44
        $this->capabilities_checker = $capabilities_checker;
45
        $this->feature_flags = apply_filters(
46
            'FHEE__EventEspresso_core_domain_services_capabilities_FeatureFlags',
47
            [
48
                'use_bulk_edit'             => false,
49
                'use_event_description_rte' => false,
50
                'use_reg_options_meta_box'  => false,
51
            ]
52
        );
53
    }
54
55
56
    /**
57
     * @param string $feature
58
     * @return bool
59
     */
60
    public function featureAllowed(string $feature)
61
    {
62
        $flag = isset($this->feature_flags[ $feature ]) ? $this->feature_flags[ $feature ] : false;
63
        try {
64
            return $flag instanceof CapCheck
65
                ? $this->capabilities_checker->processCapCheck($flag)
66
                : $flag;
67
        } catch (InsufficientPermissionsException $e) {
68
            // eat the exception
69
        }
70
        return false;
71
    }
72
73
74
    /**
75
     * @return array
76
     */
77
    public function getAllowedFeatures()
78
    {
79
        $allowed = array_filter($this->feature_flags, [$this, 'featureAllowed'], ARRAY_FILTER_USE_KEY);
80
        return array_keys($allowed);
81
    }
82
}
83