Skip to content

Commit 7dcc25e

Browse files
committed
improved logging multiple factories
1 parent ca411b9 commit 7dcc25e

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/SplitIO/Component/Common/Di.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
*/
1010
class Di
1111
{
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+
1219
private \SplitIO\Component\Log\Logger $logger;
1320

1421
private array $factoryTracker = array();
@@ -68,33 +75,15 @@ public function __wakeup()
6875
*/
6976
public static function trackFactory($apiKey)
7077
{
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));
9583
}
96-
self::getInstance()->factoryTracker[$apiKey] = $tracked;
97-
return $tracked;
84+
$current += 1;
85+
self::getInstance()->factoryTracker[$apiKey] = $current;
86+
return $current;
9887
}
9988

10089
/**

tests/Suite/InputValidation/FactoryTrackerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public function testMultipleClientInstantiation()
3939
$logger->expects($this->any())
4040
->method('warning')
4141
->with($this->logicalOr(
42-
$this->equalTo("Factory Instantiation: You already have 1 factory with this API Key. "
42+
$this->equalTo("Factory Instantiation: You already have 1 factory/factories with this API Key. "
4343
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing "
4444
. "it throughout your application."),
45-
$this->equalTo("Factory Instantiation: You already have 2 factories with this API Key. "
45+
$this->equalTo("Factory Instantiation: You already have 2 factory/factories with this API Key. "
4646
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing "
4747
. "it throughout your application."),
4848
$this->equalTo("Factory Instantiation: You already have an instance of the Split factory. "

0 commit comments

Comments
 (0)