Completed
Push — master ( 883f28...097345 )
by Vincent
03:45 queued 11s
created

Generator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 22
dl 0
loc 68
ccs 27
cts 27
cp 1
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 5 1
A relationship() 0 3 1
A setDefaultFactories() 0 8 1
A setFactory() 0 5 1
A resourceObject() 0 3 1
A resourceIdentifier() 0 3 1
A jsonapiObject() 0 3 1
A collection() 0 3 1
A getClassName() 0 9 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker;
6
7
use VGirol\JsonApiFaker\Factory\CollectionFactory;
8
use VGirol\JsonApiFaker\Factory\JsonapiFactory;
9
use VGirol\JsonApiFaker\Factory\RelationshipFactory;
10
use VGirol\JsonApiFaker\Factory\ResourceIdentifierFactory;
11
use VGirol\JsonApiFaker\Factory\ResourceObjectFactory;
12
13
class Generator
14
{
15
    private $factories;
16
17 8
    public function __construct()
18
    {
19 8
        $this->setDefaultFactories();
20 8
    }
21
22 1
    public function setFactory(string $key, string $class)
23
    {
24 1
        $this->factories[$key] = $class;
25
26 1
        return $this;
27
    }
28
29 8
    public function create($alias, ...$args)
30
    {
31 8
        $className = $this->getClassName($alias);
32
33 7
        return new $className(...$args);
34
    }
35
36 1
    public function collection(...$args)
37
    {
38 1
        return $this->create('collection', ...$args);
39
    }
40
41 1
    public function jsonapiObject(...$args)
42
    {
43 1
        return $this->create('jsonapi', ...$args);
44
    }
45
46 1
    public function relationship(...$args)
47
    {
48 1
        return $this->create('relationship', ...$args);
49
    }
50
51 1
    public function resourceIdentifier(...$args)
52
    {
53 1
        return $this->create('resource-identifier', ...$args);
54
    }
55
56 1
    public function resourceObject(...$args)
57
    {
58 1
        return $this->create('resource-object', ...$args);
59
    }
60
61 8
    private function setDefaultFactories()
62
    {
63 8
        $this->factories = [
64
            'collection' => CollectionFactory::class,
65
            'jsonapi' => JsonapiFactory::class,
66
            'relationship' => RelationshipFactory::class,
67
            'resource-identifier' => ResourceIdentifierFactory::class,
68
            'resource-object' => ResourceObjectFactory::class
69
        ];
70 8
    }
71
72 8
    private function getClassName(string $key): string
73
    {
74 8
        if (!isset($this->factories[$key])) {
75 1
            throw new \Exception(
76 1
                sprintf(Messages::FACTORY_INEXISTANT_KEY, $key)
77
            );
78
        }
79
80 7
        return $this->factories[$key];
81
    }
82
}
83