Guzzle::sendRequest()   C
last analyzed

Complexity

Conditions 11
Paths 46

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 33
rs 5.2653
c 3
b 1
f 0
cc 11
eloc 19
nc 46
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Netgen\HtmlPdfApi\HttpClient;
4
5
use Netgen\HtmlPdfApi\HttpClientInterface;
6
use Guzzle\Service\Client;
7
use Guzzle\Common\Collection;
8
use Guzzle\Service\Description\ServiceDescription;
9
use Guzzle\Http\Exception\ClientErrorResponseException;
10
use Symfony\Component\Finder\Finder;
11
12
class Guzzle extends Client implements HttpClientInterface {
13
14
    /**
15
     * Constructor
16
     *
17
     * @param string $host          Base url of the api
18
     * @param string $token         Security token
19
     * @param string $json_location Full path to service.json file
20
     */
21
    public function __construct($host, $token, $json_location)
22
    {
23
        //fallback value
24
        $default = array(
25
            //'hostname' => '{host}',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
            //'host' => 'https://htmlpdfapi.com//api//v1//'
27
        );
28
        $config = array(
29
            'host' => $host,
30
            'token' => $token
31
        );
32
        $required = array('host', 'token');
33
        $config = Collection::fromConfig($config, $default, $required);
34
35
        parent::__construct($config['host']);
36
37
        $this->setDescription(ServiceDescription::factory($json_location));
38
39
        $header = array( 'Authentication' => 'Token' . " " . $config->get('token') );
40
        $this->setDefaultOption("headers", $header);
41
    }
42
43
    /**
44
     * Sends the request via Guzzle Client
45
     *
46
     * @param string $url       Relative url for the request
47
     * @param array $params     Parameters for the request
48
     * @param string $method    Method for the request
49
     * @return string           Response body
50
     * @throws \Exception
51
     */
52
    public function sendRequest($url, $params, $method)
53
    {
54
        try{
55
            if (!empty($params['html'])){
56
                $commandName = 'GenerateFromHTML';
57
            }
58
            else if (!empty($params['url'])){
59
                $commandName = 'GenerateFromURL';
60
            }
61
            else if (!empty($params['file'])&& $url=='pdf'){
62
                $commandName = 'GenerateFromFile';
63
            }
64
            else {
65
                $operations = $this->getDescription()->getOperations();
66
                foreach ($operations as $operation)
67
                {
68
                    if ($operation->getUri()==$url && $operation->getHttpMethod() == $method)
69
                    {
70
                        $commandName = $operation->getName();
71
                    }
72
                }
73
            }
74
            $command = $this->getCommand($commandName, $params);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $commandName does not seem to be defined for all execution paths leading up to this point.
Loading history...
75
            $ret = $this->execute($command);
76
77
            if ( $commandName == 'GetAssetList' || $commandName == 'UploadAsset' )
78
            {
79
                return json_encode($ret);
80
            }
81
            return $ret->getBody(true);
82
83
        }catch(ClientErrorResponseException $exception){
84
            throw new \Exception($exception->getResponse()->getBody(true), $exception->getResponse()->getStatusCode());
85
        }
86
    }
87
} 
88