Completed
Push — master ( e54b6a...c762b0 )
by John
03:11
created

Repository::setFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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