Revert "Fix IHTTPResult::getHeader/s() - Split functionality "getHeader()" and "getHe...
[friendica.git/.git] / src / Network / GuzzleResponse.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Network;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Network\HTTPException\NotImplementedException;
27 use GuzzleHttp\Psr7\Response;
28 use Psr\Http\Message\ResponseInterface;
29
30 /**
31  * A content wrapper class for Guzzle call results
32  */
33 class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface
34 {
35         /** @var string The URL */
36         private $url;
37         /** @var boolean */
38         private $isTimeout;
39         /** @var boolean */
40         private $isSuccess;
41         /**
42          * @var int the error number or 0 (zero) if no error
43          */
44         private $errorNumber;
45
46         /**
47          * @var string the error message or '' (the empty string) if no
48          */
49         private $error;
50
51         public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
52         {
53                 parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase());
54                 $this->url         = $url;
55                 $this->error       = $error;
56                 $this->errorNumber = $errorNumber;
57
58                 $this->checkSuccess();
59         }
60
61         private function checkSuccess()
62         {
63                 $this->isSuccess = ($this->getStatusCode() >= 200 && $this->getStatusCode() <= 299) || $this->errorNumber == 0;
64
65                 // Everything higher or equal 400 is not a success
66                 if ($this->getReturnCode() >= 400) {
67                         $this->isSuccess = false;
68                 }
69
70                 if (!$this->isSuccess) {
71                         Logger::notice('http error', ['url' => $this->url, 'code' => $this->getReturnCode(), 'error'  => $this->error, 'callstack' => System::callstack(20)]);
72                         Logger::debug('debug', ['info' => $this->getHeaders()]);
73                 }
74
75                 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
76                         $this->isTimeout = true;
77                 } else {
78                         $this->isTimeout = false;
79                 }
80         }
81
82         /** {@inheritDoc} */
83         public function getReturnCode()
84         {
85                 return $this->getStatusCode();
86         }
87
88         /** {@inheritDoc} */
89         public function getContentType()
90         {
91                 return $this->getHeader('Content-Type');
92         }
93
94         /** {@inheritDoc} */
95         public function inHeader(string $field)
96         {
97                 return $this->hasHeader($field);
98         }
99
100         /** {@inheritDoc} */
101         public function getHeaderArray()
102         {
103                 return $this->getHeaders();
104         }
105
106         /** {@inheritDoc} */
107         public function isSuccess()
108         {
109                 return $this->isSuccess;
110         }
111
112         /** {@inheritDoc} */
113         public function getUrl()
114         {
115                 return $this->url;
116         }
117
118         /** {@inheritDoc} */
119         public function getRedirectUrl()
120         {
121                 return $this->url;
122         }
123
124         public function getInfo()
125         {
126                 // TODO: Implement getInfo() method.
127         }
128
129         /** {@inheritDoc} */
130         public function isRedirectUrl()
131         {
132                 throw new NotImplementedException();
133         }
134
135         /** {@inheritDoc} */
136         public function getErrorNumber()
137         {
138                 return $this->errorNumber;
139         }
140
141         /** {@inheritDoc} */
142         public function getError()
143         {
144                 return $this->error;
145         }
146
147         /** {@inheritDoc} */
148         public function isTimeout()
149         {
150                 return $this->isTimeout;
151         }
152 }