SocialProvidersLoader::getSocialProviders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MadWeb\SocialAuth;
4
5
use Illuminate\Contracts\Cache\Repository;
6
use Illuminate\Contracts\Events\Dispatcher;
7
8
class SocialProvidersLoader
9
{
10
    /**
11
     * @var Repository
12
     */
13
    protected $cache;
14
15
    /**
16
     * @var string
17
     */
18
    protected $cacheKey = 'mad-web.social-providers';
19
20
    /**
21
     * @var Dispatcher
22
     */
23
    protected $dispatcher;
24
25
    /**
26
     * @var string
27
     */
28
    protected $social_model;
29
30
    /**
31
     * SocialProvidersLoader constructor.
32
     * @param Repository $cache
33
     * @param Dispatcher $dispatcher
34
     */
35 57
    public function __construct(Repository $cache, Dispatcher $dispatcher)
36
    {
37 57
        $this->cache = $cache;
38 57
        $this->dispatcher = $dispatcher;
39
40 57
        $this->social_model = config('social-auth.models.social');
41 57
    }
42
43
    /**
44
     * Get available social providers.
45
     *
46
     * @return \Illuminate\Database\Eloquent\Collection
47
     */
48
    public function getSocialProviders()
49
    {
50
        return $this->cache->rememberForever($this->cacheKey, function () {
51
            $model = $this->social_model;
52
53
            return $model::all();
54
        });
55
    }
56
57
    /**
58
     * Forget cached social providers.
59
     */
60 57
    public function forgetSocialProviders()
61
    {
62 57
        $this->cache->forget($this->cacheKey);
63 57
    }
64
65
    /**
66
     * Register additional social providers.
67
     */
68 57
    public function registerSocialProviders()
69
    {
70 57
        $this->registerCacheRefresher();
71
72 57
        foreach (config('social-auth.providers') as $provider) {
73
            $this->dispatcher->listen(
74
                \SocialiteProviders\Manager\SocialiteWasCalled::class,
75
                $provider.'@handle'
76
            );
77
        }
78 57
    }
79
80
    /**
81
     * Remove cache data on social providers table update.
82
     */
83 57
    protected function registerCacheRefresher()
84
    {
85 57
        $model = $this->social_model;
86
87 57
        $ClearCacheFunction = function () {
88 57
            $this->forgetSocialProviders();
89 57
        };
90
91
        $model::created($ClearCacheFunction);
92
        $model::updated($ClearCacheFunction);
93
        $model::deleted($ClearCacheFunction);
94
    }
95
}
96