EndpointFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Mediapart LaPresseLibre Bundle.
5
 *
6
 * CC BY-NC-SA <https://github.com/mediapart/lapresselibre-bundle>
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mediapart\Bundle\LaPresseLibreBundle\Factory;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Mediapart\LaPresseLibre\Endpoint;
17
18
class EndpointFactory
19
{
20
    /**
21
     * @var Array
22
     */
23
    private $endpoints = [];
24
25
    /**
26
     * @param object|string $service
27
     * @param array $attributes
28
     *
29
     * - method    : method that will be called on $service
30
     * - route     : route of the endpoint
31
     * - operation : what will be executed
32
     *
33
     * @return void
34
     */
35
    public function register($service, array $attributes)
36
    {
37
        $attributes = $this->resolve($attributes);
38
39
        $route    = $attributes['route'];
40
        $callback = [$service, $attributes['method']];
41
        $endpoint = [$attributes['operation'], $callback];
42
43
        $this->endpoints[$route] = $endpoint;
44
    }
45
46
    /**
47
     * @param string $route
48
     *
49
     * @return Endpoint
50
     */
51
    public function create($route)
52
    {
53
        list($class, $callback) = $this->endpoints[$route];
54
55
        return Endpoint::answer($class, $callback);
56
    }
57
58
    /**
59
     * @param array $attributes
60
     *
61
     * @return array
62
     */
63
    private function resolve(array $attributes)
64
    {
65
        $resolver = new OptionsResolver();
66
        $resolver->setRequired(['method', 'route', 'operation']);
67
        $resolver->setAllowedValues('operation', Endpoint::all());
68
69
        return $resolver->resolve($attributes);
70
    }
71
}
72