Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

UriGenerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 121
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 6 1
A addTransformer() 0 5 1
A addDecorator() 0 5 1
A setRequest() 0 5 1
A fetchUrl() 0 14 3
A decorate() 0 6 2
A setContext() 0 6 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Service;
11
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\UriInterface;
14
use Slick\Mvc\Service\UriGenerator\LocationTransformerInterface;
15
use Slick\Mvc\Service\UriGenerator\UriDecoratorInterface;
16
17
/**
18
 * Uri Generator
19
 *
20
 * @package Slick\Mvc\Service
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class UriGenerator implements UriGeneratorInterface
24
{
25
26
    /**
27
     * @var LocationTransformerInterface[]
28
     */
29
    private $transformers = [];
30
31
    /**
32
     * @var UriDecoratorInterface[]
33
     */
34
    private $decorators = [];
35
36
    /**
37
     * @var ServerRequestInterface
38
     */
39
    private $request;
40
41
    /**
42
     * Generates an URL location from provided data
43
     *
44
     * @param string $location Location name, path or identifier
45
     * @param array $options Filter options
46
     *
47
     * @return UriInterface|null
48
     */
49
    public function generate($location, array $options = [])
50
    {
51
        $uri = $this->fetchUrl($location, $options);
52
        $this->decorate($uri);
53
        return $uri;
54
    }
55
56
    /**
57
     * Adds a location transformer to the transformers stack
58
     *
59
     * @param LocationTransformerInterface $transformer
60
     *
61
     * @return self|UriGeneratorInterface
62
     */
63
    public function addTransformer(LocationTransformerInterface $transformer)
64
    {
65
        array_push($this->transformers, $transformer);
66
        return $this;
67
    }
68
69
    /**
70
     * Adds an URI decorator to the decorators list
71
     *
72
     * @param UriDecoratorInterface $decorator
73
     * @return mixed
74
     */
75
    public function addDecorator(UriDecoratorInterface $decorator)
76
    {
77
        array_push($this->decorators, $decorator);
78
        return $this;
79
    }
80
81
    /**
82
     * Set the context HTTP request
83
     *
84
     * @param ServerRequestInterface $request
85
     *
86
     * @return self|UriGeneratorInterface
87
     */
88
    public function setRequest(ServerRequestInterface $request)
89
    {
90
        $this->request = $request;
91
        return $this;
92
    }
93
94
    /**
95
     * Fetch the URI form the list of transformers
96
     *
97
     * The URI is the result of the first transformer that returns a
98
     * UriInterface object.
99
     *
100
     * @param string $location Location name, path or identifier
101
     * @param array $options   Filter options
102
     *
103
     * @return null|UriInterface
104
     */
105
    private function fetchUrl($location, array $options = [])
106
    {
107
        $uri = null;
108
        foreach ($this->transformers as $transformer) {
109
            $this->setContext($transformer);
110
            $object = $transformer->transform($location, $options);
111
112
            if ($object instanceof UriInterface) {
113
                $uri = $object;
114
                break;
115
            }
116
        }
117
        return $uri;
118
    }
119
120
    /**
121
     * Applies decorators to the provided URI
122
     *
123
     * @param UriInterface|null $uri
124
     */
125
    private function decorate(UriInterface $uri = null)
126
    {
127
        foreach ($this->decorators as $decorator) {
128
            $decorator->decorate($uri);
129
        }
130
    }
131
132
    /**
133
     * Sets the context HTTP request to provided transformer
134
     *
135
     * @param LocationTransformerInterface $transformer
136
     */
137
    private function setContext(LocationTransformerInterface $transformer)
138
    {
139
        if ($this->request) {
140
            $transformer->setRequest($this->request);
141
        }
142
    }
143
}