Skip to content

Commit

Permalink
refactor(Files): Rename an interface and create a new
Browse files Browse the repository at this point in the history
Rename "ManagerConnectionFileInterface" to "BasicStreamInterface" and
create the FileInterface interface which extends from
"BasicStreamInterface" and implement the "getPath" method.
  • Loading branch information
zak39 committed Jul 16, 2024
1 parent 0b93ebc commit 33f6c98
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OCA\Workspace\Files;

interface ManagerConnectionFileInterface {
interface BasicStreamInterface {
public function open(?string $path = null);
public function close();
}
4 changes: 2 additions & 2 deletions lib/Files/Connection/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace OCA\Workspace\Files\Connection;

use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\BasicStreamInterface;

class Terminal implements ManagerConnectionFileInterface {
class Terminal implements BasicStreamInterface {
private $stream;

public function __construct() {
Expand Down
4 changes: 2 additions & 2 deletions lib/Files/Csv/CsvParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace OCA\Workspace\Files\Csv;

use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\BasicStreamInterface;

interface CsvParserInterface {
public function parser(ManagerConnectionFileInterface $file): array;
public function parser(BasicStreamInterface $file): array;
}
4 changes: 2 additions & 2 deletions lib/Files/Csv/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace OCA\Workspace\Files\Csv;

use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\BasicStreamInterface;

/**
* Use CscReader to read without consuming too much memory.
*/
class CsvReader {
public readonly array $headers;

public function __construct(private ManagerConnectionFileInterface $file) {
public function __construct(private BasicStreamInterface $file) {
$handle = $file->open();
$this->headers = fgetcsv($handle, 1000, Separator::COMMA);
$file->close();
Expand Down
4 changes: 2 additions & 2 deletions lib/Files/Csv/CsvValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace OCA\Workspace\Files\Csv;

use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\BasicStreamInterface;

interface CsvValidatorInterface {
public function validate(ManagerConnectionFileInterface $file): bool;
public function validate(BasicStreamInterface $file): bool;
}
4 changes: 2 additions & 2 deletions lib/Files/Csv/ImportUsers/HeaderValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace OCA\Workspace\Files\Csv\ImportUsers;

use OCA\Workspace\Files\Csv\CsvValidatorInterface;
use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\BasicStreamInterface;

class HeaderValidator implements CsvValidatorInterface {
public function validate(ManagerConnectionFileInterface $file): bool {
public function validate(BasicStreamInterface $file): bool {
$res = false;
if (($handle = $file->open()) !== false) {
$tableHeader = fgetcsv($handle, 1000, ",");
Expand Down
4 changes: 2 additions & 2 deletions lib/Files/Csv/ImportUsers/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use OCA\Workspace\Files\Csv\CsvParserInterface;
use OCA\Workspace\Files\Csv\CsvReader;
use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\BasicStreamInterface;
use OCA\Workspace\Users\Formatter\UserImportedFormatter;

class Parser implements CsvParserInterface {

/**
* @return UserImportedFormatter[]
*/
public function parser(ManagerConnectionFileInterface $file): array {
public function parser(BasicStreamInterface $file): array {
$users = [];

$csvReader = new CsvReader($file);
Expand Down
4 changes: 2 additions & 2 deletions lib/Files/Csv/ImportUsers/ValuesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace OCA\Workspace\Files\Csv\ImportUsers;

use OCA\Workspace\Files\Csv\CsvReader;
use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\BasicStreamInterface;

class ValuesValidator {
public function validateRoles(ManagerConnectionFileInterface $file): bool {
public function validateRoles(BasicStreamInterface $file): bool {
$res = true;

$csvReader = new CsvReader($file);
Expand Down
21 changes: 18 additions & 3 deletions lib/Files/Csv/SeparatorDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@

namespace OCA\Workspace\Files\Csv;

use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\FileInterface;

class SeparatorDetector {
private const SIZE = 1000;

public static function isComma(ManagerConnectionFileInterface $file): bool {
public static function isComma(FileInterface $file): bool {
$handle = $file->open();

$firstLine = fread($handle, self::SIZE);

$lines = file($file->getPath(), FILE_SKIP_EMPTY_LINES);
$totalCount = count($lines);

$separatorsAsComma = array_filter(
$lines,
fn ($line) => str_contains($line, Separator::COMMA)
);

$commasCount = count($separatorsAsComma);

$file->close();

return strpos($firstLine, Separator::COMMA) !== false;
$isComma =
(strpos($firstLine, Separator::COMMA) !== false)
&& ($totalCount === $commasCount)
;

return $isComma;
}
}
9 changes: 9 additions & 0 deletions lib/Files/FileInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace OCA\Workspace\Files;

use OCA\Workspace\Files\BasicStreamInterface;

interface FileInterface extends BasicStreamInterface {
public function getPath(): string;
}
2 changes: 1 addition & 1 deletion lib/Files/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OCA\Workspace\Files;

class FileUploader implements ManagerConnectionFileInterface {
class FileUploader implements FileInterface {
private $resource;

public function __construct(private string $path) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Files/NextcloudFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use OCP\Files\Storage\IStorage;

class NextcloudFile implements ManagerConnectionFileInterface {
class NextcloudFile implements FileInterface {
private $resource;

public function __construct(private string $path, private IStorage $store) {
Expand Down

0 comments on commit 33f6c98

Please sign in to comment.