Passed
Pull Request — 2.x (#1446)
by Harings
10:23
created

TwillCapsules::getCapsuleForModule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace A17\Twill;
4
5
use A17\Twill\Exceptions\CapsuleWithNameAlreadyExistsException;
6
use A17\Twill\Exceptions\NoCapsuleFoundException;
7
use A17\Twill\Helpers\Capsule;
8
use Illuminate\Support\Collection;
9
10
class TwillCapsules
11
{
12
    /**
13
     * @var \A17\Twill\Helpers\Capsule[]
14
     */
15
    public static $registeredCapsules = [];
16
17
    /**
18
     * @var bool
19
     */
20
    public static $isLoaded = false;
21
22
    /**
23
     * @throws \A17\Twill\Exceptions\CapsuleWithNameAlreadyExistsException
24
     */
25
    public function registerPackageCapsule(
26
        string $name,
27
        string $namespace,
28
        string $path,
29
        string $singular = null,
30
        bool $enabled = true
31
    ): Capsule {
32
        if (isset(self::$registeredCapsules[$name])) {
33
            throw new CapsuleWithNameAlreadyExistsException();
34
        }
35
36
        self::$registeredCapsules[$name] = new Capsule($name, $namespace, $path, $singular, $enabled, true);
37
38
        return self::$registeredCapsules[$name];
39
    }
40
41
    /**
42
     * @throws \A17\Twill\Exceptions\NoCapsuleFoundException
43
     */
44
    public function getCapsuleForModule(string $module): Capsule
45
    {
46
        $capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($module) {
47
            return $capsule->getModule() === $module;
48
        });
49
50
        if (!$capsule) {
51
            throw new NoCapsuleFoundException($module);
52
        }
53
54
        return $capsule;
55
    }
56
57
    /**
58
     * @throws \A17\Twill\Exceptions\NoCapsuleFoundException
59
     */
60
    public function getCapsuleForModel(string $model): Capsule
61
    {
62
        $capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($model) {
63
            return $capsule->getSingular() === $model;
64
        });
65
66
        if (!$capsule) {
67
            throw new NoCapsuleFoundException($model);
68
        }
69
70
        return $capsule;
71
    }
72
73
    /**
74
     * @return Capsule[]
75
     */
76
    public function getRegisteredCapsules(): Collection
77
    {
78
        $this->loadProjectCapsules();
79
80
        return collect(self::$registeredCapsules);
81
    }
82
83
    public function loadProjectCapsules(): void
84
    {
85
        $path = config('twill.capsules.path');
86
87
        $list = collect(config('twill.capsules.list'));
88
89
        if (!self::$isLoaded) {
90
            $list
91
                ->where('enabled', true)
92
                ->map(function ($capsule) use ($path) {
93
                    self::$registeredCapsules[$capsule['name']] = new Capsule(
94
                        $capsule['name'],
95
                        $this->capsuleNamespace($capsule['name']),
96
                        $path . '/' . $capsule['name'],
97
                        $capsule['singular'] ?? null,
98
                        $capsule['enabled'] ?? true
99
                    );
100
                });
101
        }
102
    }
103
104
    private function capsuleNamespace($capsuleName, $type = null): string
105
    {
106
        $base = config('twill.capsules.namespaces.base');
107
108
        $type = config("twill.capsules.namespaces.$type");
109
110
        return "$base\\$capsuleName" . (filled($type) ? "\\$type" : '');
111
    }
112
113
    public function getAutoloader() {
114
        return app()->bound('autoloader')
115
            ? app('autoloader')
116
            : require base_path('vendor/autoload.php');
117
    }
118
}
119