Skip to content

Commit d032d8e

Browse files
committed
add support for manager methods
1 parent d60d542 commit d032d8e

17 files changed

+388
-5
lines changed

examples/manager.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
use \SplitIO\ThinSdk\Factory;
6+
7+
$factory = Factory::withConfig([
8+
'transfer' => [
9+
'address' => '../../splitd/splitd.sock',
10+
'type' => 'unix-stream',
11+
],
12+
'logging' => [
13+
'level' => \Psr\Log\LogLevel::DEBUG,
14+
],
15+
]);
16+
17+
$manager = $factory->manager();
18+
$names = $manager->splitNames();
19+
print_r($names);
20+
var_dump($manager->split($names[0]));
21+
var_dump($manager->splits());

src/Client.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ public function getTreatments(string $key, ?string $bucketingKey, array $feature
5151
return $toReturn;
5252
} catch (\Exception $exc) {
5353
$this->logger->error($exc);
54-
return array_reduce($features, function ($r, $k) { $r[$k] = "control"; return $r; }, []);
54+
return array_reduce($features, function ($r, $k) {
55+
$r[$k] = "control";
56+
return $r;
57+
}, []);
5558
}
5659
}
5760

58-
public function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool
61+
public function track(string $key, string $trafficType, string $eventType, ?float $value = null, ?array $properties = null): bool
5962
{
6063
try {
6164
$properties = $this->inputValidator->validProperties($properties);

src/ClientInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ interface ClientInterface
66
{
77
function getTreatment(string $key, ?string $bucketingKey, string $feature, ?array $attributes): string;
88
function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
9-
function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool;
9+
function track(string $key, string $trafficType, string $eventType, ?float $value = null, ?array $properties = null): bool;
1010
}

src/Factory.php

+5
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public function client(): ClientInterface
3737
{
3838
return new Client($this->linkManager, $this->logger, $this->config->utils()->impressionListener());
3939
}
40+
41+
public function manager(): Manager
42+
{
43+
return new Manager($this->linkManager, $this->logger);
44+
}
4045
};

src/FactoryInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
interface FactoryInterface
66
{
77
public function client(): ClientInterface;
8+
public function manager(): ManagerInterface;
89
};

src/Link/Consumer/Manager.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace SplitIO\ThinSdk\Link\Consumer;
44

5+
use \SplitIO\ThinSdk\SplitView;
6+
57
interface Manager
68
{
79
function getTreatment(string $key, ?string $bucketingKey, string $feature, ?array $attributes): array;
810
function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
911
function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool;
12+
function splitNames(): array;
13+
function split(string $splitName): ?SplitView;
14+
function splits(): array;
1015
}
11-

src/Link/Consumer/V1Manager.php

+30
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use \SplitIO\ThinSdk\Link\Protocol;
66
use \SplitIO\ThinSdk\Link\Protocol\V1\RPC;
7+
use \SplitIO\ThinSdk\Link\Protocol\V1\SplitViewResult;
78
use \SplitIO\ThinSdk\Link\Transfer;
89
use \SplitIO\ThinSdk\Link\Serialization;
10+
use \SplitIO\ThinSdk\SplitView;
911

1012
use \SplitIO\ThinSdk\Config\Utils as UtilsConfig;
1113

@@ -71,6 +73,22 @@ public function track(string $key, string $trafficType, string $eventType, ?floa
7173
)->getSuccess();
7274
}
7375

76+
public function splitNames(): array
77+
{
78+
return Protocol\V1\SplitNamesResponse::fromRaw($this->rpcWithReconnect(RPC::forSplitNames()))->getSplitNames();
79+
}
80+
81+
public function split(string $splitName): ?SplitView
82+
{
83+
$view = Protocol\V1\SplitResponse::fromRaw($this->rpcWithReconnect(RPC::forSplit($splitName)))->getView();
84+
return self::splitResultToView($view);
85+
}
86+
87+
public function splits(): array
88+
{
89+
$views = Protocol\V1\SplitsResponse::fromRaw($this->rpcWithReconnect(RPC::forSplits()))->getViews();
90+
return array_map([self::class, 'splitResultToView'], $views);
91+
}
7492

7593
private function register(string $id, bool $impressionFeedback)
7694
{
@@ -100,4 +118,16 @@ private function performRPC(RPC $rpc): array
100118
$this->conn->sendMessage($this->serializer->serialize($rpc));
101119
return $this->serializer->deserialize($this->conn->readMessage());
102120
}
121+
122+
private static function splitResultToView(SplitViewResult $res): SplitView
123+
{
124+
return new SplitView(
125+
$res->getName(),
126+
$res->getTrafficType(),
127+
$res->getKilled(),
128+
$res->getTreatments(),
129+
$res->getChangeNumber(),
130+
$res->getConfigs()
131+
);
132+
}
103133
};

src/Link/Protocol/V1/OpCode.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ class OpCode extends Enum
2929

3030
private const Track = 0x80;
3131

32+
private const SplitNames = 0xA0;
33+
private const Split = 0xA1;
34+
private const Splits = 0xA2;
3235
}
33-

src/Link/Protocol/V1/RPC.php

+15
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ public static function forTrack(
9999
);
100100
}
101101

