Passed
Push — master ( 9aa048...898b5e )
by Mattia
04:05
created

Storage::getPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Helpers\Storage;
6
7
use Minepic\Minecraft\MinecraftDefaults;
8
9
class Storage
10
{
11
    /**
12
     * Storage folder type.
13
     *
14
     * @var string
15
     */
16
    protected static string $folder;
17
18
    /**
19
     * Skin Path.
20
     *
21
     * @param string $uuid
22
     *
23
     * @throws \Exception
24
     *
25
     * @return string
26
     */
27
    public static function getPath(string $uuid): string
28
    {
29
        if ($uuid === '') {
30
            $uuid = MinecraftDefaults::getRandomDefaultSkin();
31
        }
32
33
        return \sprintf(storage_path(static::$folder.'/%s.png'), $uuid);
34
    }
35
36
    /**
37
     * Checks if file exists.
38
     *
39
     * @param string $uuid
40
     *
41
     * @return bool
42
     */
43
    public static function exists(string $uuid): bool
44
    {
45
        return \file_exists(static::getPath($uuid));
46
    }
47
48
    /**
49
     * Save the skin to file.
50
     *
51
     * @param string $uuid
52
     * @param mixed  $rawData
53
     *
54
     * @return bool
55
     */
56
    public static function save(string $uuid, $rawData): bool
57
    {
58
        $fp = \fopen(static::getPath($uuid), 'wb');
59
        if ($fp) {
0 ignored issues
show
introduced by
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
60
            \fwrite($fp, $rawData);
61
            \fclose($fp);
62
63
            return true;
64
        }
65
66
        return false;
67
    }
68
69
    /**
70
     * Use Steve file for given uuid.
71
     *
72
     * @param mixed
73
     */
74
    public static function copyAsSteve(string $string): bool
75
    {
76
        if ($string !== '') {
77
            return \copy(
78
                static::getPath(MinecraftDefaults::getRandomDefaultSkin()),
79
                static::getPath($string)
80
            );
81
        }
82
83
        return false;
84
    }
85
}
86