1
0
Files
breadcrumb-the-shire/core/Service/Tenant/TenantAvatarService.php
fs 3f0db68b27 refactor: fix all 105 PHPStan 2 strict type findings across core and modules
Resolve all non-false-positive PHPStan 2 findings, reducing the baseline
from 486 to 382 entries (only unused-public false positives remain).

Categories fixed:
- Remove 39 redundant type checks (is_string, is_array, is_object, method_exists)
- Remove 12 no-op array_values() on already-sequential lists
- Simplify 13 null-coalesce on guaranteed offsets
- Remove 8 always-true instanceof checks
- Replace 12 redundant test assertions (assertTrue(true) → addToAssertionCount)
- Simplify 6 unnecessary nullsafe operators (?-> → ->)
- Fix 6 always-true !== '' comparisons
- Fix remaining: unset.offset, return.unusedType, staticMethod narrowing

53 files changed across core/, modules/, pages/, and tests/.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-14 12:54:20 +02:00

177 lines
5.2 KiB
PHP

<?php
namespace MintyPHP\Service\Tenant;
use MintyPHP\Service\Image\ImageUploadTrait;
class TenantAvatarService
{
use ImageUploadTrait;
private const MAX_SIZE = 5242880; // 5 MB
private const SIZES = [64, 128, 256];
private const DEFAULT_SIZE = 128;
public function isValidUuid(string $uuid): bool
{
return self::imageIsValidUuid($uuid);
}
public function storageBase(): string
{
return self::imageStorageBase();
}
public function tenantDir(string $uuid): string
{
return $this->storageBase() . '/tenants/' . $uuid . '/avatar';
}
public function findAvatarPath(string $uuid, ?int $size = null): ?string
{
if (!$this->isValidUuid($uuid)) {
return null;
}
$dirs = $this->avatarDirs($uuid);
foreach ($dirs as $dir) {
if (!is_dir($dir)) {
continue;
}
if ($size) {
$size = $this->normalizeSize($size);
$variant = $this->findVariantPath($dir, $size);
if ($variant) {
return $variant;
}
}
$defaultVariant = $this->findVariantPath($dir, self::DEFAULT_SIZE);
if ($defaultVariant) {
return $defaultVariant;
}
$original = self::imageFindOriginalPath($dir);
if ($original) {
return $original;
}
}
return null;
}
public function hasAvatar(string $uuid): bool
{
$path = $this->findAvatarPath($uuid);
return $path ? is_file($path) : false;
}
public function delete(string $uuid): bool
{
if (!$this->isValidUuid($uuid)) {
return false;
}
foreach ($this->avatarDirs($uuid) as $dir) {
if (!is_dir($dir)) {
continue;
}
$matches = array_merge(
glob($dir . '/avatar-*.*') ?: [],
glob($dir . '/avatar.*') ?: [],
glob($dir . '/original.*') ?: []
);
foreach ($matches as $file) {
if (is_file($file)) {
@unlink($file);
}
}
}
return true;
}
public function saveUpload(string $uuid, array $file): array
{
if (!$this->isValidUuid($uuid)) {
return ['ok' => false, 'error' => t('Tenant not found')];
}
if (empty($file) || !isset($file['tmp_name'])) {
return ['ok' => false, 'error' => t('No file uploaded')];
}
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
return ['ok' => false, 'error' => t('Upload failed')];
}
if (($file['size'] ?? 0) > self::MAX_SIZE) {
return ['ok' => false, 'error' => t('File is too large')];
}
$tmpPath = $file['tmp_name'];
$mime = $this->detectMime($tmpPath);
$isSvg = self::imageIsSvgUpload($mime, $tmpPath);
$ext = self::imageExtensionForMime($mime, $isSvg);
if (!$ext) {
return ['ok' => false, 'error' => t('Invalid image file')];
}
if ($isSvg && !self::imageIsSafeSvg($tmpPath)) {
return ['ok' => false, 'error' => t('Invalid image file')];
}
$dir = $this->tenantDir($uuid);
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
return ['ok' => false, 'error' => t('Upload failed')];
}
$this->delete($uuid);
$originalPath = $dir . '/original.' . $ext;
if (!move_uploaded_file($tmpPath, $originalPath)) {
return ['ok' => false, 'error' => t('Upload failed')];
}
@chmod($originalPath, 0644);
$variantExt = function_exists('imagewebp') ? 'webp' : 'jpg';
if (!$isSvg && self::imageCanResize()) {
foreach (self::SIZES as $size) {
$target = $dir . '/avatar-' . $size . '.' . $variantExt;
self::imageResizeAndFit($originalPath, $target, $size, $size, $variantExt);
}
}
return ['ok' => true, 'path' => $originalPath, 'mime' => $mime];
}
public function detectMime(string $path): string
{
return self::imageDetectMime($path);
}
private function normalizeSize(int $size): int
{
if (in_array($size, self::SIZES, true)) {
return $size;
}
return self::DEFAULT_SIZE;
}
private function findVariantPath(string $dir, int $size): ?string
{
$matches = glob($dir . '/avatar-' . $size . '.*');
if (!$matches) {
return null;
}
usort($matches, static function ($a, $b) {
return filemtime($b) <=> filemtime($a);
});
return $matches[0];
}
private function avatarDirs(string $uuid): array
{
$dirs = [$this->tenantDir($uuid)];
$legacy = $this->legacyTenantDir($uuid);
if ($legacy !== $dirs[0]) {
$dirs[] = $legacy;
}
return $dirs;
}
private function legacyTenantDir(string $uuid): string
{
return $this->storageBase() . '/tenants/' . $uuid;
}
}