RequestMappingAnnotation::initAnnotation()   B
last analyzed

Complexity

Conditions 10
Paths 34

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 1
b 0
f 0
nc 34
nop 1
dl 0
loc 37
rs 7.6666

How to fix   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
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Annotations\Http;
13
14
use BlitzPHP\Annotations\BaseAnnotation;
15
use mindplay\annotations\AnnotationException;
16
17
/**
18
 * Annotation pour le mappage des requêtes Web sur les méthodes dans les ccontrôleurs
19
 * avec des signatures de méthode flexibles.
20
 *
21
 * @usage('class'=>true, 'method'=> true, 'inherited'=>true)
22
 */
23
class RequestMappingAnnotation extends BaseAnnotation
24
{
25
    /**
26
     * Méthodes autorisés
27
     */
28
    private const VALID_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'TRACE'];
29
30
    /**
31
     * @var string[]
32
     *
33
     * Les méthodes de requête HTTP à mapper, en limitant le mappage principal :
34
     * GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
35
     * <p><b>Pris en charge au niveau du contrôleur ainsi qu'au niveau de la méthode !</b>
36
     * Lorsqu'il est utilisé au niveau du contrôleur, tous les mappages au niveau de la méthode héritent de ce
37
     * Restriction de la méthode HTTP.
38
     */
39
    public $method;
40
41
    /**
42
     * @var string
43
     *
44
     * Les URI de mappage de chemin (par exemple, {@code "/profile"}).
45
     * <p>Les modèles de chemin de style Ant sont également pris en charge (par exemple, {@code "/profile/**"}).
46
     * Au niveau de la méthode, les chemins relatifs (par exemple, {@code "edit"}) sont pris en charge
47
     * dans le mappage principal exprimé au niveau du contôleur.
48
     * <p><b>Pris en charge au niveau du contrôleur ainsi qu'au niveau de la méthode !</b>
49
     * Lorsqu'ils sont utilisés au niveau du contrôleur, tous les mappages au niveau de la méthode héritent
50
     * ce mappage primaire, en le rétrécissant pour une méthode de gestionnaire spécifique.
51
     */
52
    public $path;
53
54
    /**
55
     * Initialisation de l'annotation.
56
     */
57
    public function initAnnotation(array $properties)
58
    {
59
        if (isset($properties[0])) {
60
            if (isset($properties[1])) {
61
                $this->method = (array) $properties[0];
62
63
                if (! is_string($properties[1])) {
64
                    throw new AnnotationException('RequestMappingAnnotation requires a string as path property');
65
                }
66
67
                $this->path = $properties[1];
68
69
                unset($properties[1]);
70
            } else {
71
                if (is_string($properties[0])) {
72
                    $this->path = $properties[0];
73
                } elseif (is_array($properties[0])) {
74
                    $this->method = $properties[0];
75
                } else {
76
                    throw new AnnotationException('Invalid type for RequestMappingAnnotation properties');
77
                }
78
            }
79
80
            unset($properties[0]);
81
        }
82
83
        parent::initAnnotation($properties);
84
85
        if ($this->method === ['*']) {
86
            $this->method = null;
87
        }
88
        if (! empty($this->method)) {
89
            $this->method = array_map('strtoupper', $this->method);
90
91
            foreach ($this->method as $method) {
92
                if (! in_array($method, self::VALID_METHODS, true)) {
93
                    throw new AnnotationException('`' . $method . '` is not a valid method for RequestMappingAnnotation');
94
                }
95
            }
96
        }
97
    }
98
}
99