Overview

Namespaces

  • Net
    • Bazzline
      • Component
        • Curl
          • Builder
          • Dispatcher
          • HeaderLine
          • Option
            • Authentication
            • Authorization
            • Behaviour
            • Callback
            • Cookie
            • Ftp
            • Security
            • Transfer
          • Request
          • Response
          • ResponseBehaviour

Classes

  • Net\Bazzline\Component\Curl\Builder\Builder
  • Net\Bazzline\Component\Curl\Builder\BuilderFactory
  • Net\Bazzline\Component\Curl\Dispatcher\Dispatcher
  • Net\Bazzline\Component\Curl\Dispatcher\LoggingDispatcher
  • Net\Bazzline\Component\Curl\HeaderLine\AbstractContentType
  • Net\Bazzline\Component\Curl\HeaderLine\AbstractHeaderLine
  • Net\Bazzline\Component\Curl\HeaderLine\AcceptEncoding
  • Net\Bazzline\Component\Curl\HeaderLine\AcceptLanguage
  • Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsUtf8Form
  • Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsUtf8Html
  • Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsUtf8Json
  • Net\Bazzline\Component\Curl\HeaderLine\Custom
  • Net\Bazzline\Component\Curl\Option\AbstractAuthentication
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionArrayValue
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionClosureValue
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionIntValue
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionMixedValue
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionStreamValue
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionStringValue
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionToFalse
  • Net\Bazzline\Component\Curl\Option\AbstractSetOptionToTrue
  • Net\Bazzline\Component\Curl\Option\Authentication\EnableUnrestrictedAuth
  • Net\Bazzline\Component\Curl\Option\Authentication\SetBasicAuthentication
  • Net\Bazzline\Component\Curl\Option\Authentication\SetKeyPassword
  • Net\Bazzline\Component\Curl\Option\Authentication\SetUsernameAndPassword
  • Net\Bazzline\Component\Curl\Option\Authorization\EnableNetrc
  • Net\Bazzline\Component\Curl\Option\Behaviour\DisableBody
  • Net\Bazzline\Component\Curl\Option\Behaviour\DisableProgress
  • Net\Bazzline\Component\Curl\Option\Behaviour\DisableSignal
  • Net\Bazzline\Component\Curl\Option\Behaviour\EnableAutoReferer
  • Net\Bazzline\Component\Curl\Option\Behaviour\EnableFailOnError
  • Net\Bazzline\Component\Curl\Option\Behaviour\EnableFollowAllocation
  • Net\Bazzline\Component\Curl\Option\Behaviour\EnableForbidReuse
  • Net\Bazzline\Component\Curl\Option\Behaviour\EnableFreshConnect
  • Net\Bazzline\Component\Curl\Option\Behaviour\EnableMute
  • Net\Bazzline\Component\Curl\Option\Behaviour\EnableVerbose
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetConnectTimeOutInMilliSeconds
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetConnectTimeOutInSeconds
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetDnsCacheTimeout
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetHttp200Aliases
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetLowSpeedLimit
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetMaxConnects
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetMaxRedirs
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetRange
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetRedirProtocols
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetResumeFrom
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetStderr
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetTcpNoDelayMixed
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetTimeCondition
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetTimeOutInMilliSeconds
  • Net\Bazzline\Component\Curl\Option\Behaviour\SetTimeOutInSeconds
  • Net\Bazzline\Component\Curl\Option\Callback\SetCallbackForPassWordFunction
  • Net\Bazzline\Component\Curl\Option\Callback\SetCallbackForProgressFunction
  • Net\Bazzline\Component\Curl\Option\Callback\SetCallbackForReadFunction
  • Net\Bazzline\Component\Curl\Option\Callback\SetCallbackForWriteFunction
  • Net\Bazzline\Component\Curl\Option\Cookie\EnableCookieSession
  • Net\Bazzline\Component\Curl\Option\Cookie\SetCookie
  • Net\Bazzline\Component\Curl\Option\Cookie\SetCookieFile
  • Net\Bazzline\Component\Curl\Option\Cookie\SetCookieJar
  • Net\Bazzline\Component\Curl\Option\Ftp\EnableFtpAppend
  • Net\Bazzline\Component\Curl\Option\Ftp\EnableFtpAscii
  • Net\Bazzline\Component\Curl\Option\Ftp\EnableFtpCreateMissingDirs
  • Net\Bazzline\Component\Curl\Option\Ftp\EnableFtpListOnly
  • Net\Bazzline\Component\Curl\Option\Ftp\EnableFtpUseEprt
  • Net\Bazzline\Component\Curl\Option\Ftp\EnableFtpUseEpsv
  • Net\Bazzline\Component\Curl\Option\Ftp\SetFtpPort
  • Net\Bazzline\Component\Curl\Option\Ftp\SetFtpSslAuth
  • Net\Bazzline\Component\Curl\Option\Ftp\SetPostQuote
  • Net\Bazzline\Component\Curl\Option\Ftp\SetQuote
  • Net\Bazzline\Component\Curl\Option\Security\DisableSslVerifyHost
  • Net\Bazzline\Component\Curl\Option\Security\DisableSslVerifyPeer
  • Net\Bazzline\Component\Curl\Option\Security\EnableCertInfo
  • Net\Bazzline\Component\Curl\Option\Security\SetCaInfo
  • Net\Bazzline\Component\Curl\Option\Security\SetCaPath
  • Net\Bazzline\Component\Curl\Option\Security\SetSslCert
  • Net\Bazzline\Component\Curl\Option\Security\SetSslCertPasswd
  • Net\Bazzline\Component\Curl\Option\Security\SetSslCertType
  • Net\Bazzline\Component\Curl\Option\Security\SetSslCipherList
  • Net\Bazzline\Component\Curl\Option\Security\SetSslEngine
  • Net\Bazzline\Component\Curl\Option\Security\SetSslEngineDefault
  • Net\Bazzline\Component\Curl\Option\Security\SetSslKey
  • Net\Bazzline\Component\Curl\Option\Security\SetSslKeyPasswd
  • Net\Bazzline\Component\Curl\Option\Security\SetSslKeyType
  • Net\Bazzline\Component\Curl\Option\Security\SetSslVersion
  • Net\Bazzline\Component\Curl\Option\SetOption
  • Net\Bazzline\Component\Curl\Option\Transfer\DisableDnsUseGlobalCache
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableBinaryTransfer
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableCrlf
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableFileTime
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableHeader
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableHttpProxyTunnel
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableSafeUpload
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableTransferText
  • Net\Bazzline\Component\Curl\Option\Transfer\EnableUpload
  • Net\Bazzline\Component\Curl\Option\Transfer\SetBufferSize
  • Net\Bazzline\Component\Curl\Option\Transfer\SetEgdSocket
  • Net\Bazzline\Component\Curl\Option\Transfer\SetEncoding
  • Net\Bazzline\Component\Curl\Option\Transfer\SetFile
  • Net\Bazzline\Component\Curl\Option\Transfer\SetHttpVersion
  • Net\Bazzline\Component\Curl\Option\Transfer\SetInFile
  • Net\Bazzline\Component\Curl\Option\Transfer\SetInFileSize
  • Net\Bazzline\Component\Curl\Option\Transfer\SetInterface
  • Net\Bazzline\Component\Curl\Option\Transfer\SetPort
  • Net\Bazzline\Component\Curl\Option\Transfer\SetProxy
  • Net\Bazzline\Component\Curl\Option\Transfer\SetProxyAuth
  • Net\Bazzline\Component\Curl\Option\Transfer\SetProxyPort
  • Net\Bazzline\Component\Curl\Option\Transfer\SetProxyType
  • Net\Bazzline\Component\Curl\Option\Transfer\SetProxyUserPwd
  • Net\Bazzline\Component\Curl\Option\Transfer\SetRandomFile
  • Net\Bazzline\Component\Curl\Option\Transfer\SetReferer
  • Net\Bazzline\Component\Curl\Option\Transfer\SetTimeValue
  • Net\Bazzline\Component\Curl\Option\Transfer\SetUserAgent
  • Net\Bazzline\Component\Curl\Option\Transfer\SetWriteHeader
  • Net\Bazzline\Component\Curl\Request\Request
  • Net\Bazzline\Component\Curl\Request\RequestFactory
  • Net\Bazzline\Component\Curl\Response\Response
  • Net\Bazzline\Component\Curl\ResponseBehaviour\ConvertJsonToArrayBehaviour
  • Net\Bazzline\Component\Curl\ResponseBehaviour\ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviour

