Passed
Push — master ( a74354...254a60 )
by Andrea
18:29 queued 11s
created

ApiUtils   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 32.14%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
dl 0
loc 190
ccs 18
cts 56
cp 0.3214
rs 10
c 3
b 0
f 0
wmc 19

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelControllerClass() 0 4 1
A getUpdateItem() 0 3 1
A regexPathModels() 0 3 1
A apiModels() 0 20 3
A __construct() 0 10 1
A getFormClass() 0 4 1
A getApiControllerClass() 0 4 1
A namespaceModels() 0 3 1
A getAll() 0 3 1
A bundlesPath() 0 3 1
A getAllToString() 0 3 1
A getItem() 0 3 1
A getCreate() 0 3 1
A getCount() 0 3 1
A namespacePrefix() 0 3 1
A getDelete() 0 3 1
A getModelClass() 0 4 1
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Api;
4
5
use Symfony\Component\Finder\Finder;
6
use hanneskod\classtools\Iterator\ClassIterator;
7
8
class ApiUtils
9
{
10
11
    private $getAll;
12
    private $getCount;
13
    private $create;
14
    private $apiCollection;
15
    //namespaces
16
    private static $namespacePrefix = 'Swagger';
17
    private static $namespaceApi = 'Api';
18
    private static $namespaceModel = "Model";
19
    private static $namespaceForm = "App\\Form";
20
    //suffix and prefix
21
    private static $suffixApiController = 'Api';
22
    private static $prefixControllerModelItem = "DaosRow";
23
    private static $prefixModelItem = "DaosRow";
24
25
    //TODO: evaluate to move this variable into configs
26
    private static $regexPathModels = '/Swagger\\\(.*)\\\Model\\\DaosRow/';
27
28
    public function __construct($apiCollection)
29
    {
30
        $this->apiCollection = lcfirst($apiCollection);
31
        $this->getAll = "ControllerReadAll";
32
        $this->getCount = "ControllerCount";
33
        $this->create = "ControllerCreate";
34
        $this->delete = "ControllerDeleteItem";
0 ignored issues
show
Bug Best Practice introduced by
The property delete does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
        $this->get = "ControllerReadItem";
0 ignored issues
show
Bug Best Practice introduced by
The property get does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->update = "ControllerUpdateItem";
0 ignored issues
show
Bug Best Practice introduced by
The property update does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->getAllToString = "ControllerReadAllToString";
0 ignored issues
show
Bug Best Practice introduced by
The property getAllToString does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
    }
39
40
    /**
41
     * Return path where core bundle will look for api models
42
     */
43 1
    public function bundlesPath()
44
    {
45 1
        return dirname(__FILE__) . '/../../../../../vendor/fi';
46
    }
47
48
    /**
49
     * Return namespace prefix for api external bundles, i.e. Swagger
50
     */
51
    public static function namespacePrefix()
52
    {
53
        return self::$namespacePrefix;
54
    }
55
56
    /**
57
     * Return namespace component for api models of external bundles, i.e. \\\Model\\\Models*
58
     */
59
    public static function namespaceModels()
60
    {
61
        return self::$namespaceModel;
62
    }
63
64
    /**
65
     * Return the name of Api Controller.
66
     * Given the project name (i.e. Insurance) and the collection name (i.e. Claims) it returns the complete path of API controller
67
     * class (i.e. \\Swagger\\Insurance\\Api\\ClaimsApi)
68
     */
69
    public static function getApiControllerClass($project, $entityName): String
70
    {
71
        $className = "\\" . self::$namespacePrefix . "\\$project\\" . self::$namespaceApi . "\\$entityName" . self::$suffixApiController;
72
        return $className;
73
    }
74
75
    /**
76
     * Return the name of Model Controller.
77
     * Given the project name (i.e. Insurance) and the model name (i.e. Claim) it returns the complete path of API Model controller item
78
     * class (i.e. \\Swagger\\Insurance\\Model\\ControllersItemClaim)
79
     */
80
    public static function getModelControllerClass($project, $modelName): String
81
    {
82
        $className = "\\" . self::$namespacePrefix . "\\$project\\" . self::$namespaceModel . "\\" . self::$prefixControllerModelItem . $modelName;
83
        return $className;
84
    }
85
86
    /**
87
     * Return the name of Model Controller.
88
     * Given the project name (i.e. Insurance) and the model name (i.e. Claim) it returns the complete path of API Model controller item
89
     * class (i.e. \\Swagger\\Insurance\\Model\\ModelsClaim)
90
     */
91 1
    public static function getModelClass($project, $modelName): String
92
    {
93 1
        $className = "\\" . self::$namespacePrefix . "\\$project\\" . self::$namespaceModel . "\\" . self::$prefixModelItem . $modelName;
94 1
        return $className;
95
    }
96
97
    /**
98
     * Return the name of Form class.
99
     * Given the model name (i.e. Claim) it returns the complete path of API Model controller item
100
     * class (i.e. App\\Form\\Claim)
101
     */
102
    public static function getFormClass($modelName): String
103
    {
104
        $className = self::$namespaceForm . "\\" . $modelName;
105
        return $className;
106
    }
107
108
    /**
109
     * Return namespace component for api models of external bundles, i.e. \\\Model\\\Models*
110
     */
111 1
    public function regexPathModels()
112
    {
113 1
        return self::$regexPathModels;
114
    }
115
116
    /**
117
     * Return the method string to retrieve all elements / or filtering on them
118
     */
119
    public function getAll(): String
120
    {
121
        return $this->apiCollection . $this->getAll;
122
    }
123
124
    /**
125
     * Return the method string to retrieve all elements descriptions (it's possible to filter them as for getAll)
126
     */
127
    public function getAllToString(): String
128
    {
129
        return $this->apiCollection . $this->getAllToString;
130
    }
131
132
    /**
133
     * Return the method string to retrieve 1 element
134
     */
135
    public function getItem(): String
136
    {
137
        return $this->apiCollection . $this->get;
138
    }
139
140
    /**
141
     * Return the method string to update 1 element
142
     */
143
    public function getUpdateItem(): String
144
    {
145
        return $this->apiCollection . $this->update;
146
    }
147
148
    /**
149
     * Return the method string to count all elemements inside a collection
150
     */
151
    public function getCount(): String
152
    {
153
        return $this->apiCollection . $this->getCount;
154
    }
155
156
    /**
157
     * Return the method string to create an element
158
     */
159
    public function getCreate(): String
160
    {
161
        return $this->apiCollection . $this->create;
162
    }
163
164
    /**
165
     * Return the method string to delete an element
166
     */
167
    public function getDelete(): String
168
    {
169
        return $this->apiCollection . $this->delete;
170
    }
171
172
    /**
173
     * It looks for Models existent into included external bundles.
174
     * It uses ApiUtils in order to know where to search and what look for.
175
     *
176
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
177
     */
178 1
    public function apiModels(): array
179
    {
180
        //where to look for
181 1
        $path = self::bundlesPath();
0 ignored issues
show
Bug Best Practice introduced by
The method Cdf\BiCoreBundle\Utils\Api\ApiUtils::bundlesPath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
        /** @scrutinizer ignore-call */ 
182
        $path = self::bundlesPath();
Loading history...
182 1
        $regex = self::regexPathModels();
0 ignored issues
show
Bug Best Practice introduced by
The method Cdf\BiCoreBundle\Utils\A...tils::regexPathModels() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
        /** @scrutinizer ignore-call */ 
183
        $regex = self::regexPathModels();
Loading history...
183
        //what to look for
184 1
        $models = array();
185 1
        $finder = new Finder;
186 1
        $iter = new ClassIterator($finder->in($path));
187
188 1
        $matches = array();
189
        // Print the file names of classes, interfaces and traits in given path
190 1
        foreach ($iter->getClassMap() as $classname => $splFileInfo) {
191 1
            preg_match($regex, $classname, $matches);
192
193 1
            if (count($matches) > 0) {
194
                $models[] = $matches[1] . '.' . substr($classname, strlen($matches[0])) . ' (API)';
195
            }
196
        }
197 1
        return $models;
198
    }
199
}
200