Completed
Branch master (bc5900)
by Vincent
01:31
created

Generator::document()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker;
6
7
use VGirol\JsonApiFaker\Contract\GeneratorContract;
8
use VGirol\JsonApiFaker\Factory\CollectionFactory;
9
use VGirol\JsonApiFaker\Factory\DocumentFactory;
10
use VGirol\JsonApiFaker\Factory\ErrorFactory;
11
use VGirol\JsonApiFaker\Factory\JsonapiFactory;
12
use VGirol\JsonApiFaker\Factory\RelationshipFactory;
13
use VGirol\JsonApiFaker\Factory\ResourceIdentifierFactory;
14
use VGirol\JsonApiFaker\Factory\ResourceObjectFactory;
15
16
/**
17
 * This class is an helper to generate factories.
18
 */
19
class Generator implements GeneratorContract
20
{
21
    /**
22
     * All the available factories
23
     *
24
     * @var array<string,string>
25
     */
26
    private $factories;
27
28
    /**
29
     * Class constructor
30
     */
31 12
    public function __construct()
32
    {
33 12
        $this->setDefaultFactories();
34 12
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39 2
    public function setFactory(string $key, ?string $class)
40
    {
41 2
        $this->factories[$key] = $class;
42
43 2
        return $this;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 11
    public function create(string $alias, ...$args)
50
    {
51 11
        $className = $this->getClassName($alias);
52 9
        $factory = new $className(...$args);
53 9
        $factory->setGenerator($this);
54
55 9
        return $factory;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 1
    public function collection(...$args)
62
    {
63 1
        return $this->create('collection', ...$args);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 1
    public function document(...$args)
70
    {
71 1
        return $this->create('document', ...$args);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77 1
    public function error(...$args)
78
    {
79 1
        return $this->create('error', ...$args);
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85 1
    public function jsonapiObject(...$args)
86
    {
87 1
        return $this->create('jsonapi', ...$args);
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93 1
    public function relationship(...$args)
94
    {
95 1
        return $this->create('relationship', ...$args);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101 1
    public function resourceIdentifier(...$args)
102
    {
103 1
        return $this->create('resource-identifier', ...$args);
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109 1
    public function resourceObject(...$args)
110
    {
111 1
        return $this->create('resource-object', ...$args);
112
    }
113
114
    /**
115
     * Set the default factories.
116
     *
117
     * @return void
118
     */
119 12
    private function setDefaultFactories()
120
    {
121 12
        $this->factories = [
122
            'collection' => CollectionFactory::class,
123
            'document' => DocumentFactory::class,
124
            'error' => ErrorFactory::class,
125
            'jsonapi' => JsonapiFactory::class,
126
            'relationship' => RelationshipFactory::class,
127
            'resource-identifier' => ResourceIdentifierFactory::class,
128
            'resource-object' => ResourceObjectFactory::class
129
        ];
130 12
    }
131
132
    /**
133
     * Retrieve the factory class name
134
     *
135
     * @param string $key
136
     *
137
     * @return string
138
     * @throws \Exception
139
     */
140 11
    private function getClassName(string $key): string
141
    {
142 11
        if (!array_key_exists($key, $this->factories)) {
143 1
            throw new \Exception(
144 1
                sprintf(Messages::FACTORY_INEXISTANT_KEY, $key)
145
            );
146
        }
147
148 10
        if ($this->factories[$key] === null) {
0 ignored issues
show
introduced by
The condition $this->factories[$key] === null is always false.
Loading history...
149 1
            throw new \Exception(
150 1
                sprintf(Messages::FACTORY_FORBIDDEN_KEY, $key)
151
            );
152
        }
153
154 9
        return $this->factories[$key];
155
    }
156
}
157