Completed
Pull Request — master (#1)
by Spencer
02:19
created

AbstractScaleEngineResponse   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 9 2
1
<?php
2
namespace FloSports\ScaleEngine\Response;
3
4
use Exception;
5
use Guzzle\Common\Collection;
6
use Guzzle\Service\Command\OperationCommand;
7
use Guzzle\Service\Command\ResponseClassInterface;
8
9
/**
10
 * A base ScaleEngine API response class.
11
 *
12
 * This provides ease of access to shared functionality among all ScaleEngine
13
 * responses.
14
 */
15
abstract class AbstractScaleEngineResponse implements ResponseClassInterface
16
{
17
    /**
18
     * Get the result of a command, throwing an error if it failed.
19
     *
20
     * All ScaleEngine API calls have a top-level `status` field that will be
21
     * `success` for successful replies.  This will throw an exception if the
22
     * reply is not successful.
23
     *
24
     * @param OperationCommand $command The API call being executed.
25
     * @param string|array $resultPath The path to the actual result model.  If
26
     *     not provided, this will return the entire JSON response.
27
     * @return array The top-level result JSON from the API call.
28
     * @throws Exception if the result was not successful.
29
     */
30
    public static function getResult(OperationCommand $command, $resultPath = [])
31
    {
32
        $result = new Collection($command->getResponse()->json());
33
        if ($result['status'] !== 'success') {
34
            throw new Exception("Unsuccessful response from ScaleEngine: {$result['code']}: {$result['message']}");
35
        }
36
37
        return $result->getPath($resultPath);
38
    }
39
}
40