PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.89.4
MailPoet – Newsletters, Email Marketing, and Automation v3.89.4
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.89.4, at lib/Features/FeaturesController.php

86 lines 2.0 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 public const AUTOMATION = 'automation';
12
13 // Define feature defaults in the array below in the following form:
14 // self::FEATURE_NAME_OF_FEATURE => true,
15 private $defaults = [
16 self::AUTOMATION => false,
17 ];
18
19 /** @var array|null */
20 private $flags;
21
22 /** @var FeatureFlagsRepository */
23 private $featureFlagsRepository;
24
25 public function __construct(
26 FeatureFlagsRepository $featureFlagsRepository
27 ) {
28 $this->featureFlagsRepository = $featureFlagsRepository;
29 }
30
31 /** @return bool */
32 public function isSupported($feature) {
33 if (!$this->exists($feature)) {
34 throw new \RuntimeException("Unknown feature '$feature'");
35 }
36 // ensure controller works even if used before migrator, return default value in such case
37 try {
38 $this->ensureFlagsLoaded();
39 } catch (TableNotFoundException $e) {
40 return $this->defaults[$feature];
41 }
42 return ($this->flags ?? [])[$feature];
43 }
44
45 /** @return bool */
46 public function exists($feature) {
47 return array_key_exists($feature, $this->defaults);
48 }
49
50 /** @return array */
51 public function getDefaults() {
52 return $this->defaults;
53 }
54
55 /** @return array */
56 public function getAllFlags() {
57 $this->ensureFlagsLoaded();
58 return $this->flags ?? [];
59 }
60
61 public function resetCache(): void {
62 $this->flags = null;
63 }
64
65 private function ensureFlagsLoaded() {
66 if ($this->flags !== null) {
67 return;
68 }
69
70 $flagsMap = $this->getValueMap();
71 $this->flags = [];
72 foreach ($this->defaults as $name => $default) {
73 $this->flags[$name] = isset($flagsMap[$name]) ? $flagsMap[$name] : $default;
74 }
75 }
76
77 private function getValueMap() {
78 $features = $this->featureFlagsRepository->findAll();
79 $featuresMap = [];
80 foreach ($features as $feature) {
81 $featuresMap[$feature->getName()] = (bool)$feature->getValue();
82 }
83 return $featuresMap;
84 }
85 }
86