Skip to content

Commit

Permalink
style(php): run composer cs:fix
Browse files Browse the repository at this point in the history
Signed-off-by: Baptiste Fotia <[email protected]>
  • Loading branch information
zak39 committed Dec 20, 2023
1 parent 393c337 commit f70a3d6
Show file tree
Hide file tree
Showing 26 changed files with 507 additions and 555 deletions.
488 changes: 243 additions & 245 deletions lib/Controller/FileCSVController.php

Large diffs are not rendered by default.

33 changes: 15 additions & 18 deletions lib/Exceptions/Notifications/AbstractNotificationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

namespace OCA\Workspace\Exceptions\Notifications;

abstract class AbstractNotificationException extends \Exception
{

public function __construct(
private string $title,
string $message,
/**
* @var integer from OCP\AppFramework\Http
*/
int $code
)
{
parent::__construct($message, $code);
}
abstract class AbstractNotificationException extends \Exception {

public function __construct(
private string $title,
string $message,
/**
* @var integer from OCP\AppFramework\Http
*/
int $code
) {
parent::__construct($message, $code);
}

public function getTitle(): string
{
return $this->title;
}
public function getTitle(): string {
return $this->title;
}
}
10 changes: 4 additions & 6 deletions lib/Exceptions/Notifications/BadMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use OCP\AppFramework\Http;

class BadMimeType extends AbstractNotificationException
{
public function __construct(private string $title, string $message, int $code = Http::STATUS_BAD_REQUEST)
{
parent::__construct($title, $message, $code);
}
class BadMimeType extends AbstractNotificationException {
public function __construct(private string $title, string $message, int $code = Http::STATUS_BAD_REQUEST) {
parent::__construct($title, $message, $code);
}
}
10 changes: 4 additions & 6 deletions lib/Exceptions/Notifications/InvalidCsvFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use OCP\AppFramework\Http;

class InvalidCsvFormatException extends AbstractNotificationException
{
public function __construct(private string $title, string $message, int $code = Http::STATUS_BAD_REQUEST)
{
parent::__construct($title, $message, $code);
}
class InvalidCsvFormatException extends AbstractNotificationException {
public function __construct(private string $title, string $message, int $code = Http::STATUS_BAD_REQUEST) {
parent::__construct($title, $message, $code);
}
}
10 changes: 4 additions & 6 deletions lib/Exceptions/Notifications/InvalidSeparatorCsvException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use OCP\AppFramework\Http;

class InvalidSeparatorCsvException extends AbstractNotificationException
{
public function __construct(private string $title, string $message, int $code = Http::STATUS_BAD_REQUEST)
{
parent::__construct($title, $message, $code);
}
class InvalidSeparatorCsvException extends AbstractNotificationException {
public function __construct(private string $title, string $message, int $code = Http::STATUS_BAD_REQUEST) {
parent::__construct($title, $message, $code);
}
}
18 changes: 8 additions & 10 deletions lib/Exceptions/Notifications/UserDoesntExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use OCP\AppFramework\Http;

class UserDoesntExistException extends AbstractNotificationException
{
public function __construct(
private string $title,
string $message,
int $code = Http::STATUS_BAD_REQUEST
)
{
parent::__construct($title, $message, $code);
}
class UserDoesntExistException extends AbstractNotificationException {
public function __construct(
private string $title,
string $message,
int $code = Http::STATUS_BAD_REQUEST
) {
parent::__construct($title, $message, $code);
}
}
2 changes: 1 addition & 1 deletion lib/Files/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use Exception;

/**
* @deprecated
* @deprecated
* @uses OCA\Workspace\Files\Csv\ImportUsers\{Header,HeaderExtractor,HeaderValidator,Parser}
* @uses OCA\Workspace\Files\Csv\{CheckMimeType,SeparatorDetector}
*/
Expand Down
22 changes: 10 additions & 12 deletions lib/Files/Csv/CheckMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

use OCP\Files\Node;

class CheckMimeType
{
private const MIME_TYPE = 'text/csv';
class CheckMimeType {
private const MIME_TYPE = 'text/csv';

public function checkOnNode(Node $file): bool {
return $file->getMimetype() !== self::MIME_TYPE;
public function checkOnNode(Node $file): bool {
return $file->getMimetype() !== self::MIME_TYPE;
}

/**
* @param array $file as $_FILE
*/
public function checkOnArray(array $file): bool
{
return $file['type'] !== self::MIME_TYPE;
}
/**
* @param array $file as $_FILE
*/
public function checkOnArray(array $file): bool {
return $file['type'] !== self::MIME_TYPE;
}
}
7 changes: 3 additions & 4 deletions lib/Files/Csv/CsvHeaderExtractorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace OCA\Workspace\Files\Csv;

interface CsvHeaderExtractorInterface
{
public static function getIndex(array $haystack, array $needles): int|bool;
public static function getHeaderName(array $needles, array $haystack): string;
interface CsvHeaderExtractorInterface {
public static function getIndex(array $haystack, array $needles): int|bool;
public static function getHeaderName(array $needles, array $haystack): string;
}
5 changes: 2 additions & 3 deletions lib/Files/Csv/CsvParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use OCA\Workspace\Files\ManagerConnectionFileInterface;

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

namespace OCA\Workspace\Files\Csv;

use OCA\Workspace\Files\Csv\Separator;
use OCA\Workspace\Files\ManagerConnectionFileInterface;

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

public function __construct(private ManagerConnectionFileInterface $file)
{
$handle = $file->open();
$this->headers = fgetcsv($handle, 1000, Separator::COMMA);
$file->close();
}
public function __construct(private ManagerConnectionFileInterface $file) {
$handle = $file->open();
$this->headers = fgetcsv($handle, 1000, Separator::COMMA);
$file->close();
}

public function read(): \Generator {
$handle = $this->file->open();
fgetcsv($handle, 1000, Separator::COMMA);
public function read(): \Generator {
$handle = $this->file->open();
fgetcsv($handle, 1000, Separator::COMMA);

try {
while(($data = fgetcsv($handle, 1000, Separator::COMMA)) !== false) {
yield array_combine($this->headers, $data);
}
} catch (\Exception $reason){
throw new \Exception($reason->getMessage());
} finally {
$this->file->close();
}
}
try {
while(($data = fgetcsv($handle, 1000, Separator::COMMA)) !== false) {
yield array_combine($this->headers, $data);
}
} catch (\Exception $reason) {
throw new \Exception($reason->getMessage());
} finally {
$this->file->close();
}
}
}
5 changes: 2 additions & 3 deletions lib/Files/Csv/CsvValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use OCA\Workspace\Files\ManagerConnectionFileInterface;

interface CsvValidatorInterface
{
public function validate(ManagerConnectionFileInterface $file): bool;
interface CsvValidatorInterface {
public function validate(ManagerConnectionFileInterface $file): bool;
}
5 changes: 2 additions & 3 deletions lib/Files/Csv/ImportUsers/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace OCA\Workspace\Files\Csv\ImportUsers;

class Header
{
public const DISPLAY_NAME = ["username", "displayname", "name"];
class Header {
public const DISPLAY_NAME = ["username", "displayname", "name"];
public const ROLE = ["role", "status", "userrole"];
}
34 changes: 16 additions & 18 deletions lib/Files/Csv/ImportUsers/HeaderExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use OCA\Workspace\Files\Csv\CsvHeaderExtractorInterface;

class HeaderExtractor implements CsvHeaderExtractorInterface
{
public static function getIndex(array $haystack, array $needles): int|bool {
class HeaderExtractor implements CsvHeaderExtractorInterface {
public static function getIndex(array $haystack, array $needles): int|bool {
$index = null;
foreach($haystack as $key => $value) {
$index = array_search($value, $needles);
Expand All @@ -17,22 +16,21 @@ public static function getIndex(array $haystack, array $needles): int|bool {
return false;
}

/**
* @throws \Exception when nothing header is found.
*/
public static function getHeaderName(array $needles, array $haystacks): string
{
$found = array_filter(
$needles,
fn($needle) => in_array($needle, $haystacks)
);
/**
* @throws \Exception when nothing header is found.
*/
public static function getHeaderName(array $needles, array $haystacks): string {
$found = array_filter(
$needles,
fn ($needle) => in_array($needle, $haystacks)
);

if (empty($found)) {
throw new \Exception("The $needles needles is not present in $haystacks haystacks");
}
if (empty($found)) {
throw new \Exception("The $needles needles is not present in $haystacks haystacks");
}

$key = (string)array_values($found)[0];
$key = (string)array_values($found)[0];

return $key;
}
return $key;
}
}
3 changes: 1 addition & 2 deletions lib/Files/Csv/ImportUsers/HeaderValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use OCA\Workspace\Files\Csv\CsvValidatorInterface;
use OCA\Workspace\Files\ManagerConnectionFileInterface;

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

namespace OCA\Workspace\Files\Csv\ImportUsers;

use OCA\Workspace\Files\Csv\CsvReader;
use OCA\Workspace\Files\Csv\CsvParserInterface;
use OCA\Workspace\Files\Csv\ImportUsers\Header;
use OCA\Workspace\Files\Csv\CsvReader;
use OCA\Workspace\Files\ManagerConnectionFileInterface;
use OCA\Workspace\Files\Csv\ImportUsers\HeaderExtractor;
use OCA\Workspace\Users\Formatter\UserImportedFormatter;

class Parser implements CsvParserInterface
{
class Parser implements CsvParserInterface {

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

$csvReader = new CsvReader($file);
$csvReader = new CsvReader($file);

$uid = HeaderExtractor::getHeaderName($csvReader->headers, Header::DISPLAY_NAME);
$role = HeaderExtractor::getHeaderName($csvReader->headers, Header::ROLE);
$uid = HeaderExtractor::getHeaderName($csvReader->headers, Header::DISPLAY_NAME);
$role = HeaderExtractor::getHeaderName($csvReader->headers, Header::ROLE);

foreach ($csvReader->read() as $data ) {
$users[] = new UserImportedFormatter($data[$uid], $data[$role]);
}
foreach ($csvReader->read() as $data) {
$users[] = new UserImportedFormatter($data[$uid], $data[$role]);
}

return $users;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Files/Csv/Separator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace OCA\Workspace\Files\Csv;

class Separator
{
public const COMMA = ',';
class Separator {
public const COMMA = ',';
}
23 changes: 10 additions & 13 deletions lib/Files/Csv/SeparatorDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

namespace OCA\Workspace\Files\Csv;

use OCA\Workspace\Files\Csv\Separator;
use OCA\Workspace\Files\ManagerConnectionFileInterface;

class SeparatorDetector
{
private const SIZE = 1000;

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

$firstLine = fread($handle, self::SIZE);
class SeparatorDetector {
private const SIZE = 1000;

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

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

$file->close();
$file->close();

return strpos($firstLine, Separator::COMMA) !== false;
}
return strpos($firstLine, Separator::COMMA) !== false;
}
}
Loading

0 comments on commit f70a3d6

Please sign in to comment.