Passed
Push — refactor_render ( 1be99d...7e011e )
by Mattia
09:18
created

Rendering::skin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Image;
6
7
use Minepic\Helpers\Storage\Files\SkinsStorage;
8
use Minepic\Image\Components\Side;
9
use Minepic\Image\Sections\Avatar;
10
use Minepic\Image\Sections\SkinBack;
11
use Minepic\Image\Sections\SkinFront;
12
use Minepic\Minecraft\MinecraftDefaults;
13
14
class Rendering
15
{
16
    /**
17
     * @param null|string $uuid
18
     * @param int         $size
19
     * @param string      $type
20
     *
21
     * @throws Exceptions\ImageCreateFromPngFailedException
22
     * @throws Exceptions\ImageTrueColorCreationFailedException
23
     *
24
     * @return Avatar
25
     */
26
    public function avatar(?string $uuid, int $size, $type = Side::FRONT): Avatar
27
    {
28
        $avatar = new Avatar(
29
            $this->imagePath($uuid)
30
        );
31
        $avatar->render($size, $type);
32
33
        return $avatar;
34
    }
35
36
    /**
37
     * @param null|string $uuid
38
     * @param int         $size
39
     * @param null|int    $lastUpdateTimestamp
40
     *
41
     * @throws Exceptions\SkinNotFountException
42
     * @throws \Throwable
43
     *
44
     * @return IsometricAvatar
45
     */
46
    public function isometricAvatar(?string $uuid, int $size, int $lastUpdateTimestamp = null): IsometricAvatar
47
    {
48
        if ($lastUpdateTimestamp === null) {
49
            $lastUpdateTimestamp = \time();
50
        }
51
52
        $isometricAvatar = new IsometricAvatar(
53
            $uuid ?? MinecraftDefaults::STEVE_DEFAULT_SKIN_NAME,
54
            $lastUpdateTimestamp
55
        );
56
        $isometricAvatar->render($size);
57
58
        return $isometricAvatar;
59
    }
60
61
    /**
62
     * @param null|string $uuid
63
     * @param int $size
64
     * @throws Exceptions\ImageCreateFromPngFailedException
65
     * @throws Exceptions\ImageResourceCreationFailedException
66
     * @return SkinFront
67
     */
68
    public function skinFront(?string $uuid, int $size): SkinFront
69
    {
70
        $skin = new SkinFront(
71
            $this->imagePath($uuid)
72
        );
73
        $skin->render($size);
74
75
        return $skin;
76
    }
77
78
    /**
79
     * @param null|string $uuid
80
     * @param int $size
81
     * @throws Exceptions\ImageCreateFromPngFailedException
82
     * @throws Exceptions\ImageResourceCreationFailedException
83
     * @return SkinBack
84
     */
85
    public function skinBack(?string $uuid, int $size): SkinBack
86
    {
87
        $skin = new SkinBack(
88
            $this->imagePath($uuid)
89
        );
90
        $skin->render($size);
91
92
        return $skin;
93
    }
94
95
    /**
96
     * @param null|string $uuid
97
     *
98
     * @throws \Exception
99
     *
100
     * @return string
101
     */
102
    private function imagePath(?string $uuid): string
103
    {
104
        return $uuid !== null ? SkinsStorage::getPath($uuid) : SkinsStorage::getPath(MinecraftDefaults::STEVE_DEFAULT_SKIN_NAME);
105
    }
106
}
107