ResourcesResolver::resolve()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace Laravel\ProductFields;
4
5
use Illuminate\Support\Arr;
6
7
class ResourcesResolver
8
{
9
    /**
10
     * @var array
11
     */
12
    protected static $resolvers = [];
13
14
    /**
15
     * @param string   $model
16
     * @param string   $shortKey
17
     * @param callable $resolver
18
     */
19
    public static function registerResolver(string $model, string $shortKey, callable $resolver)
20
    {
21
        static::$resolvers[$model] = [
22
            'short_key' => $shortKey,
23
            'resolver' => $resolver,
24
        ];
25
    }
26
27
    /**
28
     * @param string $resource
29
     * @param array  $ids
30
     *
31
     * @return mixed
32
     */
33
    public function resolve(string $resource, array $ids)
34
    {
35
        $shortKey = Arr::get(static::$resolvers, $resource.'.short_key', $resource);
36
        $resolver = Arr::get(static::$resolvers, $resource.'.resolver');
37
38
        if (!is_callable($resolver)) {
39
            throw new \InvalidArgumentException('Resolver for given resource ('.$resource.') was not found.');
40
        }
41
42
        return [
43
            'short_key' => $shortKey,
44
            'resources' => call_user_func_array($resolver, [$ids]),
45
        ];
46
    }
47
}
48