Completed
Pull Request — master (#52)
by John
02:48
created

ParameterRefBuilder::buildDocumentLink()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 5.3846
cc 8
eloc 16
nc 24
nop 1
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Document;
10
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class ParameterRefBuilder
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $schemes = ['https', 'wss', 'http', 'ws'];
22
23
    /**
24
     * @var string
25
     */
26
    private $scheme;
27
28
    /**
29
     * @var string
30
     */
31
    private $host;
32
33
    /**
34
     * @var string
35
     */
36
    private $basePath;
37
38
    /**
39
     * Construct the wrapper
40
     *
41
     * @param string      $basePath
42
     * @param string|null $scheme
43
     * @param string|null $host
44
     */
45
    public function __construct($basePath = '/', $scheme = null, $host = null)
46
    {
47
        $this->scheme = $scheme;
48
        $this->host = $host;
49
        $this->basePath = $basePath;
50
    }
51
52
    /**
53
     * @param Request $request
54
     * @param string  $parameterName
55
     *
56
     * @return string
57
     */
58
    public function buildSpecificationLink(Request $request, $parameterName)
59
    {
60
        return "{$this->buildDocumentLink($request)}#{$this->createParameterPointer($request, $parameterName)}";
61
    }
62
63
    /**
64
     * @param Request $request
65
     *
66
     * @return string
67
     */
68
    public function buildDocumentLink(Request $request)
69
    {
70
        /** @var SwaggerDocument $document */
71
        $document = $request->attributes->get('_swagger_document');
72
        /** @var string $filePath */
73
        $filePath = $request->attributes->get('_definition');
74
75
        $definition = $document->getDefinition();
76
        $basePath = $this->basePath;
77
        $host = $this->host ?: property_exists($definition, 'host') ? $definition->host : $request->getHost();
78
        $scheme = $this->scheme;
79
        if (!$scheme) {
80
            $scheme = $request->getScheme();
81
            if (property_exists($definition, 'schemes')) {
82
                if (!in_array($scheme, self::$schemes)) {
83
                    foreach (self::$schemes as $knownScheme) {
84
                        if (in_array($knownScheme, $definition->schemes)) {
85
                            $this->scheme = $knownScheme;
86
                            break;
87
                        }
88
                    }
89
                }
90
            }
91
        }
92
93
        return "$scheme://$host{$basePath}{$filePath}";
94
    }
95
96
    /**
97
     * @param Request $request
98
     * @param string  $parameterName
99
     *
100
     * @return string
101
     */
102
    public function createParameterPointer(Request $request, $parameterName)
103
    {
104
        /** @var OperationObject $operation */
105
        $operation = $request->attributes->get('_swagger_operation');
106
107
        return $operation->createParameterPointer($parameterName);
108
    }
109
110
    /**
111
     * @param Request $request
112
     * @param string  $parameterName
113
     *
114
     * @return string
115
     */
116
    public function createParameterSchemaPointer(Request $request, $parameterName)
117
    {
118
        /** @var OperationObject $operation */
119
        $operation = $request->attributes->get('_swagger_operation');
120
121
        return $operation->createParameterSchemaPointer($parameterName);
122
    }
123
}
124