Passed
Push — develop ( d01060...4713f8 )
by Paul
14:54
created

InitialsAvatar::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
ccs 17
cts 17
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Avatars;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Text;
7
8
class InitialsAvatar extends AbstractSvgAvatar
9
{
10 18
    public function generate(string $from): string
11
    {
12 18
        $colors = [
13 18
            ['background' => '#e3effb', 'color' => '#134d92'], // blue
14 18
            ['background' => '#e1f0ee', 'color' => '#125960'], // green
15 18
            ['background' => '#ffeff7', 'color' => '#ba3a80'], // pink
16 18
            ['background' => '#fcece3', 'color' => '#a14326'], // red
17 18
            ['background' => '#faf7d9', 'color' => '#da9640'], // yellow
18 18
        ];
19 18
        $colors = glsr()->filterArray('avatar/colors', $colors);
20 18
        shuffle($colors);
21 18
        $color = Arr::getAs('array', $colors, 0);
22 18
        $data = wp_parse_args($color, [
23 18
            'background' => '#dcdce6',
24 18
            'color' => '#6f6f87',
25 18
            'text' => $this->filename($from),
26 18
        ]);
27 18
        return trim(glsr()->build('avatar', $data));
28
    }
29
30 18
    protected function filename(string $from): string
31
    {
32 18
        $initials = Text::initials((string) $from);
33 18
        if (1 === mb_strlen($initials)) {
34 1
            $initials = mb_substr(trim($from), 0, 2, 'UTF-8');
35 1
            $initials = mb_strtoupper($initials, 'UTF-8');
36
        }
37 18
        $initials = mb_substr($initials, 0, 2, 'UTF-8');
38 18
        return $initials;
39
    }
40
}
41