Passed
Push — master ( 829500...1175ab )
by Kevin
08:08
created

Story   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 17
c 2
b 0
f 1
dl 0
loc 56
ccs 22
cts 22
cp 1
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 12 2
A get() 0 7 2
A __construct() 0 2 1
A __callStatic() 0 3 1
A __call() 0 3 1
A add() 0 11 2
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
abstract class Story
9
{
10
    /** @var array<string, Proxy> */
11
    private array $objects = [];
12
13 38
    private function __construct()
14
    {
15 38
    }
16
17 2
    final public function __call(string $method, array $arguments)
18
    {
19 2
        return $this->get($method);
20
    }
21
22 4
    public static function __callStatic($name, $arguments)
23
    {
24 4
        return static::load()->get($name);
25
    }
26
27 38
    final public static function load(): self
28
    {
29 38
        if (StoryManager::has(static::class)) {
30 4
            return StoryManager::get(static::class);
31
        }
32
33 38
        $story = new static();
34 38
        $story->build();
35
36 38
        StoryManager::set($story);
37
38 38
        return $story;
39
    }
40
41 38
    final public function add(string $name, object $object): self
42
    {
43
        // ensure factories are persisted
44 38
        if ($object instanceof Factory) {
45 2
            $object = $object->persist();
46
        }
47
48
        // ensure all objects are wrapped in a auto-refreshing proxy
49 38
        $this->objects[$name] = PersistenceManager::proxy($object)->withAutoRefresh();
50
51 38
        return $this;
52
    }
53
54 8
    final public function get(string $name): Proxy
55
    {
56 8
        if (!\array_key_exists($name, $this->objects)) {
57 2
            throw new \InvalidArgumentException('explain that object was not registered'); // todo
58
        }
59
60 6
        return $this->objects[$name];
61
    }
62
63
    abstract protected function build(): void;
64
}
65