Interfaces

  • Net\Bazzline\Component\Curl\Dispatcher\DispatcherInterface
  • Net\Bazzline\Component\Curl\FactoryInterface
  • Net\Bazzline\Component\Curl\HeaderLine\HeaderLineInterface
  • Net\Bazzline\Component\Curl\Option\OptionInterface
  • Net\Bazzline\Component\Curl\ResponseBehaviour\ResponseBehaviourInterface
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2015-12-08
 */
namespace Net\Bazzline\Component\Curl\Builder;

use Exception;
use Net\Bazzline\Component\Curl\HeaderLine\HeaderLineInterface;
use Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsUtf8Json;
use Net\Bazzline\Component\Curl\Option\OptionInterface;
use Net\Bazzline\Component\Curl\Request\Request;
use Net\Bazzline\Component\Curl\Response\Response;
use Net\Bazzline\Component\Curl\ResponseBehaviour\ConvertJsonToArrayBehaviour;
use Net\Bazzline\Component\Curl\ResponseBehaviour\ResponseBehaviourInterface;
use Net\Bazzline\Component\Toolbox\HashMap\Merge;
use RuntimeException;

class Builder
{
    const METHOD_DELETE = 0;
    const METHOD_GET    = 1;
    const METHOD_PATCH  = 2;
    const METHOD_POST   = 3;
    const METHOD_PUT    = 4;

