Test Failed
Pull Request — master (#11)
by
unknown
16:45 queued 43s
created

JsonApiSerializer::injectAvailableIncludeData()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 3
nop 2
dl 0
loc 20
ccs 0
cts 0
cp 0
crap 30
rs 9.5555
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Fractal;
2
3
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
4
5
class JsonApiSerializer extends \League\Fractal\Serializer\JsonApiSerializer
6
{
7
    /**
8
     * @var RestRequestContract
9
     */
10
    protected $request;
11
12
    /**
13
     * JsonApiSerializer constructor.
14
     *
15
     * @param RestRequestContract $request
16
     */
17 19
    public function __construct(RestRequestContract $request)
18
    {
19 19
        parent::__construct($request->getBaseUrl());
20 19
        $this->request = $request;
21 19
    }
22
23
    /**
24
     * @param string $resourceKey
25
     * @param array  $data
26
     * @param bool   $includeAttributes
27
     *
28
     * @return array
29
     */
30 13
    public function collection($resourceKey, array $data, $includeAttributes = true): array
31
    {
32 13
        $resources = [];
33
34 13
        foreach ($data as $resource) {
35
36
            /**
37
             * Add option to override resource key with data.
38
             */
39 13
            if (is_array($resource) && array_key_exists('_resource_key', $resource)) {
40
                $resourceKey = $resource['_resource_key'];
41
                unset($resource['_resource_key']);
42
            }
43
44 13
            $resources[] = $this->item($resourceKey, $resource, $includeAttributes)['data'];
45
        }
46
47 13
        return ['data' => $resources];
48
    }
49
50
    /**
51
     * @param string $resourceKey
52
     * @param array  $data
53
     * @param bool   $includeAttributes
54
     *
55
     * @return array
56
     */
57 19
    public function item($resourceKey, array $data, $includeAttributes = true): array
58
    {
59 19
        $item = parent::item($resourceKey, $data);
60 19
        if (!$includeAttributes) {
61 4
            unset($item['data']['attributes']);
62
        }
63
64 19
        return $item;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function injectAvailableIncludeData(array $data, array $availableIncludes): array
71
    {
72
        if (!$this->shouldIncludeLinks()) {
73
            return $data;
74
        }
75
76
        if ($this->isCollection($data)) {
77
            $data['data'] = array_map(function ($resource) use ($availableIncludes) {
78
                foreach ($availableIncludes as $relationshipKey) {
79
                    $resource = $this->addRelationshipLinks($resource, $relationshipKey);
80
                }
81
                return $resource;
82
            }, $data['data']);
83
        } else {
84
            foreach ($availableIncludes as $relationshipKey) {
85
                $data['data'] = $this->addRelationshipLinks($data['data'], $relationshipKey);
86
            }
87
        }
88
89
        return $data;
90
    }
91
92
    /**
93
     * Do not include links if there are no results.
94
     */
95
    private function addRelationshipLinks(array $resource, string $relationshipKey): array
96
    {
97
        if (isset($resource['relationships'][$relationshipKey])) {
98
            $resource['relationships'][$relationshipKey] = array_merge(
99
                [
100
                    'links' => [
101
                        'self' => "{$this->baseUrl}/{$resource['type']}/{$resource['id']}/relationships/{$relationshipKey}",
102
                        'related' => "{$this->baseUrl}/{$resource['type']}/{$resource['id']}/{$relationshipKey}",
103
                    ]
104
                ],
105
                $resource['relationships'][$relationshipKey]
106
            );
107
        }
108
109
        return $resource;
110
    }
111
}
112