Completed
Push — master ( 20a550...cf14de )
by Piotr
03:18
created

AnnotationWSDLBuilder::getBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Copyright (C) 2013-2016
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
34
/**
35
 * AnnotationWSDLBuilder
36
 *
37
 * @author Piotr Olaszewski <[email protected]>
38
 */
39
class AnnotationWSDLBuilder
40
{
41
    /**
42
     * @var string
43
     */
44
    private $class;
45
    /**
46
     * @var WSDLBuilder
47
     */
48
    private $builder;
49
    /**
50
     * @var AnnotationReader
51
     */
52
    private $annotationReader;
53
54
    /**
55
     * @param string $class
56
     */
57 8
    public function __construct($class)
58 1
    {
59 8
        AnnotationRegistry::registerAutoloadNamespace('WSDL\Annotation', Path::join(__DIR__, '..', '..'));
60 8
        $this->class = $class;
61 8
        $this->builder = WSDLBuilder::instance();
62 8
        $this->annotationReader = new AnnotationReader();
63 8
    }
64
65
    /**
66
     * @return ReflectionClass
67
     */
68 7
    private function reflectionClass()
69
    {
70 7
        return new ReflectionClass($this->class);
71
    }
72
73
    /**
74
     * @return $this
75
     * @throws AnnotationBuilderException
76
     * @throws Exception
77
     */
78 8
    public function build()
79
    {
80 8
        if (!class_exists($this->class)) {
81 2
            throw new Exception('Class [' . $this->class . '] not exists');
82
        }
83 7
        $this->buildForClass();
84 6
        $this->buildForMethods();
85 6
        return $this;
86
    }
87
88
    /**
89
     * @return void
90
     * @throws AnnotationBuilderException
91
     */
92 7
    private function buildForClass()
93
    {
94 7
        $class = $this->reflectionClass();
95 7
        $webServiceAnnotation = $this->annotationReader->getClassAnnotation($class, '\WSDL\Annotation\WebService');
96 7
        if ($webServiceAnnotation === null) {
97 1
            throw new AnnotationBuilderException('Class must have @WebService annotation');
98
        }
99
        /** @var ClassAnnotation[] $classAnnotations */
100 6
        $classAnnotations = $this->annotationReader->getClassAnnotations($class);
101 6
        foreach ($classAnnotations as $classAnnotation) {
102 6
            $classAnnotation->build($this->builder, $class);
103 6
        }
104 6
    }
105
106
    /**
107
     * @return void
108
     */
109 6
    private function buildForMethods()
110
    {
111 6
        $classMethods = $this->reflectionClass()->getMethods();
112 6
        $methods = [];
113 6
        foreach ($classMethods as $classMethod) {
114 5
            $webMethodAnnotation = $this->annotationReader->getMethodAnnotation($classMethod, '\WSDL\Annotation\WebMethod');
115 5
            if ($webMethodAnnotation === null) {
116 1
                continue;
117
            }
118 5
            $methodBuilder = MethodBuilder::instance();
119
            /** @var MethodAnnotation[] $methodAnnotations */
120 5
            $methodAnnotations = $this->annotationReader->getMethodAnnotations($classMethod);
121 5
            foreach ($methodAnnotations as $methodAnnotation) {
122 5
                $methodAnnotation->build($methodBuilder, $classMethod);
123 5
            }
124 5
            $methods[] = $methodBuilder->build();
125 6
        }
126 6
        $this->builder->setMethods($methods);
127 6
    }
128
129
    /**
130
     * @return WSDLBuilder
131
     */
132 6
    public function getBuilder()
133
    {
134 6
        return $this->builder;
135
    }
136
}
137