Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws;
3
 
4
use Aws\Exception\AwsException;
5
use GuzzleHttp\Promise;
6
use GuzzleHttp\Promise\RejectedPromise;
7
use Psr\Http\Message\RequestInterface;
8
use Exception;
9
 
10
/**
11
 * Returns promises that are rejected or fulfilled using a queue of
12
 * Aws\ResultInterface and Aws\Exception\AwsException objects.
13
 */
14
class MockHandler implements \Countable
15
{
16
    private $queue;
17
    private $lastCommand;
18
    private $lastRequest;
19
    private $onFulfilled;
20
    private $onRejected;
21
 
22
    /**
23
     * The passed in value must be an array of {@see Aws\ResultInterface} or
24
     * {@see AwsException} objects that acts as a queue of results or
25
     * exceptions to return each time the handler is invoked.
26
     *
27
     * @param array    $resultOrQueue
28
     * @param callable $onFulfilled Callback to invoke when the return value is fulfilled.
29
     * @param callable $onRejected  Callback to invoke when the return value is rejected.
30
     */
31
    public function __construct(
32
        array $resultOrQueue = [],
33
        callable $onFulfilled = null,
34
        callable $onRejected = null
35
    ) {
36
        $this->queue = [];
37
        $this->onFulfilled = $onFulfilled;
38
        $this->onRejected = $onRejected;
39
 
40
        if ($resultOrQueue) {
41
            call_user_func_array([$this, 'append'], array_values($resultOrQueue));
42
        }
43
    }
44
 
45
    /**
46
     * Adds one or more variadic ResultInterface or AwsException objects to the
47
     * queue.
48
     */
49
    public function append()
50
    {
51
        foreach (func_get_args() as $value) {
52
            if ($value instanceof ResultInterface
53
                || $value instanceof Exception
54
                || is_callable($value)
55
            ) {
56
                $this->queue[] = $value;
57
            } else {
58
                throw new \InvalidArgumentException('Expected an Aws\ResultInterface or Exception.');
59
            }
60
        }
61
    }
62
 
63
    /**
64
     * Adds one or more \Exception or \Throwable to the queue
65
     */
66
    public function appendException()
67
    {
68
        foreach (func_get_args() as $value) {
69
            if ($value instanceof \Exception || $value instanceof \Throwable) {
70
                $this->queue[] = $value;
71
            } else {
72
                throw new \InvalidArgumentException('Expected an \Exception or \Throwable.');
73
            }
74
        }
75
    }
76
 
77
    public function __invoke(
78
        CommandInterface $command,
79
        RequestInterface $request
80
    ) {
81
        if (!$this->queue) {
82
            $last = $this->lastCommand
83
                ? ' The last command sent was ' . $this->lastCommand->getName() . '.'
84
                : '';
85
            throw new \RuntimeException('Mock queue is empty. Trying to send a '
86
                . $command->getName() . ' command failed.' . $last);
87
        }
88
 
89
        $this->lastCommand = $command;
90
        $this->lastRequest = $request;
91
 
92
        $result = array_shift($this->queue);
93
 
94
        if (is_callable($result)) {
95
            $result = $result($command, $request);
96
        }
97
 
98
        if ($result instanceof \Exception) {
99
            $result = new RejectedPromise($result);
100
        } else {
101
            // Add an effective URI and statusCode if not present.
102
            $meta = $result['@metadata'];
103
            if (!isset($meta['effectiveUri'])) {
104
                $meta['effectiveUri'] = (string) $request->getUri();
105
            }
106
            if (!isset($meta['statusCode'])) {
107
                $meta['statusCode'] = 200;
108
            }
109
            $result['@metadata'] = $meta;
110
            $result = Promise\Create::promiseFor($result);
111
        }
112
 
113
        $result->then($this->onFulfilled, $this->onRejected);
114
 
115
        return $result;
116
    }
117
 
118
    /**
119
     * Get the last received request.
120
     *
121
     * @return RequestInterface
122
     */
123
    public function getLastRequest()
124
    {
125
        return $this->lastRequest;
126
    }
127
 
128
    /**
129
     * Get the last received command.
130
     *
131
     * @return CommandInterface
132
     */
133
    public function getLastCommand()
134
    {
135
        return $this->lastCommand;
136
    }
137
 
138
    /**
139
     * Returns the number of remaining items in the queue.
140
     *
141
     * @return int
142
     */
143
    #[\ReturnTypeWillChange]
144
    public function count()
145
    {
146
        return count($this->queue);
147
    }
148
}