PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.46.8
MailPoet – Newsletters, Email Marketing, and Automation v3.46.8
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / Features / FeaturesController.php

FeaturesController.php in MailPoet – Newsletters, Email Marketing, and Automation 3.46.8, at lib/Features/FeaturesController.php

83 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MailPoet\Features;
4
5 if (!defined('ABSPATH')) exit;
6
7
8 use MailPoetVendor\Doctrine\DBAL\Exception\TableNotFoundException;
9
10 class FeaturesController {
11
12 // Define features below in the following form:
13 // const FEATURE_NAME_OF_FEATURE = 'name-of-feature';
14 const NEW_DEFAULT_LIST_NAME = 'new-default-list-name';
15
16 // Define feature defaults in the array below in the following form:
17 // self::FEATURE_NAME_OF_FEATURE => true,
18 private $defaults = [
19 self::NEW_DEFAULT_LIST_NAME => false,
20 ];
21
22 /** @var array */
23 private $flags;
24
25 /** @var FeatureFlagsRepository */
26 private $featureFlagsRepository;
27
28 public function __construct(FeatureFlagsRepository $featureFlagsRepository) {
29 $this->featureFlagsRepository = $featureFlagsRepository;
30 }
31
32 /** @return bool */
33 public function isSupported($feature) {
34 if (!$this->exists($feature)) {
35 throw new \RuntimeException("Unknown feature '$feature'");
36 }
37 // ensure controller works even if used before migrator, return default value in such case
38 try {
39 $this->ensureFlagsLoaded();
40 } catch (TableNotFoundException $e) {
41 return $this->defaults[$feature];
42 }
43 return $this->flags[$feature];
44 }
45
46 /** @return bool */
47 public function exists($feature) {
48 return array_key_exists($feature, $this->defaults);
49 }
50
51 /** @return array */
52 public function getDefaults() {
53 return $this->defaults;
54 }
55
56 /** @return array */
57 public function getAllFlags() {
58 $this->ensureFlagsLoaded();
59 return $this->flags;
60 }
61
62 private function ensureFlagsLoaded() {
63 if ($this->flags !== null) {
64 return;
65 }
66
67 $flagsMap = $this->getValueMap();
68 $this->flags = [];
69 foreach ($this->defaults as $name => $default) {
70 $this->flags[$name] = isset($flagsMap[$name]) ? $flagsMap[$name] : $default;
71 }
72 }
73
74 private function getValueMap() {
75 $features = $this->featureFlagsRepository->findAll();
76 $featuresMap = [];
77 foreach ($features as $feature) {
78 $featuresMap[$feature->getName()] = (bool)$feature->getValue();
79 }
80 return $featuresMap;
81 }
82 }
83