Completed
Push — master ( 59cbe9...e9e747 )
by John
05:11
created

PhpApiMiddleware   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createStringStream() 0 7 1
A getDescription() 0 4 1
A getPath() 0 4 1
A getOperation() 0 4 1
A getStreamFactory() 0 8 2
A getMeta() 0 4 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions 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\PhpApi\Middleware;
10
11
use Interop\Http\ServerMiddleware\MiddlewareInterface;
12
use KleijnWeb\PhpApi\Descriptions\Description\Description;
13
use KleijnWeb\PhpApi\Descriptions\Description\Operation;
14
use KleijnWeb\PhpApi\Descriptions\Description\Path;
15
use Middlewares\Utils\Factory\StreamFactory;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Zend\Diactoros\Stream;
18
19
abstract class PhpApiMiddleware implements MiddlewareInterface
20
{
21
    /**
22
     * @var StreamFactory
23
     */
24
    private $streamFactory;
25
26
    /**
27
     * @param string $string
28
     * @return Stream
29
     */
30
    protected function createStringStream(string $string): Stream
31
    {
32
        $stream = $this->getStreamFactory()->createStream($string);
33
        $stream->rewind();
34
35
        return $stream;
36
    }
37
38
    /**
39
     * @param ServerRequestInterface $request
40
     * @return Description
41
     */
42
    protected function getDescription(ServerRequestInterface $request): Description
43
    {
44
        return $this->getMeta($request)->getDescription();
45
    }
46
47
    /**
48
     * @param ServerRequestInterface $request
49
     * @return Path
50
     */
51
    protected function getPath(ServerRequestInterface $request): Path
52
    {
53
        return $this->getMeta($request)->getPath();
54
    }
55
56
    /**
57
     * @param ServerRequestInterface $request
58
     * @return Operation
59
     */
60
    protected function getOperation(ServerRequestInterface $request): Operation
61
    {
62
        return $this->getMeta($request)->getOperation();
63
    }
64
65
    /**
66
     * @return StreamFactory
67
     */
68
    protected function getStreamFactory(): StreamFactory
69
    {
70
        if (!$this->streamFactory) {
71
            $this->streamFactory = new StreamFactory();
72
        }
73
74
        return $this->streamFactory;
75
    }
76
77
    /**
78
     * @param ServerRequestInterface $request
79
     * @return Meta
80
     */
81
    private function getMeta(ServerRequestInterface $request): Meta
82
    {
83
        return Meta::getFromRequest($request);
84
    }
85
}
86