Proyectos de Subversion Moodle

Rev

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