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

AbstractLocationTransformer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequest() 0 5 1
transform() 0 1 ?
A updateOptions() 0 13 2
B updateHostName() 0 23 5
1
<?php
2
3
/**
4
 * This file is part of slick/mvc
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\UriGenerator\Transformer;
11
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\UriInterface;
14
use Slick\Mvc\Service\UriGenerator\LocationTransformerInterface;
15
16
/**
17
 * Abstract Location Transformer
18
 *
19
 * @package Slick\Mvc\Service\UriGenerator\Transformer
20
 * @author  Filipe Silva <[email protected]>
21
 */
22
abstract class AbstractLocationTransformer implements
23
    LocationTransformerInterface
24
{
25
26
    /**
27
     * @var ServerRequestInterface
28
     */
29
    protected $request;
30
31
    /**
32
     * @var array
33
     */
34
    protected $options = [
35
        'query' => [],
36
        'reuseHostName' => false,
37
        'reuseParams' => false
38
    ];
39
40
    /**
41
     * Set the context HTTP request
42
     *
43
     * @param ServerRequestInterface $request
44
     *
45
     * @return BasePathTransformer|LocationTransformerInterface
46
     */
47
    public function setRequest(ServerRequestInterface $request)
48
    {
49
        $this->request = $request;
50
        return $this;
51
    }
52
53
    /**
54
     * Tries to transform the provided location data into a server URI
55
     *
56
     * @param string $location Location name, path or identifier
57
     * @param array $options Filter options
58
     *
59
     * @return UriInterface|null
60
     */
61
    abstract public function transform($location, array $options = []);
62
63
    /**
64
     * Updates the URI according to existing options
65
     *
66
     * @param UriInterface $uri
67
     *
68
     * @return UriInterface
69
     */
70
    protected function updateOptions(UriInterface $uri)
71
    {
72
        $query = $this->options['query'];
73
        if ($this->options['reuseParams']) {
74
            $query = array_merge(
75
                $query,
76
                $this->request->getQueryParams()
77
            );
78
        }
79
        $queryString = http_build_query($query);
80
        $uri = $uri->withQuery($queryString);
81
        return $this->updateHostName($uri);
82
    }
83
84
    /**
85
     * Adds the scheme, host name and port to the provided URI
86
     *
87
     * @param UriInterface $uri
88
     *
89
     * @return UriInterface
90
     */
91
    private function updateHostName(UriInterface $uri)
92
    {
93
        if (!$this->options['reuseHostName']) {
94
            return $uri;
95
        }
96
97
        $params = $this->request->getServerParams();
98
99
        $hasPort = !in_array($params['SERVER_PORT'], ['80', '443']);
100
        if ($hasPort) {
101
            $uri = $uri->withPort($params['SERVER_PORT']);
102
        }
103
104
        $isSecure = array_key_exists('HTTPS', $params) &&
105
            $params['HTTPS'] != strtolower('off');
106
        $scheme =  $isSecure ? 'https' : 'http';
107
108
        /** @var UriInterface $uri */
109
        $uri = $uri->withHost($params['SERVER_NAME']);
110
        $uri = $uri->withScheme($scheme);
111
112
        return $uri;
113
    }
114
}
115