SocialLogin::addProviderLoginLink()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Bone\SocialAuth\View\Extension;
4
5
use Del\Icon;
6
use League\Plates\Engine;
7
use League\Plates\Extension\ExtensionInterface;
8
9
class SocialLogin implements ExtensionInterface
10
{
11
    /**
12
     * @var array $config
13
     */
14
    private $config;
15
16
    /**
17
     * @var array $customProviderConfigs
18
     */
19
    private $customProviderConfigs;
20
21
    /**
22
     * SocialLogin constructor.
23
     * @param array $config
24
     */
25 3
    public function __construct(array $config)
26
    {
27 3
        $this->config = $config;
28
29 3
        if (array_key_exists('custom', $this->config)) {
30
            $this->customProviderConfigs = $this->config['custom'];
31
            unset ($this->config['custom']);
32
        }
33
    }
34
35
    /**
36
     * @param Engine $engin
37
     */
38 1
    public function register(Engine $engine)
39
    {
40 1
        $engine->registerFunction('socialAuth', [$this, 'socialAuth']);
41
    }
42
43
    /**
44
     * @return string
45
     */
46 1
    public function socialAuth(): string
47
    {
48 1
        $html = '';
49
50 1
        foreach ($this->config['providers'] as $provider => $data) {
51 1
            $html .= $this->addProviderLoginLink(strtolower($provider), $data);
52
        }
53
54 1
        if (isset($this->customProviderConfigs)) {
55
            foreach ($this->customProviderConfigs['providers'] as $provider => $data) {
56
                $html .= $this->addCustomProviderLoginLink(strtolower($provider), $data);
57
            }
58
        }
59
        
60 1
        return $html;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 1
    private function addProviderLoginLink(string $provider, array $data): string
67
    {
68 1
        if (!$data['enabled']) {
69 1
            return '';
70
        }
71
72 1
        $icon = $this->getIcon($provider);
73 1
        $color = $this->getColor($provider);
74
75 1
        return '<a class="btn btn-primary rounded-circle m5" style="background-color: ' . $color . '" href="/user/login/via/' . $provider . '">' . $icon  . '</a>';
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    private function addCustomProviderLoginLink(string $provider, array $data): string
82
    {
83
        if (!$data['enabled']) {
84
            return '';
85
        }
86
87
        $icon = $data['icon'];
88
        $color = $data['color'];
89
90
        return '<a class="btn btn-primary rounded-circle m5" style="background-color: ' . $color . '" href="/user/login/via/' . $provider . '">' . $icon  . '</a>';
91
    }
92
93
    /**
94
     * @param string $provider
95
     * @return string
96
     */
97 1
    private function getIcon(string $provider): string
98
    {
99 1
        switch ($provider) {
100 1
            case 'facebook':
101 1
                $icon = Icon::FACEBOOK;
102 1
                break;
103 1
            case 'github':
104 1
                $icon = Icon::GITHUB;
105 1
                break;
106 1
            case 'twitter':
107 1
                $icon = Icon::TWITTER;
108 1
                break;
109 1
            case 'google':
110 1
                $icon = Icon::GOOGLE;
111 1
                break;
112
            default;
113 1
                $icon = '';
114 1
                break;
115
        }
116
117 1
        return $icon;
118
    }
119
120
    /**
121
     * @param string $provider
122
     * @return string
123
     */
124 1
    private function getColor(string $provider): string
125
    {
126 1
        switch ($provider) {
127 1
            case 'facebook':
128 1
                $color = '#2d88ff';
129 1
                break;
130 1
            case 'github':
131 1
                $color = '#7c007c';
132 1
                break;
133 1
            case 'twitter':
134 1
                $color = '#1da1f2';
135 1
                break;
136 1
            case 'google':
137 1
                $color = '#ea4335';
138 1
                break;
139
            default;
140 1
                $color = '';
141 1
                break;
142
        }
143
144 1
        return $color;
145
    }
146
}
147
148