Passed
Push — main ( c93099...0e69d1 )
by James Ekow Abaka
17:15
created

DatabaseItemFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 38
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultSchema() 0 3 1
A setChangeLogger() 0 3 1
A create() 0 11 1
A getEncapsulatedStack() 0 3 1
A __construct() 0 4 1
1
<?php
2
namespace yentu\factories;
3
4
use yentu\database\DatabaseItem;
5
use yentu\database\ItemType;
6
use yentu\database\EncapsulatedStack;
7
use yentu\ChangeLogger;
8
use yentu\database\Schema;
9
10
11
class DatabaseItemFactory
12
{
13
    private string $home;
14
    private EncapsulatedStack $stack;
15
    private ChangeLogger $changeLogger;
16
    
17
    public function __construct(EncapsulatedStack $stack, array $arguments)
18
    {
19
        $this->home = $arguments['home'] ?? './yentu';
20
        $this->stack = $stack;
21
    }
22
    
23
    public function setChangeLogger(ChangeLogger $changeLogger): void
24
    {
25
        $this->changeLogger = $changeLogger;
26
    }
27
28
    public function getDefaultSchema(string $name): Schema
29
    {
30
        return new Schema($name);
31
    }
32
    
33
    public function create(ItemType $itemType, mixed ... $args): DatabaseItem
34
    {
35
        $class = new \ReflectionClass($itemType->value);
36
        $item = $class->newInstanceArgs($args);
37
        $item->setChangeLogger($this->changeLogger);
38
        $item->init();              
39
        $item->setFactory($this);
40
        $item->setStack($this->stack);
41
        $item->setHome($this->home);
42
        $this->stack->push($item);          
43
        return $item;
44
    }   
45
    
46
    public function getEncapsulatedStack(): EncapsulatedStack
47
    {
48
        return $this->stack;
49
    }
50
}
51