AnnotationWSDLBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Copyright (C) 2013-2020
4
 * Piotr Olaszewski <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
namespace WSDL\Builder;
25
26
use Doctrine\Common\Annotations\AnnotationReader;
27
use Doctrine\Common\Annotations\AnnotationRegistry;
28
use Exception;
29
use Ouzo\Utilities\Path;
30
use ReflectionClass;
31
use WSDL\Annotation\ClassAnnotation;
32
use WSDL\Annotation\MethodAnnotation;
33
use WSDL\Annotation\WebMethod;
34
use WSDL\Annotation\WebService;
35
36
/**
37
 * AnnotationWSDLBuilder
38
 *
39
 * @author Piotr Olaszewski <[email protected]>
40
 */
41
class AnnotationWSDLBuilder
42
{
43
    /**
44
     * @var string
45
     */
46
    private $class;
47
    /**
48
     * @var WSDLBuilder
49
     */
50
    private $builder;
51
    /**
52
     * @var AnnotationReader
53
     */
54
    private $annotationReader;
55
56
    public function __construct(string $class)
57 8
    {
58 1
        AnnotationRegistry::registerAutoloadNamespace('WSDL\Annotation', Path::join(__DIR__, '..', '..'));
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...sterAutoloadNamespace() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59 8
        $this->class = $class;
60 8
        $this->builder = WSDLBuilder::instance();
61 8
        $this->annotationReader = new AnnotationReader();
62 8
    }
63 8
64
    private function reflectionClass(): ReflectionClass
65
    {
66
        return new ReflectionClass($this->class);
67
    }
68 7
69
    public function build(): AnnotationWSDLBuilder
70 7
    {
71
        if (!class_exists($this->class)) {
72
            throw new Exception('Class [' . $this->class . '] not exists');
73
        }
74
        $this->buildForClass();
75
        $this->buildForMethods();
76
77
        return $this;
78 8
    }
79
80 8
    private function buildForClass(): void
81 2
    {
82
        $class = $this->reflectionClass();
83 7
        $webServiceAnnotation = $this->annotationReader->getClassAnnotation($class, WebService::class);
84 6
        if ($webServiceAnnotation === null) {
85 6
            throw new AnnotationBuilderException('Class must have @WebService annotation');
86
        }
87
        /** @var ClassAnnotation[] $classAnnotations */
88
        $classAnnotations = $this->annotationReader->getClassAnnotations($class);
89
        foreach ($classAnnotations as $classAnnotation) {
90
            $classAnnotation->build($this->builder, $class);
91
        }
92 7
    }
93
94 7
    private function buildForMethods(): void
95 7
    {
96 7
        $methods = [];
97 1
        $classMethods = $this->reflectionClass()->getMethods();
98
        foreach ($classMethods as $classMethod) {
99
            $webMethodAnnotation = $this->annotationReader->getMethodAnnotation($classMethod, WebMethod::class);
100 6
            if ($webMethodAnnotation === null) {
101 6
                continue;
102 6
            }
103 6
            $methodBuilder = MethodBuilder::instance();
104 6
            /** @var MethodAnnotation[] $methodAnnotations */
105
            $methodAnnotations = $this->annotationReader->getMethodAnnotations($classMethod);
106
            foreach ($methodAnnotations as $methodAnnotation) {
107
                $methodAnnotation->build($methodBuilder, $classMethod);
108
            }
109 6
            $methods[] = $methodBuilder->build();
110
        }
111 6
        $this->builder->setMethods($methods);
112 6
    }
113 6
114 5
    public function getBuilder(): WSDLBuilder
115 5
    {
116 1
        return $this->builder;
117
    }
118
}
119