Passed
Push — main ( 3f5d4e...df4575 )
by James Ekow Abaka
01:53
created

DatabaseItemFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 1
A setDriver() 0 3 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
9
10
class DatabaseItemFactory
11
{
12
    private string $home;
13
    private EncapsulatedStack $stack;
14
    private ChangeLogger $driver;
15
    
16
    public function __construct(EncapsulatedStack $stack, array $arguments)
17
    {
18
        $this->home = $arguments['home'] ?? './yentu';
19
        $this->stack = $stack;
20
    }
21
    
22
    public function setDriver(ChangeLogger $driver): void
23
    {
24
        $this->driver = $driver;
25
    }
26
    
27
    public function create(ItemType $itemType, mixed ... $args): DatabaseItem
28
    {
29
        $class = new \ReflectionClass($itemType->value);
30
        $item = $class->newInstanceArgs($args);
31
        $item->setDriver($this->driver);    
32
        $item->init();              
33
        $item->setFactory($this);
34
        $item->setStack($this->stack);
35
        $item->setHome($this->home);
36
        $this->stack->push($item);          
37
        return $item;
38
    }   
39
    
40
    public function getEncapsulatedStack(): EncapsulatedStack
41
    {
42
        return $this->stack;
43
    }
44
}
45