Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
28 / 32 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AnnotationReaderService | |
87.50% |
28 / 32 |
|
0.00% |
0 / 3 |
12.28 | |
0.00% |
0 / 1 |
| getAnnotations | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| readAnnotations | |
90.48% |
19 / 21 |
|
0.00% |
0 / 1 |
6.03 | |||
| getCacheService | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2019 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Alexander Zagovorichev <zagovorichev@1pt.com> |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\routing; |
| 24 | |
| 25 | use Doctrine\Common\Annotations\AnnotationReader; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\routing\AnnotationReader\requiredRights; |
| 28 | use oat\tao\model\routing\AnnotationReader\security; |
| 29 | use ReflectionClass; |
| 30 | use ReflectionMethod; |
| 31 | |
| 32 | class AnnotationReaderService extends ConfigurableService |
| 33 | { |
| 34 | public const SERVICE_ID = 'tao/AnnotationReaderService'; |
| 35 | |
| 36 | public const KEY_PREFIX = 'routeAnnotation_'; |
| 37 | public const PROP_RIGHTS = 'required_rights'; |
| 38 | public const PROP_SECURITY = 'security'; |
| 39 | |
| 40 | /** |
| 41 | * @var \common_cache_Cache |
| 42 | */ |
| 43 | private $cacheService; |
| 44 | |
| 45 | /** |
| 46 | * @param $className |
| 47 | * @param $methodName |
| 48 | * @return array|mixed |
| 49 | * @throws \common_cache_NotFoundException |
| 50 | */ |
| 51 | public function getAnnotations($className, $methodName) |
| 52 | { |
| 53 | $annotationKey = self::KEY_PREFIX . $className . $methodName; |
| 54 | if ($this->getCacheService()->has($annotationKey)) { |
| 55 | $annotation = json_decode($this->getCacheService()->get($annotationKey), true); |
| 56 | } else { |
| 57 | $annotation = $this->readAnnotations($className, $methodName); |
| 58 | $this->getCacheService()->put(json_encode($annotation), $annotationKey); |
| 59 | } |
| 60 | return $annotation; |
| 61 | } |
| 62 | |
| 63 | private function readAnnotations($className, $methodName) |
| 64 | { |
| 65 | $rules = [ |
| 66 | self::PROP_RIGHTS => [], |
| 67 | self::PROP_SECURITY => [], |
| 68 | ]; |
| 69 | try { |
| 70 | // we need to define class |
| 71 | // we need to change autoloader file without this, on each environment |
| 72 | new requiredRights(); |
| 73 | new security(); |
| 74 | $annotationReader = new AnnotationReader(); |
| 75 | |
| 76 | if ($methodName) { |
| 77 | $reflectionMethod = new ReflectionMethod($className, $methodName); |
| 78 | $annotations = $annotationReader->getMethodAnnotations($reflectionMethod); |
| 79 | } else { |
| 80 | $reflectionClass = new ReflectionClass($className); |
| 81 | $annotations = $annotationReader->getClassAnnotations($reflectionClass); |
| 82 | } |
| 83 | foreach ($annotations as $annotation) { |
| 84 | switch (get_class($annotation)) { |
| 85 | case requiredRights::class: |
| 86 | $rules[self::PROP_RIGHTS][] = (array) $annotation; |
| 87 | break; |
| 88 | case security::class: |
| 89 | $rules[self::PROP_SECURITY][] = $annotation->value; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } catch (\Exception $e) { |
| 94 | $this->logNotice('Undefined annotation: ' . $e->getMessage()); |
| 95 | } |
| 96 | return $rules; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return \common_cache_Cache |
| 101 | */ |
| 102 | private function getCacheService() |
| 103 | { |
| 104 | if (!$this->cacheService) { |
| 105 | if ($this->hasOption('cacheService') && $this->getOption('cacheService') instanceof \common_cache_Cache) { |
| 106 | $this->cacheService = $this->getOption('cacheService'); |
| 107 | } else { |
| 108 | $this->cacheService = $this->getServiceLocator()->get(\common_cache_Cache::SERVICE_ID); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $this->cacheService; |
| 113 | } |
| 114 | } |