Passed
Pull Request — master (#61)
by
unknown
03:32
created

AdaptiveJsonLocation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A before() 0 6 2
A __construct() 0 3 1
A after() 0 12 3
1
<?php
2
3
namespace Acquia\Hmac\Client;
4
5
use GuzzleHttp\Command\Guzzle\Parameter;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Command\Guzzle\Parameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use GuzzleHttp\Command\Guzzle\ResponseLocation\JsonLocation;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Command\Guzzl...seLocation\JsonLocation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use GuzzleHttp\Command\Guzzle\ResponseLocation\ResponseLocationInterface;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Command\Guzzl...sponseLocationInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use GuzzleHttp\Command\ResultInterface;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Command\ResultInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use GuzzleHttp\Exception\InvalidArgumentException;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Class AdaptiveJsonLocation
14
 *
15
 * While it mostly deals in JSON responses, some APIs appear to deliver a few
16
 * non-JSON responses from certain endpoints under certain circumstances. For
17
 * example, Acquia Search "ping" endpoints tend to return XML, while the base
18
 * Controller Ping endpoint may simply return a string. This class acts as a
19
 * safety net; catching any exceptions that our parent JsonLocation might throw
20
 * during the course of a non-JSON response, and presenting the string response
21
 * from the underlying Guzzle Client.
22
 *
23
 * @todo
24
 *   Abandon this class and fallback to normal model-based deserialization when
25
 *   we can rely on more consistent, documented response types from Search API.
26
 */
27
class AdaptiveJsonLocation extends JsonLocation implements ResponseLocationInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function __construct($locationName = 'adaptivejson')
33
    {
34
        parent::__construct($locationName);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function before(ResultInterface $result, ResponseInterface $response, Parameter $model)
41
    {
42
        try {
43
            return parent::before($result, $response, $model);
44
        } catch (InvalidArgumentException $e) {
45
            return $result;
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function after(ResultInterface $result, ResponseInterface $response, Parameter $model)
53
    {
54
        try {
55
            // First, check for valid JSON. If we can return that, then great!
56
            $output = parent::after($result, $response, $model);
57
            $result['response'] = ($output === $result) ? (string) $response->getBody() : $output;
58
        } catch (InvalidArgumentException $e) {
59
            // If the endpoint we're dealing with returns something that isn't
60
            // valid JSON, pass it on under the "response" key.
61
            $result['response'] = (string) $response->getBody();
62
        }
63
        return $result;
64
    }
65
}
66