Completed
Push — master ( 1a3eb9...f4b0a7 )
by John
02:06
created

Repository::getUris()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
use KleijnWeb\PhpApi\Descriptions\Description\Respository\RepositoryIterator;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class Repository implements \IteratorAggregate
18
{
19
    /**
20
     * @var string
21
     */
22
    private $basePath;
23
24
    /**
25
     * @var array
26
     */
27
    private $specifications = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $uris = [];
33
34
    /**
35
     * @var Cache
36
     */
37
    private $cache;
38
39
    /**
40
     * @var DefinitionLoader
41
     */
42
    private $loader;
43
44
    /**
45
     * @var DescriptionFactory
46
     */
47
    private $factory;
48
49
    /**
50
     * Repository constructor.
51
     *
52
     * @param string|null             $basePath
53
     * @param Cache|null              $cache
54
     * @param DefinitionLoader|null   $loader
55
     * @param DescriptionFactory|null $factory
56
     */
57
    public function __construct(
58
        string $basePath = null,
59
        Cache $cache = null,
60
        DefinitionLoader $loader = null,
61
        DescriptionFactory $factory = null
62
    ) {
63
        $this->basePath = $basePath;
64
        $this->cache    = $cache;
65
        $this->loader   = $loader ?: new DefinitionLoader();
66
        $this->factory  = $factory ?: new DescriptionFactory();
67
    }
68
69
    /**
70
     * @param DescriptionFactory $factory
71
     *
72
     * @return Repository
73
     */
74
    public function setFactory(DescriptionFactory $factory): Repository
75
    {
76
        $this->factory = $factory;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $uri
83
     *
84
     * @return Description
85
     */
86
    public function get(string $uri): Description
87
    {
88
        if (!$uri) {
89
            throw new \InvalidArgumentException("No document URI provided");
90
        }
91
        if ($this->basePath) {
92
            $uri = "$this->basePath/$uri";
93
        }
94
        if (!isset($this->specifications[$uri])) {
95
            $this->specifications[$uri] = $this->load($uri);
96
        }
97
98
        return $this->specifications[$uri];
99
    }
100
101
    /**
102
     * @param string $uri
103
     *
104
     * @return Repository
105
     */
106
    public function register(string $uri): Repository
107
    {
108
        $this->uris[] = $uri;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return string[]
115
     */
116
    public function getUris(): array
117
    {
118
        return $this->uris;
119
    }
120
121
    /**
122
     * @return  \Iterator
123
     */
124
    public function getIterator(): \Iterator
125
    {
126
        return new RepositoryIterator($this);
127
    }
128
129
    /**
130
     * @param string $uri
131
     *
132
     * @return Description
133
     */
134
    private function load(string $uri): Description
135
    {
136
        if ($this->cache && $description = $this->cache->fetch($uri)) {
137
            return $description;
138
        }
139
140
        $description = $this->factory->create($uri, $this->loader->load($uri));
141
142
        if ($this->cache) {
143
            $this->cache->save($uri, $description);
144
        }
145
146
        return $description;
147
    }
148
}
149