Skip to content

Commit

Permalink
Refactors lib/private/Collaboration.
Browse files Browse the repository at this point in the history
Mainly using PHP8's constructor property promotion.

Signed-off-by: Faraz Samapoor <[email protected]>
  • Loading branch information
fsamapoor authored and Faraz Samapoor committed Jul 3, 2023
1 parent 5d9d37e commit de37c6b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@

/** @template-implements IEventListener<Event|NodeDeletedEvent|ShareDeletedEvent|ShareCreatedEvent> */
class FileReferenceEventListener implements IEventListener {
private IReferenceManager $manager;

public function __construct(IReferenceManager $manager) {
$this->manager = $manager;
public function __construct(
private IReferenceManager $manager,
) {
}

public static function register(IEventDispatcher $eventDispatcher): void {
Expand Down
20 changes: 6 additions & 14 deletions lib/private/Collaboration/Reference/File/FileReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,18 @@
use OCP\L10N\IFactory;

class FileReferenceProvider extends ADiscoverableReferenceProvider {
private IURLGenerator $urlGenerator;
private IRootFolder $rootFolder;
private ?string $userId;
private IPreview $previewManager;
private IMimeTypeDetector $mimeTypeDetector;
private IL10N $l10n;

public function __construct(
IURLGenerator $urlGenerator,
IRootFolder $rootFolder,
private IURLGenerator $urlGenerator,
private IRootFolder $rootFolder,
IUserSession $userSession,
IMimeTypeDetector $mimeTypeDetector,
IPreview $previewManager,
IFactory $l10n
private IMimeTypeDetector $mimeTypeDetector,
private IPreview $previewManager,
IFactory $l10n,
) {
$this->urlGenerator = $urlGenerator;
$this->rootFolder = $rootFolder;
$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
$this->previewManager = $previewManager;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->userId = $userSession->getUser()?->getUID();
$this->l10n = $l10n->get('files');
}

Expand Down
30 changes: 11 additions & 19 deletions lib/private/Collaboration/Reference/LinkReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,16 @@ class LinkReferenceProvider implements IReferenceProvider {
'image/webp'
];

private IClientService $clientService;
private LoggerInterface $logger;
private SystemConfig $systemConfig;
private IAppDataFactory $appDataFactory;
private IURLGenerator $urlGenerator;
private Limiter $limiter;
private IUserSession $userSession;
private IRequest $request;

public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter, IUserSession $userSession, IRequest $request) {
$this->clientService = $clientService;
$this->logger = $logger;
$this->systemConfig = $systemConfig;
$this->appDataFactory = $appDataFactory;
$this->urlGenerator = $urlGenerator;
$this->limiter = $limiter;
$this->userSession = $userSession;
$this->request = $request;
public function __construct(
private IClientService $clientService,
private LoggerInterface $logger,
private SystemConfig $systemConfig,
private IAppDataFactory $appDataFactory,
private IURLGenerator $urlGenerator,
private Limiter $limiter,
private IUserSession $userSession,
private IRequest $request,
) {
}

public function matchReference(string $referenceText): bool {
Expand Down Expand Up @@ -119,7 +111,7 @@ private function fetchReference(Reference $reference): void {
$linkContentType = $headResponse->getHeader('Content-Type');
$expectedContentType = 'text/html';
$suffixedExpectedContentType = $expectedContentType . ';';
$startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType;
$startsWithSuffixed = str_starts_with($linkContentType, $suffixedExpectedContentType);
// check the header begins with the expected content type
if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) {
$this->logger->debug('Skip resolving links pointing to content type that is not "text/html"');
Expand Down
49 changes: 10 additions & 39 deletions lib/private/Collaboration/Reference/ReferenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,22 @@ class ReferenceManager implements IReferenceManager {
/** @var IReferenceProvider[]|null */
private ?array $providers = null;
private ICache $cache;
private Coordinator $coordinator;
private ContainerInterface $container;
private LinkReferenceProvider $linkReferenceProvider;
private LoggerInterface $logger;
private IConfig $config;
private IUserSession $userSession;

public function __construct(LinkReferenceProvider $linkReferenceProvider,
ICacheFactory $cacheFactory,
Coordinator $coordinator,
ContainerInterface $container,
LoggerInterface $logger,
IConfig $config,
IUserSession $userSession) {
$this->linkReferenceProvider = $linkReferenceProvider;

public function __construct(
private LinkReferenceProvider $linkReferenceProvider,
ICacheFactory $cacheFactory,
private Coordinator $coordinator,
private ContainerInterface $container,
private LoggerInterface $logger,
private IConfig $config,
private IUserSession $userSession,
) {
$this->cache = $cacheFactory->createDistributed('reference');
$this->coordinator = $coordinator;
$this->container = $container;
$this->logger = $logger;
$this->config = $config;
$this->userSession = $userSession;
}

/**
* Extract a list of URLs from a text
*
* @param string $text
* @return string[]
*/
public function extractReferences(string $text): array {
Expand All @@ -85,9 +74,6 @@ public function extractReferences(string $text): array {

/**
* Try to get a cached reference object from a reference string
*
* @param string $referenceId
* @return IReference|null
*/
public function getReferenceFromCache(string $referenceId): ?IReference {
$matchedProvider = $this->getMatchedProvider($referenceId);
Expand All @@ -102,9 +88,6 @@ public function getReferenceFromCache(string $referenceId): ?IReference {

/**
* Try to get a cached reference object from a full cache key
*
* @param string $cacheKey
* @return IReference|null
*/
public function getReferenceByCacheKey(string $cacheKey): ?IReference {
$cached = $this->cache->get($cacheKey);
Expand All @@ -118,9 +101,6 @@ public function getReferenceByCacheKey(string $cacheKey): ?IReference {
/**
* Get a reference object from a reference string with a matching provider
* Use a cached reference if possible
*
* @param string $referenceId
* @return IReference|null
*/
public function resolveReference(string $referenceId): ?IReference {
$matchedProvider = $this->getMatchedProvider($referenceId);
Expand Down Expand Up @@ -148,7 +128,6 @@ public function resolveReference(string $referenceId): ?IReference {
* Try to match a reference string with all the registered providers
* Fallback to the link reference provider (using OpenGraph)
*
* @param string $referenceId
* @return IReferenceProvider|null the first matching provider
*/
private function getMatchedProvider(string $referenceId): ?IReferenceProvider {
Expand All @@ -169,10 +148,6 @@ private function getMatchedProvider(string $referenceId): ?IReferenceProvider {

/**
* Get a hashed full cache key from a key and prefix given by a provider
*
* @param IReferenceProvider $provider
* @param string $referenceId
* @return string
*/
private function getFullCacheKey(IReferenceProvider $provider, string $referenceId): string {
$cacheKey = $provider->getCacheKey($referenceId);
Expand All @@ -183,10 +158,6 @@ private function getFullCacheKey(IReferenceProvider $provider, string $reference

/**
* Remove a specific cache entry from its key+prefix
*
* @param string $cachePrefix
* @param string|null $cacheKey
* @return void
*/
public function invalidateCache(string $cachePrefix, ?string $cacheKey = null): void {
if ($cacheKey === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@

/** @template-implements IEventListener<Event|RenderReferenceEvent> */
class RenderReferenceEventListener implements IEventListener {
private IReferenceManager $manager;
private IInitialStateService $initialStateService;

public function __construct(IReferenceManager $manager, IInitialStateService $initialStateService) {
$this->manager = $manager;
$this->initialStateService = $initialStateService;
public function __construct(
private IReferenceManager $manager,
private IInitialStateService $initialStateService,
) {
}

public static function register(IEventDispatcher $eventDispatcher): void {
Expand Down
47 changes: 8 additions & 39 deletions lib/private/Collaboration/Resources/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,62 +37,35 @@
use OCP\IUser;

class Collection implements ICollection {
/** @var Manager */
protected $manager;

/** @var IDBConnection */
protected $connection;

/** @var int */
protected $id;

/** @var string */
protected $name;

/** @var IUser|null */
protected $userForAccess;

/** @var bool|null */
protected $access;

/** @var IResource[] */
protected $resources;
protected array $resources = [];

public function __construct(
IManager $manager,
IDBConnection $connection,
int $id,
string $name,
?IUser $userForAccess = null,
?bool $access = null
/** @var Manager $manager */
protected IManager $manager,
protected IDBConnection $connection,
protected int $id,
protected string $name,
protected ?IUser $userForAccess = null,
protected ?bool $access = null
) {
$this->manager = $manager;
$this->connection = $connection;
$this->id = $id;
$this->name = $name;
$this->userForAccess = $userForAccess;
$this->access = $access;
$this->resources = [];
}

/**
* @return int
* @since 16.0.0
*/
public function getId(): int {
return $this->id;
}

/**
* @return string
* @since 16.0.0
*/
public function getName(): string {
return $this->name;
}

/**
* @param string $name
* @since 16.0.0
*/
public function setName(string $name): void {
Expand Down Expand Up @@ -120,7 +93,6 @@ public function getResources(): array {
/**
* Adds a resource to a collection
*
* @param IResource $resource
* @throws ResourceException when the resource is already part of the collection
* @since 16.0.0
*/
Expand Down Expand Up @@ -153,7 +125,6 @@ public function addResource(IResource $resource): void {
/**
* Removes a resource from a collection
*
* @param IResource $resource
* @since 16.0.0
*/
public function removeResource(IResource $resource): void {
Expand All @@ -178,8 +149,6 @@ public function removeResource(IResource $resource): void {
/**
* Can a user/guest access the collection
*
* @param IUser|null $user
* @return bool
* @since 16.0.0
*/
public function canAccess(?IUser $user): bool {
Expand Down
Loading

0 comments on commit de37c6b

Please sign in to comment.