Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\S3;
3
 
4
use Aws\CommandInterface;
5
use Aws\ResultInterface;
6
use Aws\S3\Exception\PermanentRedirectException;
7
use Psr\Http\Message\RequestInterface;
8
 
9
/**
10
 * Throws a PermanentRedirectException exception when a 301 redirect is
11
 * encountered.
12
 *
13
 * @internal
14
 */
15
class PermanentRedirectMiddleware
16
{
17
    /** @var callable  */
18
    private $nextHandler;
19
 
20
    /**
21
     * Create a middleware wrapper function.
22
     *
23
     * @return callable
24
     */
25
    public static function wrap()
26
    {
27
        return function (callable $handler) {
28
            return new self($handler);
29
        };
30
    }
31
 
32
    /**
33
     * @param callable $nextHandler Next handler to invoke.
34
     */
35
    public function __construct(callable $nextHandler)
36
    {
37
        $this->nextHandler = $nextHandler;
38
    }
39
 
40
    public function __invoke(CommandInterface $command, RequestInterface $request = null)
41
    {
42
        $next = $this->nextHandler;
43
        return $next($command, $request)->then(
44
            function (ResultInterface $result) use ($command) {
45
                $status = isset($result['@metadata']['statusCode'])
46
                    ? $result['@metadata']['statusCode']
47
                    : null;
48
                if ($status == 301) {
49
                    throw new PermanentRedirectException(
50
                        'Encountered a permanent redirect while requesting '
51
                        . $result->search('"@metadata".effectiveUri') . '. '
52
                        . 'Are you sure you are using the correct region for '
53
                        . 'this bucket?',
54
                        $command,
55
                        ['result' => $result]
56
                    );
57
                }
58
                return $result;
59
            }
60
        );
61
    }
62
}