AbstractScaleEngineResponse::getResult()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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