Completed
Push — master ( c762b0...684d44 )
by John
02:56
created

DescriptionFactory::create()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 2
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 KleijnWeb\ApiDescriptions\Description\Builder\OpenApiBuilder;
11
use KleijnWeb\ApiDescriptions\Description\Builder\RamlBuilder;
12
use KleijnWeb\ApiDescriptions\Description\Document\Definition\RefResolver\RefResolver;
13
use KleijnWeb\ApiDescriptions\Description\Document\Document;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class DescriptionFactory
19
{
20
    const BUILDER_OPEN_API = 'openapi';
21
    const BUILDER_RAML = 'raml';
22
23
    /**
24
     * @var string
25
     */
26
    private $type;
27
28
    /**
29
     * DescriptionFactory constructor.
30
     *
31
     * @param string $type
32
     */
33
    public function __construct(string $type = self::BUILDER_OPEN_API)
34
    {
35
        $this->type = $type;
36
    }
37
38
    /**
39
     * @param string    $uri
40
     * @param \stdClass $definition
41
     *
42
     * @return Description
43
     */
44
    public function create(string $uri, \stdClass $definition): Description
45
    {
46
        switch ($this->type) {
47
            case self::BUILDER_OPEN_API:
48
                return (
49
                new OpenApiBuilder(
50
                    new Document(
51
                        $uri,
52
                        (new RefResolver($definition, $uri))->resolve()
53
                    )
54
                )
55
                )->build();
56
            case self::BUILDER_RAML:
57
                return (
58
                new RamlBuilder(
59
                    new Document(
60
                        $uri,
61
                        $definition
62
                    )
63
                )
64
                )->build();
65
            default:
66
                throw new \InvalidArgumentException();
67
        }
68
    }
69
}
70