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

Repository::get()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 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
namespace KleijnWeb\PhpApi\Descriptions\Description;
9
10
use Doctrine\Common\Cache\Cache;
11
use KleijnWeb\PhpApi\Descriptions\Description\Document\Definition\Loader\DefinitionLoader;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class Repository
17
{
18
    /**
19
     * @var string
20
     */
21
    private $basePath;
22
23
    /**
24
     * @var array
25
     */
26
    private $specifications = [];
27
28
    /**
29
     * @var Cache
30
     */
31
    private $cache;
32
33
    /**
34
     * @var DefinitionLoader
35
     */
36
    private $loader;
37
38
    /**
39
     * @var DescriptionFactory
40
     */
41
    private $factory;
42
43
    /**
44
     * Repository constructor.
45
     *
46
     * @param string|null             $basePath
47
     * @param Cache|null              $cache
48
     * @param DefinitionLoader|null   $loader
49
     * @param DescriptionFactory|null $factory
50
     */
51
    public function __construct(
52
        string $basePath = null,
53
        Cache $cache = null,
54
        DefinitionLoader $loader = null,
55
        DescriptionFactory $factory = null
56
    ) {
57
        $this->basePath = $basePath;
58
        $this->cache    = $cache;
59
        $this->loader   = $loader ?: new DefinitionLoader();
60
        $this->factory  = $factory ?: new DescriptionFactory();
61
    }
62
63
    /**
64
     * @param DescriptionFactory $factory
65
     *
66
     * @return Repository
67
     */
68
    public function setFactory(DescriptionFactory $factory): Repository
69
    {
70
        $this->factory = $factory;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $documentPath
77
     *
78
     * @return Description
79
     */
80
    public function get(string $documentPath): Description
81
    {
82
        if (!$documentPath) {
83
            throw new \InvalidArgumentException("No document path provided");
84
        }
85
        if ($this->basePath) {
86
            $documentPath = "$this->basePath/$documentPath";
87
        }
88
        if (!isset($this->specifications[$documentPath])) {
89
            $this->specifications[$documentPath] = $this->load($documentPath);
90
        }
91
92
        return $this->specifications[$documentPath];
93
    }
94
95
    /**
96
     * @param string $uri
97
     *
98
     * @return Description
99
     */
100
    private function load(string $uri): Description
101
    {
102
        if ($this->cache && $description = $this->cache->fetch($uri)) {
103
            return $description;
104
        }
105
106
        $description = $this->factory->create($uri, $this->loader->load($uri));
107
108
        if ($this->cache) {
109
            $this->cache->save($uri, $description);
110
        }
111
112
        return $description;
113
    }
114
}
115