ResourcesResolver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerResolver() 0 7 1
A resolve() 0 14 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