Completed
Push — master ( 386146...a2d301 )
by John
07:40
created

DocumentRepository::load()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
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
use Symfony\Component\Yaml\Yaml;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class DocumentRepository
19
{
20
    /**
21
     * @var string
22
     */
23
    private $basePath;
24
25
    /**
26
     * @var array
27
     */
28
    private $documents = [];
29
30
    /**
31
     * @var Cache
32
     */
33
    private $cache;
34
35
    /**
36
     * Initializes a new Repository.
37
     *
38
     * @param string $basePath
39
     * @param Cache  $cache
40
     */
41
    public function __construct($basePath = null, Cache $cache = null)
42
    {
43
        $this->basePath = $basePath;
44
        $this->cache = $cache;
45
    }
46
47
    /**
48
     * @param string $documentPath
49
     *
50
     * @return SwaggerDocument
51
     */
52
    public function get($documentPath)
53
    {
54
        if ($this->basePath) {
55
            $documentPath = "$this->basePath/$documentPath";
56
        }
57
        if (!$documentPath) {
58
            throw new \InvalidArgumentException("No document path provided");
59
        }
60
        if (!isset($this->documents[$documentPath])) {
61
            $this->documents[$documentPath] = $this->load($documentPath);
62
        }
63
64
        return $this->documents[$documentPath];
65
    }
66
67
    /**
68
     * @param string $documentPath
69
     *
70
     * @return SwaggerDocument
71
     * @throws ResourceNotReadableException
72
     */
73
    private function load($documentPath)
74
    {
75
        if ($this->cache && $document = $this->cache->fetch($documentPath)) {
76
            return $document;
77
        }
78
79
        if (!is_readable($documentPath)) {
80
            throw new ResourceNotReadableException("Document '$documentPath' is not readable");
81
        }
82
83
        $content = file_get_contents($documentPath);
84
        $resolver = new RefResolver(Yaml::parse($content, true, false, true), $documentPath);
85
        $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...
86
87
        if ($this->cache) {
88
            $this->cache->save($documentPath, $document);
89
        }
90
91
        return $document;
92
    }
93
}
94