HttpClientFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B createHttpClient() 0 48 6
1
<?php
2
3
namespace CodeCloud\Bundle\ShopifyBundle\Api;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\HandlerStack;
7
use GuzzleHttp\Middleware;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Creates authenticated clients for public and private apps.
14
 */
15
class HttpClientFactory implements HttpClientFactoryInterface
16
{
17
    public function createHttpClient($storeName, $credentials)
18
    {
19
        $handlers = HandlerStack::create();
20
        $handlers->push(Middleware::retry(
21
            function ($retries, RequestInterface $request, ResponseInterface $response = null, \Exception $error = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $error is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

21
            function ($retries, RequestInterface $request, ResponseInterface $response = null, /** @scrutinizer ignore-unused */ \Exception $error = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
23
                // todo rate limit by this
24
                //$response->getHeaderLine('X-Shopify-Shop-Api-Call-Limit');
25
                if ($response && $response->getStatusCode() == Response::HTTP_TOO_MANY_REQUESTS) {
26
                    return true;
27
                }
28
29
                return false;
30
            },
31
            function ($retries, ResponseInterface $response) {
32
                if (!$response->hasHeader('Retry-After')) {
33
                    return 1000;
34
                }
35
36
                dump((float) $response->getHeaderLine('Retry-After') * 1000);
0 ignored issues
show
Bug introduced by
The function dump was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
                /** @scrutinizer ignore-call */ 
37
                dump((float) $response->getHeaderLine('Retry-After') * 1000);
Loading history...
37
                die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
38
39
                return (float) $response->getHeaderLine('Retry-After') * 1000;
0 ignored issues
show
Unused Code introduced by
return (double)$response...e('Retry-After') * 1000 is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
40
            }
41
        ));
42
43
        $options = [
44
            'base_uri' => 'https://' . $storeName,
45
            //'handler' => $handlers,
46
        ];
47
48
        switch (true) {
49
            case  $credentials instanceof PublicAppCredentials:
50
                $options['headers'] = [
51
                    'X-Shopify-Access-Token' => $credentials->getAccessToken(),
52
                ];
53
                break;
54
            case $credentials instanceof PrivateAppCredentials:
55
                $options['auth'] = [
56
                    $credentials->getApiKey(),
57
                    $credentials->getPassword(),
58
                ];
59
                break;
60
            default:
61
                throw new \RuntimeException('Invalid credentials given');
62
        }
63
64
        return new Client($options);
65
    }
66
}
67