Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"bamarni/composer-bin-plugin": true
},
"platform": {
"php": "8.3"
"php": "8.1.17"
}
},
"autoload": {
Expand All @@ -25,6 +25,7 @@
"psalm:clear": "psalm --clear-cache && psalm --clear-global-cache",
"psalm:fix": "psalm --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
"test:unit": "phpunit -c tests/phpunit.xml --color --fail-on-warning --fail-on-risky",
"rector": "rector && composer cs:fix",
"post-install-cmd": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || composer bin all install --ansi"
],
Expand Down
10 changes: 2 additions & 8 deletions lib/Album/AlbumFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
use OCA\Photos\DB\PhotosFile;

class AlbumFile extends PhotosFile {
private int $added;
private string $owner;

public function __construct(
int $fileId,
string $name,
string $mimeType,
int $size,
int $mtime,
string $etag,
int $added,
string $owner,
private readonly int $added,
private readonly string $owner,
) {
parent::__construct(
$fileId,
Expand All @@ -32,9 +29,6 @@ public function __construct(
$mtime,
$etag
);

$this->added = $added;
$this->owner = $owner;
}

public function getAdded(): int {
Expand Down
29 changes: 7 additions & 22 deletions lib/Album/AlbumInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,15 @@
namespace OCA\Photos\Album;

class AlbumInfo {
private int $id;
private string $userId;
private string $title;
private string $location;
private int $created;
private int $lastAdded;
private ?int $receivedFrom;

public function __construct(
int $id,
string $userId,
string $title,
string $location,
int $created,
int $lastAdded,
?int $receivedFrom = null,
private readonly int $id,
private readonly string $userId,
private readonly string $title,
private readonly string $location,
private readonly int $created,
private readonly int $lastAdded,
private readonly ?int $receivedFrom = null,
) {
$this->id = $id;
$this->userId = $userId;
$this->title = $title;
$this->location = $location;
$this->created = $created;
$this->lastAdded = $lastAdded;
$this->receivedFrom = $receivedFrom;
}

public function getId(): int {
Expand Down
87 changes: 34 additions & 53 deletions lib/Album/AlbumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
use OCP\Security\ISecureRandom;

class AlbumMapper {
private IDBConnection $connection;
private IMimeTypeLoader $mimeTypeLoader;
private ITimeFactory $timeFactory;
private IUserManager $userManager;
private IGroupManager $groupManager;
private readonly IDBConnection $connection;
private readonly IMimeTypeLoader $mimeTypeLoader;
private readonly ITimeFactory $timeFactory;
private readonly IUserManager $userManager;
private readonly IGroupManager $groupManager;
protected IL10N $l;
protected ISecureRandom $random;

Expand Down Expand Up @@ -91,9 +91,7 @@ public function getForUser(string $userId): array {
->from('photos_albums')
->where($query->expr()->eq('user', $query->createNamedParameter($userId)));
$rows = $query->executeQuery()->fetchAll();
return array_map(function (array $row) use ($userId) {
return new AlbumInfo((int)$row['album_id'], $userId, $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']);
}, $rows);
return array_map(fn (array $row): AlbumInfo => new AlbumInfo((int)$row['album_id'], $userId, $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']), $rows);
}

/**
Expand Down Expand Up @@ -126,9 +124,7 @@ public function getForFile(int $fileId): array {
->leftJoin('a', 'photos_albums_files', 'p', $query->expr()->eq('a.album_id', 'p.album_id'))
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
$rows = $query->executeQuery()->fetchAll();
return array_map(function (array $row) {
return new AlbumInfo((int)$row['album_id'], $row['user'], $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']);
}, $rows);
return array_map(fn (array $row): AlbumInfo => new AlbumInfo((int)$row['album_id'], $row['user'], $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']), $rows);
}

/**
Expand All @@ -144,9 +140,7 @@ public function getForUserAndFile(string $userId, int $fileId): array {
->where($query->expr()->eq('user', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
$rows = $query->executeQuery()->fetchAll();
return array_map(function (array $row) {
return new AlbumInfo((int)$row['album_id'], $row['user'], $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']);
}, $rows);
return array_map(fn (array $row): AlbumInfo => new AlbumInfo((int)$row['album_id'], $row['user'], $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']), $rows);
}

public function rename(int $id, string $newName): void {
Expand Down Expand Up @@ -352,19 +346,12 @@ public function getCollaborators(int $albumId): array {
/** @var string|null */
$displayName = null;

switch ($row['collaborator_type']) {
case self::TYPE_USER:
$displayName = $this->userManager->get($row['collaborator_id'])?->getDisplayName();
break;
case self::TYPE_GROUP:
$displayName = $this->groupManager->get($row['collaborator_id'])?->getDisplayName();
break;
case self::TYPE_LINK:
$displayName = $this->l->t('Public link');
break;
default:
throw new \Exception('Invalid collaborator type: ' . $row['collaborator_type']);
}
$displayName = match ($row['collaborator_type']) {
self::TYPE_USER => $this->userManager->get($row['collaborator_id'])?->getDisplayName(),
self::TYPE_GROUP => $this->groupManager->get($row['collaborator_id'])?->getDisplayName(),
self::TYPE_LINK => $this->l->t('Public link'),
default => throw new \Exception('Invalid collaborator type: ' . $row['collaborator_type']),
};

if (is_null($displayName)) {
return null;
Expand All @@ -377,7 +364,7 @@ public function getCollaborators(int $albumId): array {
];
}, $rows);

return array_values(array_filter($collaborators, fn ($c) => $c !== null));
return array_values(array_filter($collaborators, fn ($c): bool => $c !== null));
}


Expand Down Expand Up @@ -422,12 +409,10 @@ public function setCollaborators(int $albumId, array $collaborators): void {
$existingCollaborators = $this->getCollaborators($albumId);

// Different behavior if type is link to prevent creating multiple link.
$computeKey = function ($c) {
return ($c['type'] === AlbumMapper::TYPE_LINK ? '' : $c['id']) . $c['type'];
};
$computeKey = (fn ($c): string => ($c['type'] === AlbumMapper::TYPE_LINK ? '' : $c['id']) . $c['type']);

$collaboratorsToAdd = array_udiff($collaborators, $existingCollaborators, fn ($a, $b) => strcmp($computeKey($a), $computeKey($b)));
$collaboratorsToRemove = array_udiff($existingCollaborators, $collaborators, fn ($a, $b) => strcmp($computeKey($a), $computeKey($b)));
$collaboratorsToAdd = array_udiff($collaborators, $existingCollaborators, fn ($a, $b): int => strcmp($computeKey($a), $computeKey($b)));
$collaboratorsToRemove = array_udiff($existingCollaborators, $collaborators, fn ($a, $b): int => strcmp($computeKey($a), $computeKey($b)));

$this->connection->beginTransaction();

Expand Down Expand Up @@ -492,16 +477,14 @@ public function getSharedAlbumsForCollaborator(string $collaboratorId, int $coll
->executeQuery()
->fetchAll();

return array_map(function (array $row) {
return new AlbumInfo(
(int)$row['album_id'],
$row['user'],
$row['name'] . ' (' . $row['user'] . ')',
$row['location'],
(int)$row['created'],
(int)$row['last_added_photo']
);
}, $rows);
return array_map(fn (array $row): AlbumInfo => new AlbumInfo(
(int)$row['album_id'],
$row['user'],
$row['name'] . ' (' . $row['user'] . ')',
$row['location'],
(int)$row['created'],
(int)$row['last_added_photo']
), $rows);
}

/**
Expand Down Expand Up @@ -604,15 +587,13 @@ public function getAlbumsForCollaboratorIdAndFileId(string $collaboratorId, int
->fetchAll();


return array_map(function (array $row) {
return new AlbumInfo(
(int)$row['album_id'],
$row['user'],
$row['name'] . ' (' . $row['user'] . ')',
$row['location'],
(int)$row['created'],
(int)$row['last_added_photo']
);
}, $rows);
return array_map(fn (array $row): AlbumInfo => new AlbumInfo(
(int)$row['album_id'],
$row['user'],
$row['name'] . ' (' . $row['user'] . ')',
$row['location'],
(int)$row['created'],
(int)$row['last_added_photo']
), $rows);
}
}
21 changes: 6 additions & 15 deletions lib/Album/AlbumWithFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@
namespace OCA\Photos\Album;

class AlbumWithFiles {
private AlbumInfo $info;
private AlbumMapper $albumMapper;

/** @var AlbumFile[] */
private array $files;

public function __construct(
AlbumInfo $info,
AlbumMapper $albumMapper,
array $files = []) {
$this->info = $info;
$this->albumMapper = $albumMapper;
$this->files = $files;
private readonly AlbumInfo $info,
private readonly AlbumMapper $albumMapper,
/** @var AlbumFile[] */
private array $files = [],
) {
}

public function getAlbum(): AlbumInfo {
Expand Down Expand Up @@ -54,9 +47,7 @@ public function addFile(AlbumFile $file): array {
* @return int[]
*/
public function getFileIds(): array {
return array_map(function (AlbumFile $file) {
return $file->getFileId();
}, $this->getFiles());
return array_map(fn (AlbumFile $file): int => $file->getFileId(), $this->getFiles());
}

/**
Expand Down
12 changes: 3 additions & 9 deletions lib/Command/AlbumAddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@
use Symfony\Component\Console\Output\OutputInterface;

class AlbumAddCommand extends Command {
private IRootFolder $rootFolder;
private IUserManager $userManager;
private AlbumMapper $albumMapper;

public function __construct(
AlbumMapper $albumMapper,
IRootFolder $rootFolder,
IUserManager $userManager,
private readonly AlbumMapper $albumMapper,
private readonly IRootFolder $rootFolder,
private readonly IUserManager $userManager,
) {
$this->rootFolder = $rootFolder;
$this->userManager = $userManager;
$this->albumMapper = $albumMapper;
parent::__construct();
}

Expand Down
8 changes: 2 additions & 6 deletions lib/Command/AlbumCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
use Symfony\Component\Console\Output\OutputInterface;

class AlbumCreateCommand extends Command {
private IUserManager $userManager;
private AlbumMapper $albumMapper;

public function __construct(
AlbumMapper $albumMapper,
IUserManager $userManager,
private readonly AlbumMapper $albumMapper,
private readonly IUserManager $userManager,
) {
$this->userManager = $userManager;
$this->albumMapper = $albumMapper;
parent::__construct();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/UpdateReverseGeocodingFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class UpdateReverseGeocodingFilesCommand extends Command {
public function __construct(
private ReverseGeoCoderService $rgcService,
private readonly ReverseGeoCoderService $rgcService,
) {
parent::__construct();
}
Expand Down
13 changes: 5 additions & 8 deletions lib/Controller/AlbumsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@
use OCP\IRequest;

class AlbumsController extends Controller {
private string $userId;
private IRootFolder $rootFolder;
private IPreview $previewManager;
private readonly IRootFolder $rootFolder;
private readonly IPreview $previewManager;

public function __construct(
string $userId,
private readonly string $userId,
IRequest $request,
IRootFolder $rootFolder,
IPreview $previewManager,
) {
parent::__construct(Application::APP_ID, $request);

$this->userId = $userId;
$this->rootFolder = $rootFolder;
$this->previewManager = $previewManager;
}
Expand All @@ -62,7 +59,7 @@ private function generate(string $path, bool $shared): JSONResponse {
if ($path !== '') {
try {
$folder = $userFolder->get($path);
} catch (NotFoundException $e) {
} catch (NotFoundException) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
}
Expand Down Expand Up @@ -171,7 +168,7 @@ private function scanFolder(Folder $folder, int $depth, bool $shared): bool {
}

$nodes = $folder->getDirectoryListing();
} catch (StorageNotAvailableException $e) {
} catch (StorageNotAvailableException) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use OCP\IUserSession;

class ApiController extends Controller {
private IConfig $config;
private IUserSession $userSession;
private readonly IConfig $config;
private readonly IUserSession $userSession;

public function __construct(
IRequest $request,
Expand Down
Loading
Loading