Test Failed
Push — master ( 37e761...633337 )
by Chris
18:21
created

AbstractModelRegistrar   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 47 4
A boot() 0 8 3
1
<?php
2
3
namespace Leonidas\Framework\Bootstrap\Abstracts;
4
5
use Leonidas\Contracts\Extension\ExtensionBootProcessInterface;
6
use Leonidas\Contracts\Extension\WpExtensionInterface;
7
use Leonidas\Contracts\Util\AutoInvokerInterface;
8
use Leonidas\Library\System\Schema\Comment\CommentEntityManager;
9
use Leonidas\Library\System\Schema\Post\AttachmentEntityManager;
10
use Leonidas\Library\System\Schema\Post\PostEntityManager;
11
use Leonidas\Library\System\Schema\Term\TermEntityManager;
12
use Leonidas\Library\System\Schema\User\UserEntityManager;
13
use Panamax\Contracts\ServiceContainerInterface;
14
15
abstract class AbstractModelRegistrar implements ExtensionBootProcessInterface
16
{
17
    protected const CONTRACTS = '';
18
19
    protected const ENTITY_MANAGERS = [
20
        'post' => PostEntityManager::class,
21
        'attachment' => AttachmentEntityManager::class,
22
        'term' => TermEntityManager::class,
23
        'user' => UserEntityManager::class,
24
        'comment' => CommentEntityManager::class,
25
    ];
26
27
    protected WpExtensionInterface $extension;
28
29
    protected ServiceContainerInterface $container;
30
31
    public function boot(WpExtensionInterface $extension, ServiceContainerInterface $container): void
32
    {
33
        $this->extension = $extension;
34
        $this->container = $container;
35
36
        foreach (get_class_methods($this) as $method) {
37
            if (str_ends_with($method, 'Services')) {
38
                $this->{$method}();
39
            }
40
        }
41
    }
42
43
    protected function register(string $model, string $entity, string $schema = 'post'): void
44
    {
45
        $parts = explode('\\', $model);
46
        $base = array_pop($parts);
47
        $namespace = implode('\\', $parts);
48
49
        if (str_ends_with($namespace, $base)) {
50
            $namespace = substr($namespace, 0, -strlen($base) - 1);
51
        }
52
53
        $converter = $model . 'Converter';
54
        $queryFactory = $model . 'QueryFactory';
55
        $repositoryInterface = sprintf(
56
            '%s\%s\%sRepositoryInterface',
57
            static::CONTRACTS ?: $namespace,
58
            $base,
59
            $base
60
        );
61
62
        $collectionFactory = $model . 'CollectionFactory';
63
        $repository = $model . 'Repository';
64
        $manager = static::ENTITY_MANAGERS[$schema];
65
66
        $this->container->share($converter, fn () => new $converter(
67
            $this->extension->get(AutoInvokerInterface::class),
68
        ));
69
70
        if (in_array($schema, ['post', 'attachment'])) {
71
            $this->container->share($queryFactory, fn () => new $queryFactory(
72
                $this->extension->get($converter),
73
            ));
74
75
            $repoCallback = fn () => new $repository(new $manager(
76
                $entity,
77
                $this->extension->get($converter),
78
                new $collectionFactory(),
79
                $this->extension->get($queryFactory),
80
            ));
81
        } else {
82
            $repoCallback = fn () => new $repository(new $manager(
83
                $entity,
84
                $this->extension->get($converter),
85
                new $collectionFactory(),
86
            ));
87
        }
88
89
        $this->container->share($repositoryInterface, $repoCallback);
90
    }
91
}
92