Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
38 / 38 |
CurlRequestBuilder | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
38 / 38 |
__construct(SerializerInterface $serializer) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
build( ConfigurationInterface $configuration, PayURequestInterface $request ) | |
100.00% |
1 / 1 |
2 | |
100.00% |
20 / 20 |
|||
addHeaders( Request $curlRequest, ConfigurationInterface $configuration ) | |
100.00% |
1 / 1 |
1 | |
100.00% |
16 / 16 |
<?php | |
/** | |
* @author Krzysztof Gzocha <krzysztof.gzocha@xsolve.pl> | |
*/ | |
namespace Team3\PayU\Communication\CurlRequestBuilder; | |
use Buzz\Message\Request; | |
use Team3\PayU\Communication\Request\PayURequestInterface; | |
use Team3\PayU\Configuration\ConfigurationInterface; | |
use Team3\PayU\Serializer\SerializerInterface; | |
/** | |
* {@inheritdoc} | |
*/ | |
class CurlRequestBuilder implements CurlRequestBuilderInterface | |
{ | |
const CONTENT_TYPE = 'application/json'; | |
/** | |
* @var SerializerInterface | |
*/ | |
private $serializer; | |
/** | |
* @param SerializerInterface $serializer | |
*/ | |
public function __construct(SerializerInterface $serializer) | |
{ | |
$this->serializer = $serializer; | |
} | |
/** | |
* @param ConfigurationInterface $configuration | |
* @param PayURequestInterface $request | |
* | |
* @return Request | |
*/ | |
public function build( | |
ConfigurationInterface $configuration, | |
PayURequestInterface $request | |
) { | |
$curlRequest = new Request(); | |
if (PayURequestInterface::METHOD_POST === $request->getMethod()) { | |
$curlRequest->setContent( | |
$this->serializer->toJson($request->getDataObject()) | |
); | |
} | |
$curlRequest->setHost(sprintf( | |
'%s://%s/', | |
$configuration->getProtocol(), | |
$configuration->getDomain() | |
)); | |
$curlRequest->setResource(sprintf( | |
'%s/%s/%s', | |
$configuration->getPath(), | |
$configuration->getVersion(), | |
$request->getPath() | |
)); | |
$curlRequest->setMethod($request->getMethod()); | |
$this->addHeaders($curlRequest, $configuration); | |
return $curlRequest; | |
} | |
/** | |
* @param Request $curlRequest | |
* @param ConfigurationInterface $configuration | |
*/ | |
private function addHeaders( | |
Request $curlRequest, | |
ConfigurationInterface $configuration | |
) { | |
$authorization = sprintf( | |
'Basic %s', | |
base64_encode( | |
sprintf( | |
'%s:%s', | |
$configuration->getCredentials()->getMerchantPosId(), | |
$configuration->getCredentials()->getPrivateKey() | |
) | |
) | |
); | |
$curlRequest->addHeaders([ | |
'Authorization' => $authorization, | |
'Content-Type' => self::CONTENT_TYPE, | |
'Accept' => self::CONTENT_TYPE, | |
]); | |
} | |
} |