Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
27 / 27
RequestProcess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
27 / 27
 __construct( ResponseDeserializerInterface $responseDeserializer, ClientInterface $client, ValidatorInterface $validator, HttpStatusParserInterface $httpStatusParser )
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 process( PayURequestInterface $payURequest, ConfigurationInterface $configuration )
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 addResponse(ResponseInterface $response)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 disableValidation()
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 validate(SerializableInterface $object)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
11 / 11
<?php
/**
 * @author Krzysztof Gzocha <krzysztof.gzocha@xsolve.pl>
 */
namespace Team3\PayU\Communication\Process;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Team3\PayU\Communication\ClientInterface;
use Team3\PayU\Communication\HttpStatusParser\HttpStatusParser;
use Team3\PayU\Communication\HttpStatusParser\HttpStatusParserException;
use Team3\PayU\Communication\HttpStatusParser\HttpStatusParserInterface;
use Team3\PayU\Communication\Process\ResponseDeserializer\ResponseDeserializerInterface;
use Team3\PayU\Communication\Request\PayURequestInterface;
use Team3\PayU\Communication\Response\ResponseInterface;
use Team3\PayU\Configuration\ConfigurationInterface;
use Team3\PayU\Serializer\SerializableInterface;
/**
 * This class will help user to make a request to PayU.
 * Basing on {@link PayURequestInterface} and {@link ConfigurationInterface}
 * it will serialize objects, send request, parse HTTP status code of the response
 * and deserialize the response into proper objects.
 *
 * Class RequestProcess
 * @package Team3\PayU\Communication\Process
 */
class RequestProcess implements RequestProcessInterface
{
    /**
     * @var ResponseDeserializerInterface
     */
    private $responseDeserializer;
    /**
     * @var ClientInterface
     */
    private $client;
    /**
     * @var ValidatorInterface
     */
    private $validator;
    /**
     * @var HttpStatusParserInterface
     */
    private $httpStatusParser;
    /**
     * @var bool
     */
    private $shouldValidate;
    /**
     * @param ResponseDeserializerInterface $responseDeserializer
     * @param ClientInterface               $client
     * @param ValidatorInterface            $validator
     * @param HttpStatusParserInterface     $httpStatusParser
     */
    public function __construct(
        ResponseDeserializerInterface $responseDeserializer,
        ClientInterface $client,
        ValidatorInterface $validator,
        HttpStatusParserInterface $httpStatusParser
    ) {
        $this->responseDeserializer = $responseDeserializer;
        $this->client = $client;
        $this->validator = $validator;
        $this->httpStatusParser = $httpStatusParser;
        $this->shouldValidate = true;
    }
    /**
     * @param PayURequestInterface   $payURequest
     * @param ConfigurationInterface $configuration
     *
     * @return object
     * @throws HttpStatusParserException         when status code is not 200
     * @throws InvalidRequestDataObjectException when request data object is invalid
     */
    public function process(
        PayURequestInterface $payURequest,
        ConfigurationInterface $configuration
    ) {
        if ($this->shouldValidate) {
            $this->validate($payURequest->getDataObject());
        }
        $curlResponse = $this->client->sendRequest($configuration, $payURequest);
        $this->httpStatusParser->parse($curlResponse);
        return $this->responseDeserializer->deserializeResponse($curlResponse, $payURequest);
    }
    /**
     * @param ResponseInterface $response
     *
     * @return $this
     */
    public function addResponse(ResponseInterface $response)
    {
        $this->responseDeserializer->addResponse($response);
        return $this;
    }
    /**
     * @return $this
     */
    public function disableValidation()
    {
        $this->shouldValidate = false;
        return $this;
    }
    /**
     * @param SerializableInterface $object
     *
     * @throws InvalidRequestDataObjectException
     */
    private function validate(SerializableInterface $object)
    {
        $violations = $this->validator->validate($object);
        if (0 < $violations->count()) {
            throw new InvalidRequestDataObjectException(
                $violations,
                sprintf(
                    'Given object %s in PayU request is invalid. %d violations are in this exception.',
                    get_class($object),
                    $violations->count()
                )
            );
        }
    }
}