Test Failed
Push — master ( 306f11...77481f )
by Fran
14:04
created

DocumentorService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 28
Bugs 2 Features 0
Metric Value
eloc 33
c 28
b 2
f 0
dl 0
loc 69
ccs 0
cts 28
cp 0
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
B extractApiInfo() 0 20 8
A extractApiEndpoints() 0 21 5
1
<?php
2
3
namespace PSFS\services;
4
5
use Exception;
6
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
7
use PSFS\base\dto\Dto;
8
use PSFS\base\Logger;
9
use PSFS\base\Router;
10
use PSFS\base\types\helpers\I18nHelper;
11
use PSFS\base\types\helpers\InjectorHelper;
12
use PSFS\base\types\SimpleService;
13
use PSFS\base\types\traits\Api\DocumentorHelperTrait;
14
use PSFS\base\types\traits\Api\SwaggerFormaterTrait;
15
use ReflectionClass;
16
use ReflectionException;
17
use ReflectionMethod;
18
use SplFileInfo;
19
use Symfony\Component\Finder\Finder;
20
21
/**
22
 * Class DocumentorService
23
 * @package PSFS\services
24
 */
25
class DocumentorService extends SimpleService
26
{
27
    use DocumentorHelperTrait;
28
    use SwaggerFormaterTrait;
29
30
    const DTO_INTERFACE = Dto::class;
31
    const MODEL_INTERFACE = ActiveRecordInterface::class;
32
33
    /**
34
     * @Injectable
35
     * @var \PSFS\base\Router route
36
     */
37
    protected $route;
38
39
    /**
40
     * Method that extract all endpoints for each module
41
     * @param array $module
42
     * @return array
43
     * @throws ReflectionException
44
     */
45
    public function extractApiEndpoints(array $module)
46
    {
47
        $modulePath = $module['path'] . DIRECTORY_SEPARATOR . 'Api';
48
        $moduleName = $module['name'];
49
        $endpoints = [];
50
        if (file_exists($modulePath)) {
51
            $finder = new Finder();
52
            $finder->files()->in($modulePath)->depth('< 2')->name('*.php');
53
            if (count($finder)) {
54
                /** @var SplFileInfo $file */
55
                foreach ($finder as $file) {
56
                    $filename = str_replace([$modulePath, '/'], ['', '\\'], $file->getPathname());
57
                    $namespace = "\\{$moduleName}\\Api" . str_replace('.php', '', $filename);
58
                    $info = $this->extractApiInfo($namespace, $moduleName);
59
                    if (!empty($info)) {
60
                        $endpoints[$namespace] = $info;
61
                    }
62
                }
63
            }
64
        }
65
        return $endpoints;
66
    }
67
68
    /**
69
     * @param $namespace
70
     * @param $module
71
     * @return array
72
     * @throws ReflectionException
73
     */
74
    public function extractApiInfo($namespace, $module)
75
    {
76
        $info = [];
77
        if (Router::exists($namespace) && !I18nHelper::checkI18Class($namespace)) {
78
            $reflection = new ReflectionClass($namespace);
79
            $visible = InjectorHelper::checkIsVisible($reflection->getDocComment());
80
            if ($visible && $reflection->isInstantiable()) {
81
                foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
82
                    try {
83
                        $mInfo = $this->extractMethodInfo($namespace, $method, $reflection, $module);
84
                        if (NULL !== $mInfo) {
85
                            $info[] = $mInfo;
86
                        }
87
                    } catch (Exception $e) {
88
                        Logger::log($e->getMessage(), LOG_ERR);
89
                    }
90
                }
91
            }
92
        }
93
        return $info;
94
    }
95
}
96