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

Story::__call()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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