| Conditions | 19 |
| Paths | 1 |
| Total Lines | 133 |
| Code Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 50 | public function __construct(array $config = [], int $retryLimit = 4, ?CacheInterface $cache = null) |
||
| 51 | { |
||
| 52 | $this->setRetryLimit($retryLimit); |
||
| 53 | $this->setCache($cache); |
||
| 54 | |||
| 55 | $handlerStack = HandlerStack::create(); |
||
| 56 | $handlerStack->unshift( |
||
| 57 | function (callable $handler) { |
||
| 58 | return function (RequestInterface $request, array $options) use ($handler) { |
||
| 59 | if (!isset($options[self::OPTION_HANDLER_RESPONSE])) { |
||
| 60 | return $handler($request, $options); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (!$options[self::OPTION_HANDLER_RESPONSE] instanceof ResponseHandlerInterface) { |
||
| 64 | throw new \RuntimeException("'" . self::OPTION_HANDLER_RESPONSE . "' option is not implements " . ResponseHandlerInterface::class); |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($this->cache !== null) { |
||
| 68 | if (!isset($options[self::OPTION_CACHE_KEY])) { |
||
| 69 | $func = $options[self::OPTION_HANDLER_RESPONSE]; |
||
| 70 | $ref = new \ReflectionClass($func); |
||
| 71 | if ($ref->isAnonymous()) { |
||
| 72 | static $hashes; |
||
| 73 | |||
| 74 | if ($hashes === null) { |
||
| 75 | $hashes = new \SplObjectStorage(); |
||
| 76 | } |
||
| 77 | if (!isset($hashes[$func])) { |
||
| 78 | try { |
||
| 79 | $file = new \SplFileObject($ref->getFileName()); |
||
| 80 | $file->seek($ref->getStartLine() - 1); |
||
| 81 | $content = ''; |
||
| 82 | while ($file->key() < $ref->getEndLine()) { |
||
| 83 | $content .= $file->current(); |
||
| 84 | $file->next(); |
||
| 85 | } |
||
| 86 | $hashes[$func] = $content; |
||
| 87 | } catch (\ReflectionException $e) { |
||
| 88 | throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | $handlerHash = (string)$hashes[$func]; |
||
| 92 | } else { |
||
| 93 | $handlerHash = $ref->getName(); |
||
| 94 | } |
||
| 95 | |||
| 96 | $options[self::OPTION_CACHE_KEY] = sprintf( |
||
| 97 | self::CACHE_KEY, |
||
| 98 | hash('crc32b', $handlerHash), |
||
| 99 | self::getRequestHash($request) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | $value = $this->cache->get($options[self::OPTION_CACHE_KEY]); |
||
| 104 | if ($value !== null) { |
||
| 105 | return $value; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | return $handler($request, $options) |
||
| 110 | ->then( |
||
| 111 | function (ResponseInterface $response) use ($options, $request) { |
||
| 112 | $result = call_user_func( |
||
| 113 | $options[self::OPTION_HANDLER_RESPONSE], |
||
| 114 | $request, |
||
| 115 | $response |
||
| 116 | ); |
||
| 117 | if ($this->cache !== null && $result !== null) { |
||
| 118 | $ttl = $options[self::OPTION_CACHE_TTL] ?? \DateInterval::createFromDateString('1 hour'); |
||
| 119 | $noCache = $options[self::OPTION_NO_CACHE] ?? false; |
||
| 120 | if (!$noCache) { |
||
| 121 | $this->cache->set( |
||
| 122 | $options[self::OPTION_CACHE_KEY], |
||
| 123 | $result, |
||
| 124 | $ttl |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | return $result; |
||
| 129 | } |
||
| 130 | ); |
||
| 131 | }; |
||
| 132 | } |
||
| 133 | ); |
||
| 134 | $handlerStack->push( |
||
| 135 | Middleware::retry( |
||
| 136 | function ( |
||
| 137 | $retries, |
||
| 138 | /** @noinspection PhpUnusedParameterInspection */ |
||
| 139 | RequestInterface $request, |
||
| 140 | ResponseInterface $response = null, |
||
| 141 | RequestException $exception = null |
||
| 142 | ) { |
||
| 143 | // retry decider |
||
| 144 | if ($retries >= $this->retryLimit) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | |||
| 148 | // Retry connection exceptions |
||
| 149 | if ($exception instanceof ConnectException) { |
||
| 150 | return true; |
||
| 151 | } |
||
| 152 | |||
| 153 | if ( |
||
| 154 | $response !== null && ( |
||
| 155 | $response->getStatusCode() !== 404 && |
||
| 156 | $response->getStatusCode() >= 400 |
||
| 157 | ) |
||
| 158 | ) { |
||
| 159 | return true; |
||
| 160 | } |
||
| 161 | return false; |
||
| 162 | }, |
||
| 163 | static function (int $numberOfRetries) { |
||
| 164 | // retry delay |
||
| 165 | return 1000 * $numberOfRetries; |
||
| 166 | } |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | |||
| 170 | $config = array_replace_recursive($config, [ |
||
| 171 | 'handler' => $handlerStack, |
||
| 172 | RequestOptions::TIMEOUT => 10.0, |
||
| 173 | RequestOptions::COOKIES => new CookieJar(), |
||
| 174 | RequestOptions::HEADERS => [ |
||
| 175 | 'Accept-Encoding' => 'gzip', |
||
| 176 | 'Accept-Language' => 'en', |
||
| 177 | 'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0', |
||
| 178 | 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
||
| 179 | 'Connection' => 'keep-alive', |
||
| 180 | ], |
||
| 181 | ]); |
||
| 182 | parent::__construct($config); |
||
| 183 | } |
||
| 335 |