Completed
Pull Request — master (#608)
by Amrouche
03:47
created

ContextTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 48.04 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createContext() 0 12 3
A createHalContext() 12 12 2
A createJsonLdContext() 16 16 2
A addContext() 21 21 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, string $format) : array
34
    {
35
        if ('jsonld' === $format) {
36
            return $this->createJsonLdContext($resourceClass, $context);
37
        }
38
39
        if ('jsonhal' === $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
     * @param string                  $format
100
     *
101
     * @return array
102
     */
103 View Code Duplication
    private function addContext(ContextBuilderInterface $contextBuilder, string $resourceClass, array $context, array $data, string $format) : 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...
104
    {
105
        if ('jsonld' === $format) {
106
            if (isset($context['jsonld_has_context'])) {
107
                return $data;
108
            }
109
110
            if (isset($context['jsonld_embed_context'])) {
111
                $data['@context'] = $contextBuilder->getResourceContext($resourceClass);
112
113
                return $data;
114
            }
115
116
117
            $data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
118
119
            return $data;
120
        }
121
122
        return $data;
123
    }
124
}
125