| 1 |
<?php |
| 2 |
namespace MediaWiki\PoolCounter; |
| 3 |
|
| 4 |
use Psr\Log\LoggerInterface; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class PoolCounterFactory { |
| 10 |
private ?PoolCounterConnectionManager $manager = null; |
| 11 |
private ?array $typeConfigs; |
| 12 |
private array $clientConf; |
| 13 |
private LoggerInterface $logger; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public function __construct( ?array $typeConfigs, array $clientConf, LoggerInterface $logger ) { |
| 22 |
$this->typeConfigs = $typeConfigs; |
| 23 |
$this->clientConf = $clientConf; |
| 24 |
$this->logger = $logger; |
| 25 |
} |
| 26 |
|
| 27 |
private function getClientManager(): PoolCounterConnectionManager { |
| 28 |
$this->manager ??= new PoolCounterConnectionManager( $this->clientConf ); |
| 29 |
return $this->manager; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function create( string $type, string $key ): PoolCounter { |
| 41 |
$conf = $this->typeConfigs[$type] ?? null; |
| 42 |
if ( $conf === null ) { |
| 43 |
return new PoolCounterNull(); |
| 44 |
} |
| 45 |
|
| 46 |
$class = $conf['class'] ?? null; |
| 47 |
if ( $class === 'PoolCounter_Client' ) { |
| 48 |
|
| 49 |
|
| 50 |
$class = PoolCounterClient::class; |
| 51 |
} |
| 52 |
|
| 53 |
$poolCounter = new $class( $conf, $type, $key ); |
| 54 |
$poolCounter->setLogger( $this->logger ); |
| 55 |
|
| 56 |
|
| 57 |
if ( $poolCounter instanceof PoolCounterClient ) { |
| 58 |
$poolCounter->setManager( $this->getClientManager() ); |
| 59 |
} |
| 60 |
|
| 61 |
return $poolCounter; |
| 62 |
} |
| 63 |
} |