1 <?php
2 3 4
5 namespace Team3\PayU\Communication\Process;
6
7 use Symfony\Component\Validator\Validator\ValidatorInterface;
8 use Team3\PayU\Communication\ClientInterface;
9 use Team3\PayU\Communication\HttpStatusParser\HttpStatusParser;
10 use Team3\PayU\Communication\HttpStatusParser\HttpStatusParserException;
11 use Team3\PayU\Communication\HttpStatusParser\HttpStatusParserInterface;
12 use Team3\PayU\Communication\Process\ResponseDeserializer\ResponseDeserializerInterface;
13 use Team3\PayU\Communication\Request\PayURequestInterface;
14 use Team3\PayU\Communication\Response\ResponseInterface;
15 use Team3\PayU\Configuration\ConfigurationInterface;
16 use Team3\PayU\Serializer\SerializableInterface;
17
18 19 20 21 22 23 24 25 26
27 class RequestProcess implements RequestProcessInterface
28 {
29 30 31
32 private $responseDeserializer;
33
34 35 36
37 private $client;
38
39 40 41
42 private $validator;
43
44 45 46
47 private $httpStatusParser;
48
49 50 51
52 private $shouldValidate;
53
54 55 56 57 58 59
60 public function __construct(
61 ResponseDeserializerInterface $responseDeserializer,
62 ClientInterface $client,
63 ValidatorInterface $validator,
64 HttpStatusParserInterface $httpStatusParser
65 ) {
66 $this->responseDeserializer = $responseDeserializer;
67 $this->client = $client;
68 $this->validator = $validator;
69 $this->httpStatusParser = $httpStatusParser;
70 $this->shouldValidate = true;
71 }
72
73 74 75 76 77 78 79 80
81 public function process(
82 PayURequestInterface $payURequest,
83 ConfigurationInterface $configuration
84 ) {
85 if ($this->shouldValidate) {
86 $this->validate($payURequest->getDataObject());
87 }
88
89 $curlResponse = $this->client->sendRequest($configuration, $payURequest);
90 $this->httpStatusParser->parse($curlResponse);
91
92 return $this->responseDeserializer->deserializeResponse($curlResponse, $payURequest);
93 }
94
95 96 97 98 99
100 public function addResponse(ResponseInterface $response)
101 {
102 $this->responseDeserializer->addResponse($response);
103
104 return $this;
105 }
106
107 108 109
110 public function disableValidation()
111 {
112 $this->shouldValidate = false;
113
114 return $this;
115 }
116
117 118 119 120 121
122 private function validate(SerializableInterface $object)
123 {
124 $violations = $this->validator->validate($object);
125
126 if (0 < $violations->count()) {
127 throw new InvalidRequestDataObjectException(
128 $violations,
129 sprintf(
130 'Given object %s in PayU request is invalid. %d violations are in this exception.',
131 get_class($object),
132 $violations->count()
133 )
134 );
135 }
136 }
137 }
138