Test Setup Failed
Push — master ( cef6c0...9daa70 )
by Php Easy Api
04:33
created

App::repository()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 19
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
use Lingua\Lingua;
6
use Store\Services\Crypt;
0 ignored issues
show
Bug introduced by
The type Store\Services\Crypt was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Store\Services\Queue;
0 ignored issues
show
Bug introduced by
The type Store\Services\Queue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Store\Services\Redis as Redis;
0 ignored issues
show
Bug introduced by
The type Store\Services\Redis was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Resta\Cache\CacheManager as Cache;
10
use Store\Services\HttpSession as Session;
0 ignored issues
show
Bug introduced by
The type Store\Services\HttpSession was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Store\Services\DateCollection as Date;
0 ignored issues
show
Bug introduced by
The type Store\Services\DateCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Store\Services\AppCollection as Collection;
0 ignored issues
show
Bug introduced by
The type Store\Services\AppCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Resta\Foundation\PathManager\StaticPathModel;
14
15
class App
16
{
17
    /**
18
     * @var array
19
     */
20
    protected static $instance = [];
21
22
    /**
23
     * @var bool
24
     */
25
    protected static $reports = false;
26
27
    /**
28
     * @param $service
29
     * @param $arg
30
     * @param bool $reports
31
     * @return mixed|string
32
     */
33
    public static function annotationsLoaders($service,$arg,$reports=false)
34
    {
35
        static::$reports = $reports;
36
37
        //factory runner
38
        if($service=="factory"){
39
            return self::factory();
40
        }
41
        //if $name starts with $needles for repository
42
        if(Str::endsWith($service,'Repository')){
43
            return self::repository($service);
44
        }
45
46
        //if $name starts with $needles for source
47
        if(Str::endsWith($service,'Source')){
48
            return self::source($service,$arg);
49
        }
50
51
        //if $name starts with $needles for model
52
        if(Str::endsWith($service,'Builder')){
53
            return self::Builder(ucfirst($service));
54
        }
55
56
57
        if(method_exists(new self,$service)){
58
            return self::$service($arg);
59
        }
60
61
        exception()->badMethodCall($service.' invalid method or property');
62
63
    }
64
65
    /**
66
     * @return \Resta\Contracts\ApplicationContracts|\Resta\Contracts\ApplicationHelpersContracts|\Resta\Contracts\ContainerContracts
67
     */
68
    private static function app()
69
    {
70
        return app();
71
    }
72
73
    /**
74
     * client manager instance
75
     *
76
     * @return mixed
77
     */
78
    private static function client()
79
    {
80
        $clientManager = self::app()->namespace()->version().'\\ClientManager';
81
        return new $clientManager;
82
    }
83
84
    /**
85
     * @param $service
86
     * @return mixed
87
     */
88
    private static function entity($service)
89
    {
90
        //we are making a namespace assignment for the entity.
91
        $entity = app()->namespace()->model().'\Entity\EntityMap';
92
93
        //we are getting entity instance.
94
        return app()->resolve($entity);
95
    }
96
97
    private static function factory()
98
    {
99
        $factory = app()->namespace()->factory().'\Factory';
100
        return app()->resolve($factory);
101
    }
102
103
    /**
104
     * @param $service
105
     * @return mixed
106
     */
107
    private static function builder($service)
108
    {
109
        if(static::$reports){
110
            self::writeTrace(debug_backtrace(),'builders',$service);
111
        }
112
113
        //we are making a namespace assignment for the builder.
114
        $builder=app()->namespace()->builder().'\BuilderMap';
115
116
        //we are getting builder instance.
117
        return app()->resolve($builder);
118
    }
119
120
    /**
121
     * @return Cache
122
     */
123
    private static function cache()
124
    {
125
        return new Cache();
0 ignored issues
show
Bug introduced by
The call to Resta\Cache\CacheManager::__construct() has too few arguments starting with app. ( Ignorable by Annotation )

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

125
        return /** @scrutinizer ignore-call */ new Cache();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
126
    }
127
128
    /**
129
     * @return Collection
130
     */
131
    private static function collection()
132
    {
133
        return (new Collection());
134
    }
135
136
    /**
137
     * @param $instance
138
     * @param $class
139
     * @param array $bind
140
     * @return mixed
141
     */
142
    public function container($instance,$class,$bind=array())
143
    {
144
        if(!property_exists($instance->container(),$class)){
145
            throw new \InvalidArgumentException('container object false for ('.$class.') object');
146
        }
147
148
        $container=$instance->container()->{$class};
149
150
        if(!is_array($instance->container()->{$class}) AND Utils::isNamespaceExists($container)){
151
            return $instance->resolve($container,$bind);
152
        }
153
        return $instance->container()->{$class};
154
    }
155
156
    /**
157
     * @param $object
158
     */
159
    public function createAppInstance($object)
160
    {
161
        if(!defined('appInstance')){
162
            define('appInstance',(base64_encode(serialize($object))));
163
        }
164
    }
165
166
    /**
167
     * @param array $arg
168
     * @return mixed
169
     */
170
    private static function date($arg=array())
171
    {
172
        $locale = (count($arg)=="0") ? config('app.locale','en') : current($arg);
173
174
        return app()->resolve(Date::class)->setLocale($locale);
175
    }
176
177
    /**
178
     * @return mixed
179
     */
180
    private static function crypt()
181
    {
182
        return app()->resolve(Crypt::class);
183
    }
184
185
    /**
186
     * @return mixed
187
     */
188
    public static function getAppInstance()
189
    {
190
        //we save an instance for the entire application
191
        //and add it to the helper file to be accessed from anywhere in the application.
192
        if(!isset(self::$instance['appInstance'])){
193
            self::$instance['appInstance']=unserialize(base64_decode(appInstance));
194
            return self::$instance['appInstance'];
195
        }
196
        return self::$instance['appInstance'];
197
    }
198
199
    /**
200
     * @return \stdClass
201
     */
202
    public static function kernelBindObject()
203
    {
204
        return new \stdClass;
205
    }
206
207
    /**
208
     * @return Session
209
     */
210
    private static function session()
211
    {
212
        return new Session();
213
    }
214
215
    /**
216
     * @return mixed
217
     */
218
    private static function queue()
219
    {
220
        if(!isset(self::$instance['queue'])){
221
222
            self::$instance['queue']=(new Queue());
223
            return self::$instance['queue'];
224
225
        }
226
        return self::$instance['queue'];
227
    }
228
229
    /**
230
     * @param $service
231
     * @param bool $namespace
232
     * @return string
233
     */
234
    public static function repository($service,$namespace=false)
235
    {
236
        if(static::$reports){
237
            self::writeTrace(debug_backtrace(),'repositories',$service);
238
        }
239
240
        //I can get the repository name from the magic method as a salt repository,
241
        //after which we will edit it as an adapter namespace.
242
        $repositoryName=ucfirst(preg_replace('@Repository@is','',$service));
243
244
        //If we then configure the name of the simple repository to be an adapter
245
        //then we will give the user an example of the adapter class in each repository call.
246
        $repositoryAdapterName  = $repositoryName.'Adapter';
247
        $repositoryNamespace    = app()->namespace()->repository().'\\'.$repositoryName.'\\'.$repositoryAdapterName;
248
249
        if($namespace) return $repositoryNamespace;
250
251
        //and eventually we conclude the adapter class of the repository package as an instance.
252
        return app()->resolve($repositoryNamespace)->adapter();
253
    }
254
255
    /**
256
     * @param $trace
257
     */
258
    private static function writeTrace($trace,$type,$service)
259
    {
260
        if(self::app()->isLocale()){
261
            $trace = $trace[2];
262
263
            $time = time();
264
265
            JsonHandler::$file = path()->kernel().''.DIRECTORY_SEPARATOR.'Reports.json';
266
267
            $getJsonFile = JsonHandler::get('annotation.'.$type.'.'.$service.'.file');
268
269
            $filenamespace = Utils::getNamespace($trace['file']);
270
271
            if(!in_array($filenamespace,!is_array($getJsonFile) ? [] : $getJsonFile)){
0 ignored issues
show
introduced by
The condition is_array($getJsonFile) is always true.
Loading history...
272
                JsonHandler::set('annotation.'.$type.'.'.$service.'.file.'.$time,$filenamespace);
273
                JsonHandler::set('annotation.'.$type.'.'.$service.'.line.'.$time,$trace['line']);
274
            }
275
        }
276
    }
277
278
    /**
279
     * @param $service
280
     * @param $arg
281
     * @return mixed
282
     */
283
    private static function source($service,$arg)
284
    {
285
        //get Source path
286
        $service=ucfirst($service);
287
        $getCalledClass=str_replace('\\'.class_basename($arg[0]),'',get_class($arg[0]));
288
        $getCalledClass=class_basename($getCalledClass);
289
290
        $service=str_replace($getCalledClass,'',$service);
291
292
        //run service for endpoint
293
        $serviceSource=StaticPathModel::appSourceEndpoint().'\\'.$getCalledClass.'\\'.$service.'\Main';
294
        return app()->resolve($serviceSource);
295
    }
296
297
    /**
298
     * @return mixed
299
     */
300
    public static function redis()
301
    {
302
        if(!isset(self::$instance['redis'])){
303
304
            self::$instance['redis']=(new Redis())->client();
305
            return self::$instance['redis'];
306
307
        }
308
        return self::$instance['redis'];
309
    }
310
311
    /**
312
     * @param null $param
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $param is correct as it would always require null to be passed?
Loading history...
313
     * @return array|null|string
314
     */
315
    public function route($param=null)
316
    {
317
        $kernel=self::getAppInstance()->kernel;
318
319
        $saltRouteParameters=$kernel->routeParameters;
320
321
        if($param===null){
0 ignored issues
show
introduced by
The condition $param === null is always true.
Loading history...
322
            return $saltRouteParameters;
323
        }
324
325
        $saltRouteParameters = (self::app()->get('routeParams')) ?: $saltRouteParameters;
326
327
        return (isset($saltRouteParameters[$param])) ? strtolower($saltRouteParameters[$param]) : null;
328
329
330
    }
331
332
    /**
333
     * @param $data
334
     * @param array $select
335
     * @return mixed|string
336
     */
337
    public function translator($data,$select=array())
338
    {
339
        $languageDir = path()->appLanguage();
340
341
        $lang=(new Lingua($languageDir));
342
343
        if(self::app()->has('locale')){
344
            $defaultLocale = self::app()->get('locale');
345
        }
346
        else{
347
            $defaultLocale = config('app.locale');
348
        }
349
350
        if(!file_exists($languageDir.''.DIRECTORY_SEPARATOR.''.$defaultLocale)){
351
            $defaultLocale = config('app.fallback_locale');
352
        }
353
354
        if(count($select)){
355
            return $lang->include(['default'])->locale($defaultLocale)->get($data,$select);
356
        }
357
358
        return $lang->include(['default'])->locale($defaultLocale)->get($data);
359
    }
360
361
}