AnnotationParser::parse()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 22
ccs 0
cts 10
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
1
<?php
2
3
/**
4
 * AppserverIo\RestApi\Parsers\OA2\AnnotationParser
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/restapi
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RestApi\Parsers\OA2;
22
23
use Swagger\Annotations\Get;
24
use Doctrine\Common\Annotations\AnnotationReader;
25
use AppserverIo\RestApi\Wrappers\OA2\OperationWrapper;
26
use AppserverIo\RestApi\Handlers\RequestHandlerInterface;
27
use AppserverIo\RestApi\Parsers\ConfigurationParserInterface;
28
use AppserverIo\Psr\Di\ObjectManagerInterface;
29
use AppserverIo\Psr\Application\ApplicationInterface;
30
use AppserverIo\Psr\EnterpriseBeans\Description\NameAwareDescriptorInterface;
31
32
/**
33
 * OpenApi 2.0 compatible annotation parser.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2018 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/appserver-io/restapi
39
 * @link      http://www.appserver.io
40
 */
41
class AnnotationParser implements ConfigurationParserInterface
42
{
43
44
    /**
45
     * The application instance.
46
     *
47
     * @var \AppserverIo\Psr\Application\ApplicationInterface
48
     */
49
    protected $application;
50
51
    /**
52
     * Returns the application instance.
53
     *
54
     * @return \AppserverIo\Psr\Application\ApplicationInterface The application instance
55
     */
56
    protected function getApplication()
57
    {
58
        return $this->application;
59
    }
60
61
    /**
62
     * Initializes the request handler with the passed instances.
63
     *
64
     * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
65
     */
66
    public function __construct(ApplicationInterface $application)
67
    {
68
        $this->application = $application;
69
    }
70
71
    /**
72
     * Parses the OpenApi annotations of the application's beans and adds the
73
     * found operations to the passed request handler.
74
     *
75
     * @param \AppserverIo\RestApi\Handlers\RequestHandlerInterface $requestHandler The request handler instance
76
     *
77
     * @return void
78
     */
79
    public function parse(RequestHandlerInterface $requestHandler)
80
    {
81
82
        // initialize the annotation reader instance
83
        $annotationReader = new AnnotationReader();
84
85
        // load the object manager from the application
86
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
87
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
88
89
        // iterate over all object descriptors and parse their methods for OpenApi annotations
90
        foreach ($objectManager->getObjectDescriptors() as $objectDescriptor) {
91
            // create a reflection class of the object descriptor's class name
92
            $reflectionClass = new \ReflectionClass($objectDescriptor->getClassName());
93
94
            // query wheter or not we've name aware object descriptor
95
            if ($objectDescriptor instanceof NameAwareDescriptorInterface) {
96
                // if yes, iterate over the methods
97
                foreach ($reflectionClass->getMethods() as $reflectionMethod) {
98
                    // adn add the get operation, if the method has the apropriate annotation
99
                    if ($operation = $annotationReader->getMethodAnnotation($reflectionMethod, Get::class)) {
100
                        $requestHandler->addOperation(new OperationWrapper($operation, $objectDescriptor, $reflectionMethod));
101
                    }
102
                }
103
            }
104
        }
105
    }
106
}
107