    /** @var boolean */
    private $asJson;

    /** @var null|string|array */
    private $data;

    /** @var array|ResponseBehaviourInterface[] */
    private $defaultResponseBehaviours;

    /** @var Merge */
    private $merge;

    /** @var int */
    private $method;

    /** @var array */
    private $parameters;

    /** @var Request */
    private $request;

    /** @var array|ResponseBehaviourInterface[] */
    private $responseBehaviours;

    /** @var string */
    private $url;

    /**
     * @param Request $request
     * @param Merge $merge
     * @param array|ResponseBehaviourInterface[] $defaultResponseBehaviours
     */
    public function __construct(Request $request, Merge $merge, array $defaultResponseBehaviours = [])
    {
        $this->defaultResponseBehaviours    = $defaultResponseBehaviours;
        $this->merge                        = $merge;
        $this->request                      = $request;
        $this->reset();
    }

    /**
     * @return Response
     * @throws Exception|RuntimeException
     */
    public function andFetchTheResponse()
    {
        //begin of dependencies
        $asJson     = $this->asJson;
        $merge      = $this->merge;
        $data       = $this->data;
        $method     = $this->method;
        $parameters = $this->parameters;
        $request    = $this->request;
        $url        = $this->url;
        //end of dependencies

        //begin of business logic
        /** @var ResponseBehaviourInterface[] $behaviours */
        $behaviours = $merge($this->responseBehaviours, $this->defaultResponseBehaviours);
        $data       = $this->convertToJsonIfNeeded($data, $asJson);
        $response   = $this->fetchResponseFromRequestOrThrowRuntimeException(
            $method,
            $request,
            $url,
            $parameters,
            $data
        );
        $response   = $this->applyBehaviours($behaviours, $response);
        //end of business logic

        return $response;
    }

    /**
     * @return $this
     */
    public function asJson()
    {
        $this->asJson = true;
        $this->request->addHeaderLine(new ContentTypeIsUtf8Json());
        $this->responseBehaviours[] = new ConvertJsonToArrayBehaviour();

        return $this;
    }

