Completed
Push — master ( 90c255...29f0bf )
by Alex
30s queued 11s
created

MetadataBaseProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
eloc 26
c 2
b 0
f 1
dl 0
loc 65
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsCaching() 0 3 1
A handlePostBoot() 0 11 4
A getClassMap() 0 13 3
A checkClassMap() 0 5 2
A getAppNamespace() 0 8 2
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Providers;
4
5
use Illuminate\Support\Facades\App;
6
use Illuminate\Support\Facades\Cache;
7
use Illuminate\Support\ServiceProvider;
8
9
abstract class MetadataBaseProvider extends ServiceProvider
10
{
11
12
    /**
13
     * @return bool
14
     */
15
    protected function getIsCaching()
16
    {
17
        return true === env('APP_METADATA_CACHING', false);
18
    }
19
20
    /**
21
     * @param bool      $isCaching
22
     * @param bool|null $hasCache
23
     * @param string    $key
24
     * @param $meta
25
     */
26
    protected function handlePostBoot(bool $isCaching, $hasCache, string $key, $meta)
27
    {
28
        if (!$isCaching) {
29
            Cache::forget($key);
30
            return;
31
        }
32
        $hasCache = isset($hasCache) ? boolval($hasCache) : false;
33
        if (!$hasCache) {
34
            $cacheTime = abs(intval(env('APP_METADATA_CACHE_DURATION', 10)));
35
            $cacheTime = max($cacheTime, 1);
36
            Cache::put($key, $meta, $cacheTime);
37
        }
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    protected function getClassMap()
44
    {
45
        $classes = get_declared_classes();
46
        $autoClass = null;
47
        foreach ($classes as $class) {
48
            if (\Illuminate\Support\Str::startsWith($class, 'Composer\\Autoload\\ComposerStaticInit')) {
49
                $autoClass = $class;
50
            }
51
        }
52
53
        $classes = $autoClass::$classMap;
54
        $this->checkClassMap($classes);
55
        return array_keys($classes);
56
    }
57
58
    protected function checkClassMap($classMap)
59
    {
60
        if (!isset($classMap[__CLASS__])) {
61
            throw new \Exception(sprintf('%s was not found in autoload class map, this usually indicates you '.
62
            'need to dump an opimized autoloader (`composer dump-autoload -o`)', __CLASS__));
63
        }
64
    }
65
66
    protected function getAppNamespace()
67
    {
68
        try {
69
            $startName = App::getNamespace();
70
        } catch (\Exception $e) {
71
            $startName = 'App';
72
        }
73
        return $startName;
74
    }
75
}
76