Completed
Push — master ( 066cec...0625cf )
by Piotr
03:30
created

AnnotationWSDLBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 0 Features 2
Metric Value
wmc 11
c 10
b 0
f 2
lcom 1
cbo 8
dl 0
loc 93
rs 10
ccs 41
cts 41
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A reflectionClass() 0 4 1
A build() 0 6 1
A buildForClass() 0 13 3
A buildForMethods() 0 19 4
A getBuilder() 0 4 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 Ouzo\Utilities\Path;
29
use ReflectionClass;
30
use WSDL\Annotation\ClassAnnotation;
31
use WSDL\Annotation\MethodAnnotation;
32
33
/**
34
 * AnnotationWSDLBuilder
35
 *
36
 * @author Piotr Olaszewski <[email protected]>
37
 */
38
class AnnotationWSDLBuilder
39
{
40
    /**
41
     * @var string
42
     */
43
    private $class;
44
    /**
45
     * @var WSDLBuilder
46
     */
47
    private $builder;
48
    /**
49
     * @var AnnotationReader
50
     */
51
    private $annotationReader;
52
53
    /**
54
     * @param string $class
55
     */
56 7
    public function __construct($class)
57 1
    {
58 7
        AnnotationRegistry::registerAutoloadNamespace('WSDL\Annotation', Path::join(__DIR__, '..', '..'));
59 7
        $this->class = $class;
60 7
        $this->builder = WSDLBuilder::instance();
61 7
        $this->annotationReader = new AnnotationReader();
62 7
    }
63
64
    /**
65
     * @return ReflectionClass
66
     */
67 7
    private function reflectionClass()
68
    {
69 7
        return new ReflectionClass($this->class);
70
    }
71
72
    /**
73
     * @return $this
74
     */
75 7
    public function build()
76
    {
77 7
        $this->buildForClass();
78 6
        $this->buildForMethods();
79 6
        return $this;
80
    }
81
82
    /**
83
     * @return void
84
     * @throws AnnotationBuilderException
85
     */
86 7
    private function buildForClass()
87
    {
88 7
        $class = $this->reflectionClass();
89 7
        $webServiceAnnotation = $this->annotationReader->getClassAnnotation($class, '\WSDL\Annotation\WebService');
90 7
        if ($webServiceAnnotation === null) {
91 1
            throw new AnnotationBuilderException('Class must have @WebService annotation');
92
        }
93
        /** @var ClassAnnotation[] $classAnnotations */
94 6
        $classAnnotations = $this->annotationReader->getClassAnnotations($class);
95 6
        foreach ($classAnnotations as $classAnnotation) {
96 6
            $classAnnotation->build($this->builder, $class);
97 6
        }
98 6
    }
99
100
    /**
101
     * @return void
102
     */
103 6
    private function buildForMethods()
104
    {
105 6
        $classMethods = $this->reflectionClass()->getMethods();
106 6
        $methods = [];
107 6
        foreach ($classMethods as $classMethod) {
108 5
            $webMethodAnnotation = $this->annotationReader->getMethodAnnotation($classMethod, '\WSDL\Annotation\WebMethod');
109 5
            if ($webMethodAnnotation === null) {
110 1
                continue;
111
            }
112 5
            $methodBuilder = MethodBuilder::instance();
113
            /** @var MethodAnnotation[] $methodAnnotations */
114 5
            $methodAnnotations = $this->annotationReader->getMethodAnnotations($classMethod);
115 5
            foreach ($methodAnnotations as $methodAnnotation) {
116 5
                $methodAnnotation->build($methodBuilder, $classMethod);
117 5
            }
118 5
            $methods[] = $methodBuilder->build();
119 6
        }
120 6
        $this->builder->setMethods($methods);
121 6
    }
122
123
    /**
124
     * @return WSDLBuilder
125
     */
126 6
    public function getBuilder()
127
    {
128 6
        return $this->builder;
129
    }
130
}
131