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

ContextTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 28.87 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createContext() 0 11 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
        return $context;
43
    }
44
45
    /**
46
     * Import the context defined in metadata and set some default values.
47
     *
48
     * @param string $resourceClass
49
     * @param array  $context
50
     *
51
     * @return array
52
     */
53 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...
54
    {
55
        if (isset($context['jsonhal_has_context'])) {
56
            return $context;
57
        }
58
59
        return array_merge($context, [
60
            'jsonhal_has_context' => true,
61
            'jsonhal_sub_level' => true,
62
            'resource_class' => $resourceClass,
63
        ]);
64
    }
65
    /**
66
     * Import the context defined in metadata and set some default values.
67
     *
68
     * @param string $resourceClass
69
     * @param array  $context
70
     *
71
     * @return array
72
     */
73 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...
74
    {
75
        if (isset($context['jsonld_has_context'])) {
76
            return $context;
77
        }
78
79
        return array_merge(
80
            $context,
81
            [
82
                'jsonld_has_context' => true,
83
                // Don't use hydra:Collection in sub levels
84
                'jsonld_sub_level'   => true,
85
                'resource_class'     => $resourceClass,
86
            ]
87
        );
88
    }
89
    /**
90
     * Updates the given document to add its keys.
91
     *
92
     * @param ContextBuilderInterface $contextBuilder
93
     * @param string                  $resourceClass
94
     * @param array                   $context
95
     * @param array                   $data
96
     *
97
     * @return array
98
     */
99
    private function addContext(ContextBuilderInterface $contextBuilder, string $resourceClass, array $context, array $data = [], array $format) : array
100
    {
101
        if (in_array('application/ld+json' , $format)) {
102
            if (isset($context['jsonld_has_context'])) {
103
                return $data;
104
            }
105
106
            if (isset($context['jsonld_embed_context'])) {
107
                $data['@context'] = $contextBuilder->getResourceContext($resourceClass);
108
109
                return $data;
110
            }
111
112
            $data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
113
114
            return $data;
115
        }
116
        
117
        return $data;
118
    }
119
}
120