Completed
Pull Request — master (#608)
by Amrouche
05:29 queued 02:26
created

ContextTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 28 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 28
loc 100
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createContext() 0 12 3
A createHalContext() 12 12 2
A createJsonLdContext() 16 16 2
A addContext() 0 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Serializer;
13
14
use ApiPlatform\Core\Hypermedia\ContextBuilderInterface;
15
16
/**
17
 * Creates and manipulates the Serializer context.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 *
21
 * @internal
22
 */
23
trait ContextTrait
24
{
25
    /**
26
     * Import the context defined in metadata and set some default values.
27
     *
28
     * @param string $resourceClass
29
     * @param array  $context
30
     *
31
     * @return array
32
     */
33
    private function createContext(string $resourceClass, array $context, array $format) : array
34
    {
35
        if (in_array('application/ld+json', $format)) {
36
            return $this->createJsonLdContext($resourceClass, $context);
37
        }
38
39
        if (in_array('application/hal+json', $format)) {
40
            return $this->createHalContext($resourceClass, $context);
41
        }
42
43
        return $context;
44
    }
45
46
    /**
47
     * Import the context defined in metadata and set some default values.
48
     *
49
     * @param string $resourceClass
50
     * @param array  $context
51
     *
52
     * @return array
53
     */
54 View Code Duplication
    private function createHalContext(string $resourceClass, array $context) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if (isset($context['jsonhal_has_context'])) {
57
            return $context;
58
        }
59
60
        return array_merge($context, [
61
            'jsonhal_has_context' => true,
62
            'jsonhal_sub_level' => true,
63
            'resource_class' => $resourceClass,
64
        ]);
65
    }
66
67
    /**
68
     * Import the context defined in metadata and set some default values.
69
     *
70
     * @param string $resourceClass
71
     * @param array  $context
72
     *
73
     * @return array
74
     */
75 View Code Duplication
    private function createJsonLdContext(string $resourceClass, array $context) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        if (isset($context['jsonld_has_context'])) {
78
            return $context;
79
        }
80
81
        return array_merge(
82
            $context,
83
            [
84
                'jsonld_has_context' => true,
85
                // Don't use hydra:Collection in sub levels
86
                'jsonld_sub_level' => true,
87
                'resource_class' => $resourceClass,
88
            ]
89
        );
90
    }
91
92
    /**
93
     * Updates the given document to add its keys.
94
     *
95
     * @param ContextBuilderInterface $contextBuilder
96
     * @param string                  $resourceClass
97
     * @param array                   $context
98
     * @param array                   $data
99
     *
100
     * @return array
101
     */
102
    private function addContext(ContextBuilderInterface $contextBuilder, string $resourceClass, array $context, array $data, array $format) : array
103
    {
104
        if (in_array('application/ld+json', $format)) {
105
            if (isset($context['jsonld_has_context'])) {
106
                return $data;
107
            }
108
109
            if (isset($context['jsonld_embed_context'])) {
110
                $data['@context'] = $contextBuilder->getResourceContext($resourceClass);
111
112
                return $data;
113
            }
114
115
            $data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
116
117
            return $data;
118
        }
119
120
        return $data;
121
    }
122
}
123