Skip to content

Commit 143e27b

Browse files
committed
Segregate methods from DriverBase
1 parent d1c6901 commit 143e27b

6 files changed

Lines changed: 89 additions & 90 deletions

File tree

src/TripodStatFactory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Tripod;
66

7-
use Tripod\Mongo\NoStat;
8-
97
class TripodStatFactory
108
{
119
/**

src/classes/NoStat.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tripod;
6+
7+
final class NoStat implements ITripodStat
8+
{
9+
/**
10+
* @var self
11+
*/
12+
public static $instance;
13+
14+
public function increment(string $operation, int $inc = 1): void
15+
{
16+
// do nothing
17+
}
18+
19+
/**
20+
* @param float|int $duration
21+
*/
22+
public function timer(string $operation, $duration): void
23+
{
24+
// do nothing
25+
}
26+
27+
public function getConfig(): array
28+
{
29+
return [];
30+
}
31+
32+
public static function getInstance(): self
33+
{
34+
if (self::$instance == null) {
35+
self::$instance = new NoStat();
36+
}
37+
38+
return self::$instance;
39+
}
40+
41+
/**
42+
* @return self
43+
*/
44+
public static function createFromConfig(array $config = [])
45+
{
46+
return self::getInstance();
47+
}
48+
}

src/mongo/base/DriverBase.php

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Monolog\Logger;
1111
use Psr\Log\LoggerInterface;
1212
use Psr\Log\LogLevel;
13-
use Tripod\Exceptions\Exception;
14-
use Tripod\IEventHook;
1513
use Tripod\ITripodStat;
1614
use Tripod\Mongo\Composites\Views;
1715
use Tripod\StatsD;
@@ -20,15 +18,6 @@
2018

2119
abstract class DriverBase
2220
{
23-
/**
24-
* constants for the supported hook functions that can be applied.
25-
*/
26-
public const HOOK_FN_PRE = 'pre';
27-
28-
public const HOOK_FN_SUCCESS = 'success';
29-
30-
public const HOOK_FN_FAILURE = 'failure';
31-
3221
public static ?LoggerInterface $logger = null;
3322

3423
protected string $storeName;
@@ -157,11 +146,6 @@ protected function getStatFromStatFactory(): ITripodStat
157146
return TripodStatFactory::create($this->statsConfig);
158147
}
159148

160-
protected function getExpirySecFromNow(int $secs): int
161-
{
162-
return time() + $secs;
163-
}
164-
165149
protected function getContextAlias(?string $context = null): string
166150
{
167151
$contextAlias = $this->labeller->uri_to_alias((empty($context)) ? $this->defaultContext : $context);
@@ -337,33 +321,6 @@ protected function getCollection(): Collection
337321
return $this->collection;
338322
}
339323

340-
/**
341-
* @param IEventHook[] $hooks
342-
*
343-
* @throws Exception If an invalid hook function is requested
344-
*/
345-
protected function applyHooks(string $fn, array $hooks, array $args = []): void
346-
{
347-
switch ($fn) {
348-
case $this::HOOK_FN_PRE:
349-
case $this::HOOK_FN_SUCCESS:
350-
case $this::HOOK_FN_FAILURE:
351-
break;
352-
353-
default:
354-
throw new Exception(sprintf('Invalid hook function %s requested', $fn));
355-
}
356-
357-
foreach ($hooks as $hook) {
358-
try {
359-
call_user_func([$hook, $fn], $args);
360-
} catch (\Exception $e) {
361-
// don't let rabid hooks stop tripod
362-
static::getLogger()->error('Hook ' . get_class($hook) . sprintf(' threw exception %s, continuing', $e->getMessage()));
363-
}
364-
}
365-
}
366-
367324
/**
368325
* @codeCoverageIgnore
369326
*/
@@ -372,46 +329,3 @@ private function log(string $level, string $message, ?array $params): void
372329
self::getLogger()->log($level, $message, $params ?: []);
373330
}
374331
}
375-
376-
final class NoStat implements ITripodStat
377-
{
378-
/**
379-
* @var self
380-
*/
381-
public static $instance;
382-
383-
public function increment(string $operation, int $inc = 1): void
384-
{
385-
// do nothing
386-
}
387-
388-
/**
389-
* @param float|int $duration
390-
*/
391-
public function timer(string $operation, $duration): void
392-
{
393-
// do nothing
394-
}
395-
396-
public function getConfig(): array
397-
{
398-
return [];
399-
}
400-
401-
public static function getInstance(): self
402-
{
403-
if (self::$instance == null) {
404-
self::$instance = new NoStat();
405-
}
406-
407-
return self::$instance;
408-
}
409-
410-
/**
411-
* @return self
412-
*/
413-
public static function createFromConfig(array $config = [])
414-
{
415-
return self::getInstance();
416-
}
417-
}

src/mongo/delegates/Updates.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
class Updates extends DriverBase
2424
{
25+
/**
26+
* constants for the supported hook functions that can be applied.
27+
*/
28+
public const HOOK_FN_PRE = 'pre';
29+
public const HOOK_FN_SUCCESS = 'success';
30+
public const HOOK_FN_FAILURE = 'failure';
31+
2532
protected Driver $tripod;
2633

2734
protected ?Database $locksDb = null;
@@ -352,6 +359,33 @@ public function registerSaveChangesEventHook(IEventHook $hook): void
352359
$this->saveChangesHooks[] = $hook;
353360
}
354361

362+
/**
363+
* @param IEventHook[] $hooks
364+
*
365+
* @throws Exception If an invalid hook function is requested
366+
*/
367+
protected function applyHooks(string $fn, array $hooks, array $args = []): void
368+
{
369+
switch ($fn) {
370+
case $this::HOOK_FN_PRE:
371+
case $this::HOOK_FN_SUCCESS:
372+
case $this::HOOK_FN_FAILURE:
373+
break;
374+
375+
default:
376+
throw new Exception(sprintf('Invalid hook function %s requested', $fn));
377+
}
378+
379+
foreach ($hooks as $hook) {
380+
try {
381+
call_user_func([$hook, $fn], $args);
382+
} catch (\Exception $e) {
383+
// don't let rabid hooks stop tripod
384+
static::getLogger()->error('Hook ' . get_class($hook) . sprintf(' threw exception %s, continuing', $e->getMessage()));
385+
}
386+
}
387+
}
388+
355389
/**
356390
* Change the read preferences to RP_PRIMARY
357391
* Used for a write operation.
@@ -965,7 +999,7 @@ protected function unlockAllDocuments(string $transaction_id): bool
965999
*
9661000
* @param string $s subject URI of resource to lock
9671001
*
968-
* @return array|null|false
1002+
* @return array|false|null
9691003
*/
9701004
protected function lockSingleDocument(string $s, string $transaction_id, string $contextAlias)
9711005
{

src/mongo/delegates/Views.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ public function count(string $viewSpec, array $filters = []): int
503503
return $this->getCollectionForViewSpec($viewSpec)->count($filters);
504504
}
505505

506+
protected function getExpirySecFromNow(int $secs): int
507+
{
508+
return time() + $secs;
509+
}
510+
506511
/**
507512
* Joins data to $dest from $source according to specification in $joins, or queries DB if data is not available in $source.
508513
*/

test/unit/mongo/MongoTripodStatTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use Tripod\Mongo\NoStat;
5+
use Tripod\NoStat;
66
use Tripod\StatsD;
77
use Tripod\TripodStatFactory;
88

0 commit comments

Comments
 (0)