AbstractScaleEngineResponse   A
last analyzed

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