1 <?php
2 3 4
5 namespace Team3\PayU\PropertyExtractor\Reader;
6
7 use Doctrine\Common\Annotations\Reader;
8 use Psr\Log\LoggerInterface;
9 use ReflectionClass;
10 use ReflectionMethod;
11 use Team3\PayU\Annotation\PayU;
12 use Team3\PayU\PropertyExtractor\ExtractorException;
13
14 class AnnotationReader implements ReaderInterface
15 {
16 const ANNOTATION_CLASS = 'Team3\PayU\Annotation\PayU';
17
18 19 20
21 private $reader;
22
23 24 25
26 private $logger;
27
28 29 30 31
32 public function __construct(
33 Reader $reader,
34 LoggerInterface $logger
35 ) {
36 $this->reader = $reader;
37 $this->logger = $logger;
38 }
39
40 41 42
43 public function read($object)
44 {
45 try {
46 $reflectionClass = new ReflectionClass($object);
47 } catch (\ReflectionException $exception) {
48 throw new ExtractorException(
49 $exception->getMessage(),
50 $exception->getCode(),
51 $exception
52 );
53 }
54
55 $read = [];
56
57 foreach ($this->getMethods($reflectionClass) as $reflectionMethod) {
58
59 $methodAnnotation = $this
60 ->reader
61 ->getMethodAnnotation($reflectionMethod, self::ANNOTATION_CLASS);
62
63 if (!is_object($methodAnnotation)) {
64 continue;
65 }
66
67 $readerResult = new ReaderResult(
68 $reflectionMethod->getName(),
69 $methodAnnotation->getPropertyName()
70 );
71 $read[] = $readerResult;
72 $this->logReaderResult($readerResult, $object);
73 }
74
75 return $read;
76 }
77
78 79 80 81 82
83 private function getMethods(ReflectionClass $reflectionClass)
84 {
85 return $reflectionClass->getMethods();
86 }
87
88 89 90 91
92 private function logReaderResult(
93 ReaderResultInterface $readerResult,
94 $object
95 ) {
96 $this
97 ->logger
98 ->debug(sprintf(
99 '%s found result on object %s method %s with property name %s',
100 get_class($this),
101 get_class($object),
102 $readerResult->getMethodName(),
103 $readerResult->getPropertyName()
104 ));
105 }
106 }
107