Issues (6)

HttpClient/Guzzle.php (1 issue)

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}',
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