Completed
Push — master ( bca24d...1b0d8b )
by John
03:18
created

SimpleReader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A read() 0 13 3
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
namespace KleijnWeb\PhpApi\Descriptions\Description\Document\Reader;
9
10
/**
11
 * @author John Kleijn <[email protected]>
12
 */
13
class SimpleReader implements Reader
14
{
15
    const CONTENT_TYPE_YAML = 'application/yml';
16
    const CONTENT_TYPE_JSON = 'application/json';
17
18
    /**
19
     * @param string $uri
20
     *
21
     * @return Response
22
     */
23
    public function read(string $uri): Response
24
    {
25
        $contents = @file_get_contents($uri);
26
27
        if (false === $contents) {
28
            throw new ResourceNotReadableException("Failed reading '$uri'");
29
        }
30
31
        return new Response(
32
            preg_match('/\b(yml|yaml|raml)\b/', $uri) ? self::CONTENT_TYPE_YAML : self::CONTENT_TYPE_JSON,
33
            $contents
34
        );
35
    }
36
}
37