Skip to content

Commit

Permalink
Fixed REST server boot router
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdalpino committed Nov 17, 2019
1 parent b94721b commit 56e41ae
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,11 @@ public probabilities() : array
**Example:**

```php
use Rubix\Server\Responses\ProbasSampleResponse;
use Rubix\Server\Responses\ProbaSampleResponse;

// Obtain probabilities from model

$response = new ProbaampleResponse($probabilities);
$response = new ProbaSampleResponse($probabilities);
```

### Query Model Response
Expand Down
6 changes: 2 additions & 4 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ public function __construct(array $mapping)
{
foreach ($mapping as $command => $handler) {
if (!class_exists($command)) {
throw new InvalidArgumentException("$command does"
. ' not exist.');
throw new InvalidArgumentException("$command does not exist.");
}

if (!$handler instanceof Handler) {
throw new InvalidArgumentException('Command must map'
. ' to a handler, ' . get_class($handler)
. ' given.');
. ' to a handler, ' . get_class($handler) . ' given.');
}
}

Expand Down
57 changes: 28 additions & 29 deletions src/RESTServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,36 +290,35 @@ function ($group) use ($estimator, $bus) {
new PredictionsController($bus)
);

switch (true) {
case $estimator instanceof Learner:
$group->post(
self::PREDICT_SAMPLE_ENDPOINT,
new SamplePredictionController($bus)
);
if ($estimator instanceof Learner) {
$group->post(
self::PREDICT_SAMPLE_ENDPOINT,
new SamplePredictionController($bus)
);
}

// no break
case $estimator instanceof Probabilistic:
$group->post(
self::PROBA_ENDPOINT,
new ProbabilitiesController($bus)
);

$group->post(
self::PROBA_SAMPLE_ENDPOINT,
new SampleProbabilitiesController($bus)
);

// no break
case $estimator instanceof Ranking:
$group->post(
self::RANK_ENDPOINT,
new ScoresController($bus)
);

$group->post(
self::RANK_SAMPLE_ENDPOINT,
new SampleScoreController($bus)
);
if ($estimator instanceof Probabilistic) {
$group->post(
self::PROBA_ENDPOINT,
new ProbabilitiesController($bus)
);

$group->post(
self::PROBA_SAMPLE_ENDPOINT,
new SampleProbabilitiesController($bus)
);
}

if ($estimator instanceof Ranking) {
$group->post(
self::RANK_ENDPOINT,
new ScoresController($bus)
);

$group->post(
self::RANK_SAMPLE_ENDPOINT,
new SampleScoreController($bus)
);
}
}
);
Expand Down
3 changes: 1 addition & 2 deletions src/RPCClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ public function send(Command $command) : Response
$response = $this->serializer->unserialize($payload);

if (!$response instanceof Response) {
throw new RuntimeException('Response could not'
. ' be reconstituted.');
throw new RuntimeException('Message is not a valid response.');
}

return $response;
Expand Down
8 changes: 4 additions & 4 deletions src/Responses/ProbaSampleResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ public static function fromArray(array $data) : self
}

/**
* @param mixed $probabilities
* @param array $probabilities
*/
public function __construct($probabilities)
public function __construct(array $probabilities)
{
$this->probabilities = $probabilities;
}

/**
* Return the probabilities.
*
* @return mixed
* @return array
*/
public function probabilities()
public function probabilities() : array
{
return $this->probabilities;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Serializers/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public function unserialize(string $data) : Message
{
$json = json_decode($data, true);

if (isset($json['name']) and class_exists($json['name'])) {
return $json['name']::fromArray($json['data'] ?? []);
$class = $json['name'] ?? null;

if ($class and class_exists($class)) {
return $class::fromArray($json['data'] ?? []);
}

throw new RuntimeException('Message could not be'
. ' reconstituted.');
throw new RuntimeException('Message could not be reconstituted.');
}
}

0 comments on commit 56e41ae

Please sign in to comment.