Completed
Push — master ( e50616...a5acb0 )
by Raffael
28:40 queued 24:37
created

AttributeResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 80.65%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 66
ccs 25
cts 31
cp 0.8065
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 17 3
A addResourceMetadata() 0 18 5
A translateAttributes() 0 17 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Resource;
13
14
use Closure;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Tubee\Secret\Factory as SecretFactory;
17
18
class AttributeResolver
19
{
20
    /**
21
     * Resolve.
22
     */
23 2
    public static function resolve(ServerRequestInterface $request, ResourceInterface $resource, array $resolvable): array
24
    {
25 2
        $resolvable = array_replace_recursive(self::addResourceMetaData($request, $resource), $resolvable);
26
27 2
        $params = $request->getQueryParams();
28 2
        $attributes = [];
29
30 2
        if (isset($params['attributes'])) {
31
            $attributes = $params['attributes'];
32
        }
33
34 2
        if (0 === count($attributes)) {
35 2
            return SecretFactory::reverse($resource, self::translateAttributes($resolvable, $resource));
36
        }
37
38
        return SecretFactory::reverse($resource, self::translateAttributes($resolvable, array_intersect_key($resolvable, array_flip($attributes))));
0 ignored issues
show
Documentation introduced by
array_intersect_key($res...rray_flip($attributes)) is of type array, but the function expects a object<Tubee\Resource\ResourceInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    }
40
41
    /**
42
     * Add metadata.
43
     */
44 2
    protected static function addResourceMetadata(ServerRequestInterface $request, ResourceInterface $resource): array
45
    {
46 2
        $data = $resource->toArray();
47
48
        return [
49 2
            '_links' => [
50 2
                'self' => ['href' => (string) $request->getUri()],
51
            ],
52 2
            'id' => (string) $resource->getId(),
53 2
            'kind' => $resource->getKind(),
54 2
            'name' => $resource->getName(),
55 2
            'version' => isset($data['version']) ? $data['version'] : 0,
56 2
            'created' => isset($data['created']) ? $data['created']->toDateTime()->format('c') : null,
57 2
            'changed' => isset($data['changed']) ? $data['changed']->toDateTime()->format('c') : null,
58 2
            'description' => isset($data['description']) ? $data['description'] : null,
59 2
            'secrets' => $resource->getSecrets(),
60
        ];
61
    }
62
63
    /**
64
     * Execute closures.
65
     */
66 2
    protected static function translateAttributes(array $resolvable, ResourceInterface $resource): array
67
    {
68 2
        foreach ($resolvable as $key => &$value) {
69 2
            if ($value instanceof Closure) {
70
                $result = $value($resource);
71
                if (null === $result) {
72
                    unset($resolvable[$key]);
73
                } else {
74
                    $value = $result;
75
                }
76 2
            } elseif ($value === null) {
77 2
                unset($resolvable[$key]);
78
            }
79
        }
80
81 2
        return $resolvable;
82
    }
83
}
84