Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Aws\EndpointV2;
4
 
5
use Aws\EndpointV2\Ruleset\Ruleset;
6
use Aws\Exception\UnresolvedEndpointException;
7
use Aws\LruArrayCache;
8
 
9
/**
10
 * Given a service's Ruleset and client-provided input parameters, provides
11
 * either an object reflecting the properties of a resolved endpoint,
12
 * or throws an error.
13
 */
14
class EndpointProviderV2
15
{
16
    /** @var Ruleset */
17
    private $ruleset;
18
 
19
    /** @var LruArrayCache */
20
    private $cache;
21
 
22
    public function __construct(array $ruleset, array $partitions)
23
    {
24
        $this->ruleset = new Ruleset($ruleset, $partitions);
25
        $this->cache = new LruArrayCache(100);
26
    }
27
 
28
    /**
29
     * @return Ruleset
30
     */
31
    public function getRuleset()
32
    {
33
        return $this->ruleset;
34
    }
35
 
36
    /**
37
     * Given a Ruleset and input parameters, determines the correct endpoint
38
     * or an error to be thrown for a given request.
39
     *
40
     * @return RulesetEndpoint
41
     * @throws UnresolvedEndpointException
42
     */
43
    public function resolveEndpoint(array $inputParameters)
44
    {
45
        $hashedParams = $this->hashInputParameters($inputParameters);
46
        $match = $this->cache->get($hashedParams);
47
 
48
        if (!is_null($match)) {
49
            return $match;
50
        }
51
 
52
        $endpoint = $this->ruleset->evaluate($inputParameters);
53
        if ($endpoint === false) {
54
            throw new UnresolvedEndpointException(
55
                'Unable to resolve an endpoint using the provider arguments: '
56
                . json_encode($inputParameters)
57
            );
58
        }
59
        $this->cache->set($hashedParams, $endpoint);
60
 
61
        return $endpoint;
62
    }
63
 
64
    private function hashInputParameters($inputParameters)
65
    {
66
        return md5(serialize($inputParameters));
67
    }
68
}