Passed
Pull Request — release/1.0.0 (#6)
by Yo
03:05
created

ConstraintPayloadDocHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 91
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPayloadDocValue() 0 5 2
A appendPayloadDoc() 0 25 3
A getTypeIfExist() 0 7 2
A hasPayloadDoc() 0 4 2
A hasPayloadDocKey() 0 3 1
1
<?php
2
namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper;
3
4
use Symfony\Component\Validator\Constraint;
5
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
6
7
/**
8
 * Class ConstraintPayloadDocHelper
9
 */
10
class ConstraintPayloadDocHelper
11
{
12
    /** Use this key in constraint payload to define parameter documentation */
13
    const PAYLOAD_DOCUMENTATION_KEY = 'documentation';
14
    const PAYLOAD_DOCUMENTATION_TYPE_KEY = 'type';
15
    const PAYLOAD_DOCUMENTATION_REQUIRED_KEY = 'required';
16
    const PAYLOAD_DOCUMENTATION_NULLABLE_KEY = 'nullable';
17
    const PAYLOAD_DOCUMENTATION_DESCRIPTION_KEY = 'description';
18
    const PAYLOAD_DOCUMENTATION_DEFAULT_KEY = 'default';
19
    const PAYLOAD_DOCUMENTATION_EXAMPLE_KEY = 'example';
20
21
    /**
22
     * @param TypeDoc $doc
23
     * @param Constraint $constraint
24
     */
25 6
    public function appendPayloadDoc(TypeDoc $doc, Constraint $constraint)
26
    {
27 6
        if (!$this->hasPayloadDoc($constraint)) {
28 1
            return;
29
        }
30
31
        // /!\ Do not override value if payload have nothing defined for a key ! /!\
32
33 5
        $doc->setRequired(
34 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_REQUIRED_KEY) ?? $doc->isRequired()
35
        );
36 5
        $doc->setNullable(
37 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_NULLABLE_KEY) ?? $doc->isNullable()
38
        );
39
40 5
        $doc->setExample(
41 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_EXAMPLE_KEY) ?? $doc->getExample()
42
        );
43 5
        $doc->setDefault(
44 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_DEFAULT_KEY) ?? $doc->getDefault()
45
        );
46
47 5
        $description = $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_DESCRIPTION_KEY);
48 5
        if (null !== $description) {
49 1
            $doc->setDescription($description);
50
        }
51 5
    }
52
53
    /**
54
     * @param Constraint $constraint
55
     *
56
     * @return mixed|null Null in case type does not exist
57
     */
58 2
    public function getTypeIfExist(Constraint $constraint)
59
    {
60 2
        if (!$this->hasPayloadDoc($constraint)) {
61
            return null;
62
        }
63
64 2
        return $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_TYPE_KEY);
65
    }
66
67
    /**
68
     * @param Constraint $constraint
69
     *
70
     * @return bool
71
     */
72 8
    protected function hasPayloadDoc(Constraint $constraint) : bool
73
    {
74 8
        return is_array($constraint->payload)
75 8
            && array_key_exists(self::PAYLOAD_DOCUMENTATION_KEY, $constraint->payload);
76
    }
77
78
    /**
79
     * @param Constraint $constraint
80
     * @param string     $documentationKey
81
     *
82
     * @return mixed|null Return null if value does not exist
83
     */
84 7
    protected function getPayloadDocValue(Constraint $constraint, string $documentationKey)
85
    {
86 7
        return $this->hasPayloadDocKey($constraint, $documentationKey)
87 6
            ? $constraint->payload[self::PAYLOAD_DOCUMENTATION_KEY][$documentationKey]
88 7
            : null
89
        ;
90
    }
91
92
    /**
93
     * @param Constraint $constraint
94
     * @param string     $documentationKey
95
     *
96
     * @return bool
97
     */
98 7
    protected function hasPayloadDocKey(Constraint $constraint, string $documentationKey) : bool
99
    {
100 7
        return array_key_exists($documentationKey, $constraint->payload[self::PAYLOAD_DOCUMENTATION_KEY]);
101
    }
102
}
103