Avatar::toHtml()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.9332
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\OAuth2\Client;
5
6
final class Avatar
7
{
8
    /** @var string */
9
    private $content;
10
11
    /** @var string */
12
    private $mimeType;
13
14
    public function __construct(string $data, string $mimeType)
15
    {
16
        $this->content = $data;
17
        $this->mimeType = $mimeType;
18
    }
19
20
    public function image(): string
21
    {
22
        return $this->content;
23
    }
24
25
    public function imageBase64(): string
26
    {
27
        return base64_encode($this->content);
28
    }
29
30
    public function mimeType(): string
31
    {
32
        return $this->mimeType;
33
    }
34
35
    public function toHtml(array $attributes = []): string
36
    {
37
        unset($attributes['src']);
38
        $htmlAttributes = [];
39
        foreach ($attributes as $key => $value) {
40
            $htmlAttributes[] = "$key=\"$value\"";
41
        }
42
43
        return
44
            sprintf(
45
                "<img src=\"data:%s;base64,%s\" %s></img>",
46
                $this->mimeType(),
47
                $this->imageBase64(),
48
                implode(' ', $htmlAttributes)
49
            );
50
    }
51
}
52