102+
public static function forSplitNames(): RPC
103+
{
104+
return new RPC(Version::V1(), OpCode::SplitNames(), []);
105+
}
106+
107+
public static function forSplit(string $splitName): RPC
108+
{
109+
return new RPC(Version::V1(), OpCode::Split(), [SplitArgs::SPLIT_NAME()->getValue() => $splitName]);
110+
}
111+
112+
public static function forSplits(): RPC
113+
{
114+
return new RPC(Version::V1(), OpCode::Splits(), []);
115+
}
116+
102117
function getSerializable() /* : mixed */
103118
{
104119
return array(

src/Link/Protocol/V1/SplitArgs.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace SplitIO\ThinSdk\Link\Protocol\V1;
4+
5+
/*
6+
enum TreatmentArgs: int
7+
{
8+
case SPLIT_SNAME = 0;
9+
}
10+
*/
11+
12+
use MyCLabs\Enum\Enum;
13+
14+
class SplitArgs extends Enum
15+
{
16+
private const SPLIT_NAME = 0;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace SplitIO\ThinSdk\Link\Protocol\V1;
4+
5+
use SplitIO\ThinSdk\Link\Protocol\V1\Result;
6+
use SplitIO\ThinSdk\Foundation\Lang\Enforce;
7+
8+
class SplitNamesResponse extends Response
9+
{
10+
11+
private /*array*/ $splitNames;
12+
13+
public function __construct(Result $status, array $splitNames)
14+
{
15+
parent::__construct($status);
16+
$this->splitNames = $splitNames;
17+
}
18+
19+
public function getSplitNames(): array
20+
{
21+
return $this->splitNames;
22+
}
23+
24+
public static function fromRaw(/*array*/$raw): SplitNamesResponse
25+
{
26+
if (!is_array($raw)) {
27+
throw new \InvalidArgumentException("SplitNamesResponse must be parsed from an array. Got a " . gettype($raw));
28+
}
29+
30+
$payload = Enforce::isArray($raw['p']);
31+
return new SplitNamesResponse(Result::from(Enforce::isInt($raw['s'])), Enforce::isArray($payload['n']));
32+
}
33+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace SplitIO\ThinSdk\Link\Protocol\V1;
4+
5+
use SplitIO\ThinSdk\Link\Protocol\V1\Result;
6+
use SplitIO\ThinSdk\Foundation\Lang\Enforce;
7+
8+
class SplitResponse extends Response
9+
{
10+
11+
private /*array*/ $splitView;
12+
13+
public function __construct(Result $status, ?SplitViewResult $splitView)
14+
{
15+
parent::__construct($status);
16+
$this->splitView = $splitView;
17+
}
18+
19+
public function getView(): ?SplitViewResult
20+
{
21+
return $this->splitView;
22+
}
23+
24+
public static function fromRaw(/*array*/$raw): SplitResponse
25+
{
26+
if (!is_array($raw)) {
27+
throw new \InvalidArgumentException("SplitNamesResponse must be parsed from an array. Got a " . gettype($raw));
28+
}
29+
30+
return new SplitResponse(Result::from(Enforce::isInt($raw['s'])), SplitViewResult::fromRaw(Enforce::isArray($raw['p'])));
31+
}
32+
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace SplitIO\ThinSdk\Link\Protocol\V1;
4+
5+
use SplitIO\ThinSdk\Foundation\Lang\Enforce;
6+
7+
class SplitViewResult
8+
{
9+
private /*string*/ $name;
10+
private /*string*/ $trafficType;
11+
private /*bool*/ $killed;
12+
private /*array*/ $treatments;
13+
private /*int*/ $changeNumber;
14+
private /*array*/ $configs;
15+
16+
public function __construct(string $name, string $trafficType, bool $killed, array $treatments, int $changeNumber, ?array $configs)
17+
{
18+
$this->name = $name;
19+
$this->trafficType = $trafficType;
20+
$this->killed = $killed;
21+
$this->treatments = $treatments;
22+
$this->changeNumber = $changeNumber;
23+
$this->configs = $configs;
24+
}
25+
26+
public function getName(): string
27+
{
28+
return $this->name;
29+
}
30+
31+
public function getTrafficType(): string
32+
{
33+
return $this->trafficType;
34+
}
35+
36+
public function getKilled(): bool
37+
{
38+
return $this->killed;
39+
}
40+
41+
public function getTreatments(): array
42+
{
43+
return $this->treatments;
44+
}
45+
46+
public function getChangeNumber(): int
47+
{
48+
return $this->changeNumber;
49+
}
50+
51+
public function getConfigs(): ?array
52+
{
53+
return $this->configs;
54+
}
55+
public static function fromRaw(array $raw): SplitViewResult
56+
{
57+
return new SplitViewResult(
58+
Enforce::isString($raw['n']),
59+
Enforce::isString($raw['t']),
60+
Enforce::isBool($raw['k']),
61+
Enforce::isArray($raw['s']),
62+
Enforce::isInt($raw['c']),
63+
isset($raw['f']) ? Enforce::isArray($raw['f']) : null
64+
);
65+
}
66+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace SplitIO\ThinSdk\Link\Protocol\V1;
4+
5+
use SplitIO\ThinSdk\Link\Protocol\V1\Result;
6+
use SplitIO\ThinSdk\Foundation\Lang\Enforce;
7+
8+
class SplitsResponse extends Response
9+
{
10+
11+
private /*array*/ $splitViews;
12+
13+
public function __construct(Result $status, array $splitViews)
14+
{
15+
parent::__construct($status);
16+
$this->splitViews = $splitViews;
17+
}
18+
19+
public function getViews(): array
20+
{
21+
return $this->splitViews;
22+
}
23+
24+
public static function fromRaw(/*array*/$raw): SplitsResponse
25+
{
26+
if (!is_array($raw)) {
27+
throw new \InvalidArgumentException("SplitNamesResponse must be parsed from an array. Got a " . gettype($raw));
28+
}
29+
30+
$payload = Enforce::isArray($raw['p']);
31+
return new SplitsResponse(
32+
Result::from(Enforce::isInt($raw['s'])),
33+
array_map(function ($e) {
34+
return SplitViewResult::fromRaw(Enforce::isArray($e));
35+
}, $payload['s'])
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)