• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-library / 5472784411

pending completion
5472784411

push

github

webeweb
Update CHANGELOG

49034 of 49178 relevant lines covered (99.71%)

48.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/src/symfony/Service/UploadedFileService.php
1
<?php
2

3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2022 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Library\Symfony\Service;
13

14
use SplFileInfo;
15
use Throwable;
16
use WBW\Library\Traits\Strings\StringDirectoryTrait;
17
use WBW\Library\Types\Helper\StringHelper;
18

19
/**
20
 * Uploaded file service.
21
 *
22
 * @author webeweb <https://github.com/webeweb>
23
 * @package WBW\Library\Symfony\Service
24
 */
25
class UploadedFileService implements UploadedFileServiceInterface {
26

27
    use StringDirectoryTrait {
28
        setDirectory as protected;
29
    }
30

31
    /**
32
     * Service name.
33
     *
34
     * @var string
35
     */
36
    const SERVICE_NAME = "wbw.core.service.uploaded_file";
37

38
    /**
39
     * Upload directory.
40
     *
41
     * @var string
42
     */
43
    const UPLOAD_DIRECTORY = "/uploads";
44

45
    /**
46
     * Constructor.
47
     *
48
     * @param string $directory The directory.
49
     */
50
    public function __construct(string $directory) {
51
        $this->setDirectory($directory);
42✔
52
    }
12✔
53

54
    /**
55
     * Determine if a filename exists.
56
     *
57
     * @param string $filename The filename.
58
     * @return bool Returns true in case of success, false otherwise.
59
     */
60
    public function exists(string $filename): bool {
61
        return file_exists($this->path($filename));
14✔
62
    }
63

64
    /**
65
     * Create a directory.
66
     *
67
     * @param string $directory The directory.
68
     * @return bool Returns true in case of success, false otherwise.
69
     */
70
    protected function mkdir(string $directory): bool {
71

72
        if (true === file_exists($directory)) {
7✔
73
            return true;
×
74
        }
75

76
        return mkdir($directory, 0755, true);
7✔
77
    }
78

79
    /**
80
     * Path.
81
     *
82
     * @param string $filename The filename.
83
     * @return string Returns the path.
84
     */
85
    public function path(string $filename): string {
86
        return implode(DIRECTORY_SEPARATOR, [
21✔
87
            $this->getDirectory(),
21✔
88
            $filename,
21✔
89
        ]);
15✔
90
    }
91

92
    /**
93
     * Save.
94
     *
95
     * @param SplFileInfo $uploadedFile The uploaded file.
96
     * @param string $subdirectory The subdirectory.
97
     * @param string|null $filename The filename.
98
     * @return string|null Returns the uploaded file path.
99
     */
100
    public function save(SplFileInfo $uploadedFile, string $subdirectory, string $filename = null): ?string {
101

102
        // Directory
103
        $dir = implode("", [
7✔
104
            $this->getDirectory(),
7✔
105
            self::UPLOAD_DIRECTORY,
7✔
106
            $subdirectory,
7✔
107
        ]);
5✔
108

109
        // Destination
110
        $dst = implode(DIRECTORY_SEPARATOR, [
7✔
111
            $dir,
7✔
112
            null !== $filename ? $filename : $uploadedFile->getFilename(),
7✔
113
        ]);
5✔
114

115
        if (false === $this->mkdir($dir) || false === rename($uploadedFile->getPathname(), $dst)) {
7✔
116
            return null;
×
117
        }
118

119
        return str_replace($this->getDirectory(), "", $dst);
7✔
120
    }
121

122
    /**
123
     * Unique id.
124
     *
125
     * @return string Returns the unique id.
126
     * @throws Throwable Throws an exception if an error occurs.
127
     */
128
    public function uniqid(): string {
129

130
        $random  = random_bytes(32);
7✔
131
        $bin2hex = bin2hex($random);
7✔
132

133
        return StringHelper::format($bin2hex, "________-____-____-____-____________");
7✔
134
    }
135

136
    /**
137
     * Unlink.
138
     *
139
     * @param string $filename The filename.
140
     * @return bool|null Returns true in case of success, false otherwise.
141
     */
142
    public function unlink(string $filename): ?bool {
143

144
        if (false === $this->exists($filename)) {
7✔
145
            return null;
7✔
146
        }
147

148
        return unlink($this->path($filename));
7✔
149
    }
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc