Content::process()   D
last analyzed

Complexity

Conditions 20
Paths 24

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 20.1186

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 57
ccs 28
cts 30
cp 0.9333
rs 4.1666
cc 20
nc 24
nop 5
crap 20.1186

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Swaggest\JsonSchema\Constraint;
4
5
use Swaggest\JsonSchema\Context;
6
use Swaggest\JsonSchema\Exception\ContentException;
7
8
class Content
9
{
10
    const MEDIA_TYPE_APPLICATION_JSON = 'application/json';
11
    const ENCODING_BASE64 = 'base64';
12
13
    const BASE64_INVALID_REGEX = '_[^A-Za-z0-9+/=]+_';
14
15
16
    /**
17
     * @param Context $options
18
     * @param string|null $encoding
19
     * @param string|null $mediaType
20
     * @param string $data
21
     * @param bool $import
22
     * @return bool|mixed|string
23
     * @throws ContentException
24
     */
25 20
    public static function process(Context $options, $encoding, $mediaType, $data, $import = true)
26
    {
27 20
        if ($import) {
28 20
            if ($encoding !== null) {
29
                switch ($encoding) {
30 14
                    case self::ENCODING_BASE64:
31 14
                        if ($options->strictBase64Validation && preg_match(self::BASE64_INVALID_REGEX, $data)) {
32 2
                            throw new ContentException('Invalid base64 string');
33
                        }
34 12
                        $data = base64_decode($data);
35 12
                        if ($data === false && !$options->skipValidation) { // @phpstan-ignore-line
36
                            throw new ContentException('Unable to decode base64');
37
                        }
38 12
                        break;
39
                }
40
            }
41
42 18
            if ($mediaType !== null && $data !== false) {
43
                switch ($mediaType) {
44 13
                    case self::MEDIA_TYPE_APPLICATION_JSON:
45 13
                        $data = json_decode($data);
46 13
                        $lastErrorCode = json_last_error();
47 13
                        if (($lastErrorCode !== JSON_ERROR_NONE) && !$options->skipValidation) {
48
                            // TODO add readable error message
49 2
                            throw new ContentException('Unable to decode json, err code: ' . $lastErrorCode);
50
                        }
51 11
                        break;
52
53
                }
54
            }
55
56 16
            return $data;
57
        } else {
58
            // export
59
60 6
            if ($mediaType !== null) {
61
                switch ($mediaType) {
62 4
                    case self::MEDIA_TYPE_APPLICATION_JSON:
63 4
                        $data = json_encode($data);
64 4
                        $lastErrorCode = json_last_error();
65 4
                        if (($lastErrorCode !== JSON_ERROR_NONE) && !$options->skipValidation) {
66
                            // TODO add readable error message
67
                            throw new ContentException('Unable to encode json, err code: ' . $lastErrorCode);
68
                        }
69 4
                        break;
70
                }
71
            }
72
73 6
            if ($encoding !== null && $data !== false) {
74
                switch ($encoding) {
75 4
                    case self::ENCODING_BASE64:
76 4
                        $data = base64_encode($data);
77 4
                        break;
78
                }
79
            }
80
81 6
            return $data;
82
        }
83
84
    }
85
}