|
9 | 9 | */ |
10 | 10 | class Di |
11 | 11 | { |
| 12 | + const SAME_APIKEY = "Factory Instantiation: You already have %s factory/factories with this API Key. " |
| 13 | + . "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application."; |
| 14 | + |
| 15 | + const MULTIPLE_INSTANCES = "Factory Instantiation: You already have an instance of the Split factory. " . |
| 16 | + "Make sure you definitely want this additional instance. We recommend keeping only one instance of the factory at all times " . |
| 17 | + "(Singleton pattern) and reusing it throughout your application."; |
| 18 | + |
12 | 19 | private \SplitIO\Component\Log\Logger $logger; |
13 | 20 |
|
14 | 21 | private array $factoryTracker = array(); |
@@ -68,33 +75,15 @@ public function __wakeup() |
68 | 75 | */ |
69 | 76 | public static function trackFactory($apiKey) |
70 | 77 | { |
71 | | - $tracked = 1; |
72 | | - if (isset(self::getInstance()->factoryTracker[$apiKey])) { |
73 | | - $currentInstances = self::getInstance()->factoryTracker[$apiKey]; |
74 | | - if ($currentInstances == 1) { |
75 | | - self::getInstance()->getLogger()->warning( |
76 | | - "Factory Instantiation: You already have 1 factory with this API Key. " . |
77 | | - "We recommend keeping only one instance of the factory at all times " . |
78 | | - "(Singleton pattern) and reusing it throughout your application." |
79 | | - ); |
80 | | - } else { |
81 | | - self::getInstance()->getLogger()->warning( |
82 | | - "Factory Instantiation: You already have " . $currentInstances . " factories with this API Key. " . |
83 | | - "We recommend keeping only one instance of the factory at all times " . |
84 | | - "(Singleton pattern) and reusing it throughout your application." |
85 | | - ); |
86 | | - } |
87 | | - $tracked = $currentInstances + $tracked; |
88 | | - } elseif (count(self::getInstance()->factoryTracker) > 0) { |
89 | | - self::getInstance()->getLogger()->warning( |
90 | | - "Factory Instantiation: You already have an instance of the Split factory. " . |
91 | | - "Make sure you definitely want this additional instance. " . |
92 | | - "We recommend keeping only one instance of the factory at all times " . |
93 | | - "(Singleton pattern) and reusing it throughout your application." |
94 | | - ); |
| 78 | + $current = self::getInstance()->factoryTracker[$apiKey] ?? 0; |
| 79 | + if ($current == 0) { |
| 80 | + self::getInstance()->getLogger()->warning(self::MULTIPLE_INSTANCES); |
| 81 | + } else { |
| 82 | + self::getInstance()->getLogger()->warning(sprintf(self::SAME_APIKEY, $current)); |
95 | 83 | } |
96 | | - self::getInstance()->factoryTracker[$apiKey] = $tracked; |
97 | | - return $tracked; |
| 84 | + $current += 1; |
| 85 | + self::getInstance()->factoryTracker[$apiKey] = $current; |
| 86 | + return $current; |
98 | 87 | } |
99 | 88 |
|
100 | 89 | /** |
|
0 commit comments