    /**
     * @param bool $alsoTheDefaults
     * @return $this
     */
    public function reset($alsoTheDefaults = false)
    {
        $this->asJson               = false;
        $this->data                 = null;
        $this->parameters           = [];
        $this->request->reset($alsoTheDefaults);
        $this->responseBehaviours   = [];
        $this->url                  = null;

        return $this;
    }

    /**
     * @param string $url
     * @return $this
     */
    public function onTheUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * @param null|string|array $data
     * @return $this
     */
    public function withTheData($data)
    {
        $this->data = $data;

        return $this;
    }

    /**
     * @param HeaderLineInterface $line
     * @return $this
     */
    public function withTheHeaderLine(HeaderLineInterface $line)
    {
        $this->request->addHeaderLine($line);

        return $this;
    }

    /**
     * @param OptionInterface $option
     * @return $this
     */
    public function withTheOption(OptionInterface $option)
    {
        $this->request->addOption($option);

        return $this;
    }

    /**
     * @param array $parameters
     * @return $this
     */
    public function withTheParameters(array $parameters)
    {
        $this->parameters = $parameters;

        return $this;
    }

    /**
     * @param string $line
     * @return $this
     */
    public function withTheRawHeaderLine($line)
    {
        $this->request->addRawHeaderLine($line);

        return $this;
    }

    /**
     * @param string $key
     * @param string $value
     * @return $this
     */
    public function withTheRawOption($key, $value)
    {
        $this->request->addRawOption($key, $value);

        return $this;
    }

    /**
     * @param ResponseBehaviourInterface $behaviour
     * @return $this
     */
    public function withTheResponseBehaviour(ResponseBehaviourInterface $behaviour)
    {
        $this->responseBehaviours[] = $behaviour;

        return $this;
    }

    //@todo better naming?
    //  callDelete
    /**
     * @return $this
     */
    public function useDelete()
    {
        $this->method = self::METHOD_DELETE;

        return $this;
    }

    /**
     * @return $this
     */
    public function useGet()
    {
        $this->method = self::METHOD_GET;

        return $this;
    }

    /**
     * @return $this
     */
    public function usePatch()
    {
        $this->method = self::METHOD_PATCH;

        return $this;
    }

    /**
     * @return $this
     */
    public function usePost()
    {
        $this->method = self::METHOD_POST;

        return $this;
    }

    /**
     * @return $this
     */
    public function usePut()
    {
        $this->method = self::METHOD_PUT;

        return $this;
    }

    /**
     * @param array|ResponseBehaviourInterface[] $behaviours
     * @param Response $response
     * @return Response
     */
    private function applyBehaviours(array $behaviours, Response $response)
    {
        foreach ($behaviours as $behaviour) {
            $response = $behaviour->behave($response);
        }

        return $response;
    }

    /**
     * @param mixed $data
     * @param boolean $convertIt
     * @return mixed
     */
    private function convertToJsonIfNeeded($data, $convertIt)
    {
        return ($convertIt) ? json_encode($data) : $data;
    }

    /**
     * @param string $method
     * @param Request $request
     * @param string $url
     * @param array $parameters
     * @param mixed $data
     * @return Response
     * @throws RuntimeException
     */
    private function fetchResponseFromRequestOrThrowRuntimeException($method, Request $request, $url, array $parameters, $data)
    {
        switch ($method) {
            case self::METHOD_DELETE:
                $response = $request->delete($url, $parameters, $data);
                break;
            case self::METHOD_GET:
                $response = $request->get($url, $parameters);
                break;
            case self::METHOD_PATCH:
                $response = $request->patch($url, $parameters, $data);
                break;
            case self::METHOD_POST:
                $response = $request->post($url, $parameters, $data);
                break;
            case self::METHOD_PUT:
                $response = $request->put($url, $parameters, $data);
                break;
            default:
                throw new RuntimeException(
                    'no http method set'
                );
        }

        return $response;
    }
}
PHP Curl Component by bazzline.net API documentation generated by ApiGen