Completed
Pull Request — master (#52)
by John
06:05
created

DocumentRepository::load()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 4
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 Doctrine\Common\Cache\Cache;
12
use KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotReadableException;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class DocumentRepository
18
{
19
    /**
20
     * @var string
21
     */
22
    private $basePath;
23
24
    /**
25
     * @var array
26
     */
27
    private $documents = [];
28
29
    /**
30
     * @var Cache
31
     */
32
    private $cache;
33
34
    /**
35
     * Initializes a new Repository.
36
     *
37
     * @param string $basePath
38
     * @param Cache  $cache
39
     */
40
    public function __construct($basePath = null, Cache $cache = null)
41
    {
42
        $this->basePath = $basePath;
43
        $this->cache = $cache;
44
    }
45
46
    /**
47
     * @param string $documentPath
48
     *
49
     * @return SwaggerDocument
50
     */
51
    public function get($documentPath)
52
    {
53
        if ($this->basePath) {
54
            $documentPath = "$this->basePath/$documentPath";
55
        }
56
        if (!$documentPath) {
57
            throw new \InvalidArgumentException("No document path provided");
58
        }
59
        if (!isset($this->documents[$documentPath])) {
60
            $this->documents[$documentPath] = $this->load($documentPath);
61
        }
62
63
        return $this->documents[$documentPath];
64
    }
65
66
    /**
67
     * @param string $documentPath
68
     *
69
     * @return SwaggerDocument
70
     * @throws ResourceNotReadableException
71
     */
72
    private function load($documentPath)
73
    {
74
        if ($this->cache && $document = $this->cache->fetch($documentPath)) {
75
            return $document;
76
        }
77
78
        if (!is_readable($documentPath)) {
79
            throw new ResourceNotReadableException("Document '$documentPath' is not readable");
80
        }
81
82
        $parser = new  YamlParser();
83
        $resolver = new RefResolver($parser->parse(file_get_contents($documentPath)), $documentPath);
0 ignored issues
show
Bug introduced by
It seems like $parser->parse(file_get_contents($documentPath)) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
84
        $document = new SwaggerDocument($documentPath, $resolver->resolve());
1 ignored issue
show
Bug introduced by
It seems like $resolver->resolve() targeting KleijnWeb\SwaggerBundle\...\RefResolver::resolve() can also be of type array; however, KleijnWeb\SwaggerBundle\...Document::__construct() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
85
86
        if ($this->cache) {
87
            $this->cache->save($documentPath, $document);
88
        }
89
90
        return $document;
91
    }
92
}
93