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

AbstractScaleEngineResponse::getResult()   A

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
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