diff --git a/boot.php b/boot.php index 53fd082..0e29a60 100644 --- a/boot.php +++ b/boot.php @@ -2,7 +2,11 @@ use FriendsOfRedaxo\addon\MediapoolExif\MediapoolExif; -rex_fragment::addDirectory(realpath(__DIR__)); +$dir = realpath(__DIR__); +if ($dir !== false) { + rex_fragment::addDirectory($dir); +} + $class = MediapoolExif::class; rex_extension::register('MEDIA_ADDED', [$class, 'processUploadedMedia'], rex_extension::LATE); diff --git a/install.php b/install.php index 4f2cabf..bcedf2a 100644 --- a/install.php +++ b/install.php @@ -5,12 +5,12 @@ ->ensureColumn(new rex_sql_column('exif', 'longtext', true)) ->ensure(); -rex_metainfo_add_field('Beschreibung', 'med_description', '1', '', '2', '', '', '', ''); -rex_metainfo_add_field('Schlagworte', 'med_keywords', '2', '', '1', '', '', '', ''); -rex_metainfo_add_field('Copyright', 'med_copyright', '3', '', '1', '', '', '', ''); -rex_metainfo_add_field('Autor', 'med_author', '4', '', '1', '', '', '', ''); -rex_metainfo_add_field('Ausrichtung', 'med_orientation', '5', '', '1', '', '', '', ''); -rex_metainfo_add_field('Erstellungsdatum', 'med_createdate', '6', '', '1', '', '', '', ''); -rex_metainfo_add_field('GPS Breitengrad', 'med_gps_lat', '7', '', '1', '', '', '', ''); -rex_metainfo_add_field('GPS Längengrad', 'med_gps_long', '8', '', '1', '', '', '', ''); +rex_metainfo_add_field('Beschreibung', 'med_description', 1, '', 2, '', '', '', ''); +rex_metainfo_add_field('Schlagworte', 'med_keywords', 2, '', 1, '', '', '', ''); +rex_metainfo_add_field('Copyright', 'med_copyright', 3, '', 1, '', '', '', ''); +rex_metainfo_add_field('Autor', 'med_author', 4, '', 1, '', '', '', ''); +rex_metainfo_add_field('Ausrichtung', 'med_orientation', 5, '', 1, '', '', '', ''); +rex_metainfo_add_field('Erstellungsdatum', 'med_createdate', 6, '', 1, '', '', '', ''); +rex_metainfo_add_field('GPS Breitengrad', 'med_gps_lat', 7, '', 1, '', '', '', ''); +rex_metainfo_add_field('GPS Längengrad', 'med_gps_long', 8, '', 1, '', '', '', ''); diff --git a/lib/Cli/Read.php b/lib/Cli/Read.php index 4a420fc..619faef 100644 --- a/lib/Cli/Read.php +++ b/lib/Cli/Read.php @@ -20,6 +20,7 @@ * Description of Renew * * @author akrys + * @codeCoverageIgnore */ class Read extends rex_console_command { @@ -42,33 +43,38 @@ protected function configure(): void * @param InputInterface $input * @param OutputInterface $output */ - protected function execute(InputInterface $input, OutputInterface $output): void + protected function execute(InputInterface $input, OutputInterface $output): int { - $io = $this->getStyle($input, $output); - $io->title('Read EXIF data'); + $command = $this->getStyle($input, $output); + $command->title('Read EXIF data'); $updateAll = $input->getOption('all'); $updateSilent = $input->getOption('silent'); $mode = MediaFetchMode::NULL_ONLY; if ($updateAll) { - if ($updateSilent || $io->confirm('You are going to update all files. Are you sure?', false)) { + if ($updateSilent || $command->confirm('You are going to update all files. Are you sure?', false)) { $mode = MediaFetchMode::ALL; } } $files = Exif::getMediaToRead($mode); $numEntries = count($files); - $io->writeln($numEntries.' entries to read'); + $command->writeln($numEntries.' entries to read'); $counter = 0; foreach ($files as $file) { + if (!isset($file['filename'])) { + continue; + } + $counter++; - $io->writeln('Process file '.$counter.' of '.$numEntries.': '.$file['filename']); + $command->writeln('Process file '.$counter.' of '.$numEntries.': '.$file['filename']); - MediapoolExif::readExifFromFile($file['filename']); + MediapoolExif::readExifFromFile((string) $file['filename']); } - $io->success('done'); + $command->success('done'); + return self::SUCCESS; } } diff --git a/lib/Enum/Format.php b/lib/Enum/Format.php index eb087fe..6e5a767 100644 --- a/lib/Enum/Format.php +++ b/lib/Enum/Format.php @@ -18,8 +18,9 @@ * * @author akrys */ -enum Format :string +enum Format: string { case RAW = 'numeric'; case READABLE = 'readable'; + case UNDEFINED = ''; } diff --git a/lib/Enum/MediaFetchMode.php b/lib/Enum/MediaFetchMode.php index 9594cfe..1e0eddb 100644 --- a/lib/Enum/MediaFetchMode.php +++ b/lib/Enum/MediaFetchMode.php @@ -4,7 +4,6 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template */ - namespace FriendsOfRedaxo\addon\MediapoolExif\Enum; /** @@ -19,7 +18,7 @@ * * @author akrys */ -enum MediaFetchMode :int +enum MediaFetchMode: int { case NULL_ONLY = 1000; case ALL = 1001; diff --git a/lib/Exception/InvalidClassException.php b/lib/Exception/InvalidClassException.php new file mode 100644 index 0000000..25e4ce0 --- /dev/null +++ b/lib/Exception/InvalidClassException.php @@ -0,0 +1,54 @@ +class; + } +} diff --git a/lib/Exception/InvalidFormatExcption.php b/lib/Exception/InvalidFormatExcption.php index ae57192..6d71343 100644 --- a/lib/Exception/InvalidFormatExcption.php +++ b/lib/Exception/InvalidFormatExcption.php @@ -19,32 +19,34 @@ */ class InvalidFormatExcption extends Exception { - /** - * @var Format - */ - public Format $format; /** * Konstruktor - * @param string $format + * @param ?Format $format * @param string $message * @param int $code * @param Throwable $previous */ - public function __construct(Format $format, string $message = "", int $code = 0, Throwable $previous = NULL) - { - $this->format = $format; + public function __construct( + private ?Format $format, + string $message = "", + int $code = 0, + Throwable $previous = null + ) { + if ($this->format === null) { + $this->format = Format::UNDEFINED; + } if ($message === '') { - $message = 'Invalid Format: '.$format->value; + $message = 'Invalid Format: '.$this->format->value; } parent::__construct($message, $code, $previous); } /** * Formatname - * @return Format + * @return Format|null */ - public function getFormat(): Format + public function getFormat(): ?Format { return $this->format; } diff --git a/lib/Exception/NotFoundException.php b/lib/Exception/NotFoundException.php index 583642f..08ca6da 100644 --- a/lib/Exception/NotFoundException.php +++ b/lib/Exception/NotFoundException.php @@ -18,10 +18,6 @@ */ class NotFoundException extends Exception { - /** - * @var string - */ - public string $index; /** * Konstruktor @@ -30,9 +26,12 @@ class NotFoundException extends Exception * @param int $code * @param Throwable $previous */ - public function __construct(string $index, string $message = "", int $code = 0, Throwable $previous = NULL) - { - $this->index = $index; + public function __construct( + private string $index, + string $message = "", + int $code = 0, + Throwable $previous = null + ) { parent::__construct($message, $code, $previous); } diff --git a/lib/Exif.php b/lib/Exif.php index e6fc44f..1d5310f 100644 --- a/lib/Exif.php +++ b/lib/Exif.php @@ -21,10 +21,11 @@ */ class Exif { + /** * Alternative Datenerhebung, analog zu rex_media::get() * - * @param \FriendsOfRedaxo\addon\MediapoolExif\rex_media $media + * @param rex_media $media * @return ExifData */ public static function get(rex_media $media, ReturnMode $mode = ReturnMode::THROW_EXCEPTION): ExifData @@ -34,8 +35,8 @@ public static function get(rex_media $media, ReturnMode $mode = ReturnMode::THRO /** * - * @param type $mode - * @return array + * @param MediaFetchMode $mode + * @return list>|array */ public static function getMediaToRead(MediaFetchMode $mode = MediaFetchMode::NULL_ONLY): array { diff --git a/lib/ExifData.php b/lib/ExifData.php index 009ade8..a73d236 100644 --- a/lib/ExifData.php +++ b/lib/ExifData.php @@ -22,26 +22,13 @@ */ class ExifData { - /** - * Media-Objekt - * - * @var rex_media - */ - private rex_media $media; - /** * Exif-Daten-Array * - * @var array + * @var array */ private array $exif; - /** - * Modus - * @var ReturnMode - */ - private ReturnMode $mode; - /** * Konstruktor * @@ -63,26 +50,26 @@ class ExifData *
  • [] (MODE_RETURN_EMPTY_ARRAY)
  • * * - * @param \FriendsOfRedaxo\addon\MediapoolExif\rex_media $media - * @param int $mode + * @param rex_media $media + * @param ReturnMode $mode */ - public function __construct(rex_media $media, ReturnMode $mode = null) - { - $this->media = $media; + public function __construct( + private rex_media $media, + private ?ReturnMode $mode = null + ) { $this->exif = []; $exifRaw = $this->media->getValue('exif'); if ($exifRaw !== null) { - $this->exif = json_decode($exifRaw, true); + $this->exif = json_decode((string) $exifRaw, true); if (!$this->exif) { $this->exif = []; } } - if ($mode === null) { - $mode = ReturnMode::THROW_EXCEPTION; + if ($this->mode === null) { + $this->mode = ReturnMode::THROW_EXCEPTION; } - $this->mode = $mode; } /** @@ -108,9 +95,8 @@ public function get(string $index = null): mixed /** * Formatierungsalgorithmus anstoßen - * @param string $type - * @param Format $format * @param string $className + * @param Format $format * @return mixed */ public function format(string $className, Format $format = null): mixed @@ -132,7 +118,7 @@ public function format(string $className, Format $format = null): mixed * * Welche Rückgabe hätten's gern? * - * @param string $exception + * @param Exception $exception * @return mixed * @throws NotFoundException */ diff --git a/lib/Format/Camera.php b/lib/Format/Camera.php index cc69b39..f799055 100644 --- a/lib/Format/Camera.php +++ b/lib/Format/Camera.php @@ -27,7 +27,7 @@ class Camera extends FormatInterface /** * Daten formatieren - * @return array + * @return array * @throws Exception * @throws InvalidFormatExcption */ @@ -37,22 +37,26 @@ public function format(): array throw new Exception('No camera data found'); } - $format = $this->format; - if ($format === null) { - $format = Format::READABLE; + if ($this->format === null) { + $this->format = Format::READABLE; } + $formatValue = $this->format->value; - $formatValue = $format->value; - - if (is_callable([$this, $formatValue])) { - return $this->$formatValue(); + /** @phpstan-ignore-next-line */ + if (!is_callable([$this, $formatValue])) { + // @codeCoverageIgnoreStart + throw new InvalidFormatExcption($this->format); + // @codeCoverageIgnoreEnd } - throw new InvalidFormatExcption($format); + + return $this->$formatValue(); } /** * Daten lesbar anzeigen - * @return array + * @return array + * @SuppressWarnings(PHPMD.UnusedPrivateMethod) + * -> zugriff via $this->$formatValue */ private function readable(): array { @@ -68,7 +72,9 @@ private function readable(): array /** * Daten nummerisch anzeigen - * @return array + * @return array + * @SuppressWarnings(PHPMD.UnusedPrivateMethod) + * -> zugriff via $this->$formatValue */ private function numeric(): array { diff --git a/lib/Format/Camera/Aperture.php b/lib/Format/Camera/Aperture.php index 1102077..73da205 100644 --- a/lib/Format/Camera/Aperture.php +++ b/lib/Format/Camera/Aperture.php @@ -10,6 +10,7 @@ use Exception; use FriendsOfRedaxo\addon\MediapoolExif\Enum\Format; +use FriendsOfRedaxo\addon\MediapoolExif\Exception\InvalidFormatExcption; use FriendsOfRedaxo\addon\MediapoolExif\Format\FormatInterface; /** @@ -35,10 +36,9 @@ public function format(): string switch ($this->format) { case Format::READABLE: return 'f/'.number_format((float) $data[0] / (float) $data[1], 1); - break; case Format::RAW: + default: return number_format((float) $data[0] / (float) $data[1], 1); - break; } } } diff --git a/lib/Format/Camera/Exposure.php b/lib/Format/Camera/Exposure.php index a8ae308..41f7c8c 100644 --- a/lib/Format/Camera/Exposure.php +++ b/lib/Format/Camera/Exposure.php @@ -28,7 +28,7 @@ class Exposure extends FormatInterface public function format(): string { if (!isset($this->data['ExposureTime'])) { - throw new Exception('No aperture found'); + throw new Exception('No exposure time found'); } $return = ''; diff --git a/lib/Format/Camera/Iso.php b/lib/Format/Camera/Iso.php index bcd3222..5835520 100644 --- a/lib/Format/Camera/Iso.php +++ b/lib/Format/Camera/Iso.php @@ -27,7 +27,7 @@ class Iso extends FormatInterface public function format(): string { if (!isset($this->data['ISOSpeedRatings'])) { - throw new Exception('No aperture found'); + throw new Exception('No iso setting found'); } return $this->data['ISOSpeedRatings']; diff --git a/lib/Format/Camera/Length.php b/lib/Format/Camera/Length.php index cd8c6e2..8a94e42 100644 --- a/lib/Format/Camera/Length.php +++ b/lib/Format/Camera/Length.php @@ -9,6 +9,7 @@ use Exception; use FriendsOfRedaxo\addon\MediapoolExif\Enum\Format; +use FriendsOfRedaxo\addon\MediapoolExif\Exception\InvalidFormatExcption; use FriendsOfRedaxo\addon\MediapoolExif\Format\FormatInterface; /** @@ -34,7 +35,7 @@ class Length extends FormatInterface public function format(): string { if (!isset($this->data['FocalLength'])) { - throw new Exception('No aperture found'); + throw new Exception('No focial length found'); } $data = explode('/', $this->data['FocalLength']); @@ -42,11 +43,9 @@ public function format(): string switch ($this->format) { case Format::READABLE: return $value.' mm'; - break; case Format::RAW: default: - return $value; - break; + return (string) $value; } } } diff --git a/lib/Format/FormatInterface.php b/lib/Format/FormatInterface.php index 03752b2..cf25a11 100644 --- a/lib/Format/FormatInterface.php +++ b/lib/Format/FormatInterface.php @@ -9,8 +9,7 @@ namespace FriendsOfRedaxo\addon\MediapoolExif\Format; use FriendsOfRedaxo\addon\MediapoolExif\Enum\Format; -use FriendsOfRedaxo\addon\MediapoolExif\Exception\InvalidFormatExcption; -use FriendsOfRedaxo\addon\MediapoolExif\Format\FormatInterface; +use FriendsOfRedaxo\addon\MediapoolExif\Exception\InvalidClassException; /** * Description of FormatBase @@ -21,7 +20,7 @@ abstract class FormatInterface { /** * Exif-Daten - * @var array + * @var array */ protected array $data; @@ -33,8 +32,8 @@ abstract class FormatInterface /** * Konstruktor - * @param array $data - * @param Format|null $format + * @param array $data + * @param ?Format $format */ public function __construct(array $data, ?Format $format = null) { @@ -52,9 +51,9 @@ public function __construct(array $data, ?Format $format = null) * Es kann aber auch sein, dass sie direkt aus der Datei stammen. z.B. für die automatische Koordinaten-Errechnung. * Im Prinzip spielt es aber keine Rolle, wo die Daten genau her kommen. Wichtig ist nur, dass sie hier ankommen. * - * $type: - * Bei Type geht es um die Formatter-Klasse, die verwendet werden soll. Wichtig ist, dass siek das FormatInterface - * implementiert. Damit kann die Funktion format immer aufgerufen werden. + * $className: + * Bei ClassName geht es um die Formatter-Klasse, die verwendet werden soll. Wichtig ist, dass sie das + * FormatInterface implementiert. Damit kann die Funktion format immer aufgerufen werden. * * $format: * Beim Format-Parameter kann man einen Hinweis liefern, wie die Ausgabe zu formatieren ist. @@ -66,21 +65,24 @@ public function __construct(array $data, ?Format $format = null) *
  • degree: 51° 06' 39.32" N / 8° 40' 55.656 E
  • *
      * - * @param string $data exif-Daten-Array - * @param string $className Formatter Namespace + * @param array $data exif-Daten-Array + * @param ?string $className Formatter Namespace * @param Format|null $format Format-Parameter - * @return \FriendsOfRedaxo\addon\MediapoolExif\Format\className - * @throws InvalidFormatExcption + * @return FormatInterface + * @throws InvalidClassException */ public static function get($data, ?string $className = null, ?Format $format = null): FormatInterface { + if ($className === null) { + $className = ''; + } if (class_exists($className)) { $object = new $className($data, $format); if (is_a($object, FormatInterface::class)) { return $object; } } - throw new InvalidFormatExcption($className); + throw new InvalidClassException($className); } /** @@ -90,7 +92,7 @@ public static function get($data, ?string $className = null, ?Format $format = n * * Die Rückgabe kann bei einzelwerten ein string, sont ein array sein. * - * @return string|array + * @return string|array */ abstract public function format(): string|array; } diff --git a/lib/Format/Geo.php b/lib/Format/Geo.php index 6005e2e..58722e4 100644 --- a/lib/Format/Geo.php +++ b/lib/Format/Geo.php @@ -20,13 +20,12 @@ class Geo extends FormatInterface /** * Formatierung der Daten - * @return array + * @return array * @throws Exception */ public function format(): array { - if ( - !isset($this->data['GPSLatitude']) || + if (!isset($this->data['GPSLatitude']) || !isset($this->data['GPSLatitudeRef']) || !isset($this->data['GPSLongitude']) || !isset($this->data['GPSLongitudeRef'])) { @@ -52,18 +51,18 @@ public function format(): array $GPSLatitude_m = explode("/", $GPSLatitude[1]); $GPSLatitude_s = explode("/", $GPSLatitude[2]); - $GPSLat_h = $GPSLatitude_h[0] / $GPSLatitude_h[1]; - $GPSLat_m = $GPSLatitude_m[0] / $GPSLatitude_m[1]; - $GPSLat_s = $GPSLatitude_s[0] / $GPSLatitude_s[1]; + $GPSLat_h = (float) $GPSLatitude_h[0] / (float) $GPSLatitude_h[1]; + $GPSLat_m = (float) $GPSLatitude_m[0] / (float) $GPSLatitude_m[1]; + $GPSLat_s = (float) $GPSLatitude_s[0] / (float) $GPSLatitude_s[1]; $GPSLatGrad = $GPSLatfaktor * ($GPSLat_h + ($GPSLat_m + ($GPSLat_s / 60)) / 60); $GPSLongitude_h = explode("/", $GPSLongitude[0]); $GPSLongitude_m = explode("/", $GPSLongitude[1]); $GPSLongitude_s = explode("/", $GPSLongitude[2]); - $GPSLong_h = $GPSLongitude_h[0] / $GPSLongitude_h[1]; - $GPSLong_m = $GPSLongitude_m[0] / $GPSLongitude_m[1]; - $GPSLong_s = $GPSLongitude_s[0] / $GPSLongitude_s[1]; + $GPSLong_h = (float) $GPSLongitude_h[0] / (float) $GPSLongitude_h[1]; + $GPSLong_m = (float) $GPSLongitude_m[0] / (float) $GPSLongitude_m[1]; + $GPSLong_s = (float) $GPSLongitude_s[0] / (float) $GPSLongitude_s[1]; $GPSLongGrad = $GPSLongfaktor * ($GPSLong_h + ($GPSLong_m + ($GPSLong_s / 60)) / 60); return [ diff --git a/lib/MediapoolExif.php b/lib/MediapoolExif.php index e38c01d..48ebd43 100644 --- a/lib/MediapoolExif.php +++ b/lib/MediapoolExif.php @@ -18,11 +18,18 @@ use rex_sql; use function mb_convert_encoding; -class MediapoolExif +/** + * @codeCoverageIgnore + * -> Nur Redaxo=Plattform-Integration + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * -> man könnte vllt mal was auslagern + */ +final class MediapoolExif { /** * Field mapping - * @var array + * @var array */ protected static array $fields = [ 'author' => ['Artist', 'AuthorByLine', 'CaptionWriter'], @@ -39,13 +46,19 @@ class MediapoolExif /** * Upload processing - * @param rex_extension_point $ep + * @param rex_extension_point $exp + * @SuppressWarnings(PHPMD.ElseExpression) + * -> zu tief verschachtelt.... vllt. Funktionsauslagerung? + * @SuppressWarnings(PHPMD.IfStatementAssignment) + * -> bei Datenbankabfragen kaum anders zu machen + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) */ - public static function processUploadedMedia(rex_extension_point $ep): void + public static function processUploadedMedia(rex_extension_point $exp): void { - $oldMedia = rex_media::get($ep->getParam('filename')); - if ($data = static::getDataByFilename($ep->getParam('filename'))) { - $qry = "SELECT * FROM `".rex::getTablePrefix()."media` WHERE `filename` = '".$ep->getParam('filename')."'"; + $oldMedia = rex_media::get($exp->getParam('filename')); + if ($data = static::getDataByFilename($exp->getParam('filename'))) { + $qry = "SELECT * FROM `".rex::getTablePrefix()."media` WHERE `filename` = '".$exp->getParam('filename')."'"; $sql = rex_sql::factory(); $sql->setQuery($qry); if ($result = $sql->getArray()) { @@ -55,9 +68,9 @@ public static function processUploadedMedia(rex_extension_point $ep): void // check for category?! if (isset($data['categories'])) { - $qry = "SELECT `id` FROM `".rex::getTablePrefix()."media_category` WHERE `name` IN ('".join( - "', '", $data['categories'] - )."') ORDER BY FIELD (`name`, '".join("', '", $data['categories'])."') LIMIT 1"; + $qry = "SELECT `id` FROM `".rex::getTablePrefix()."media_category` WHERE ". + "`name` IN ('".join("', '", $data['categories'])."') ". + "ORDER BY FIELD (`name`, '".join("', '", $data['categories'])."') LIMIT 1"; $sql->setQuery($qry); if ($tmp_result = $sql->getArray()) { $data['category_id'] = $tmp_result[0]['id']; @@ -76,7 +89,7 @@ public static function processUploadedMedia(rex_extension_point $ep): void if (preg_match('/date$/', $key)) { $result[$key] = null; $value = date('Y-m-d H:i:s', $value); - } else if (is_array($value)) { + } elseif (is_array($value)) { $value = join(', ', $value); } @@ -87,22 +100,26 @@ public static function processUploadedMedia(rex_extension_point $ep): void } unset($field, $value, $key); - if (self::exifHasChanged($data['exif'], $oldMedia->getValue('exif'))) { + if (self::exifHasChanged($data['exif'], $oldMedia?->getValue('exif'))) { $update['exif'] = '`exif`='.$sql->escape($data['exif']); } if (!empty($update)) { - $qry = "UPDATE `".rex::getTablePrefix()."media` SET ".join(", ", array_values($update))." WHERE `filename` = '".$ep->getParam('filename')."'"; + $qry = "UPDATE `".rex::getTablePrefix()."media` ". + "SET ".join(", ", array_values($update))." ". + "WHERE `filename` = '".$exp->getParam('filename')."'"; + /** @phpstan-ignore-next-line */ if ($sql->setQuery($qry)) { $names = ''.join(', ', array_keys($update)).''; $names = preg_replace_callback('/\>[a-z]/', function ($match) { return strtoupper($match[0]); }, $names); - $ep->setParam('msg', $ep->getParam('msg').'
      '.rex_i18n::msg('exif_data_updated').' '.$names); + $msg = $exp->getParam('msg').'
      '.rex_i18n::msg('exif_data_updated').' '.$names; + $exp->setParam('msg', $msg); - rex_media_cache::delete($ep->getParam('filename')); + rex_media_cache::delete($exp->getParam('filename')); } else { rex_logger::factory()->alert('SQL-Error ['.$sql->getErrno().'] '.$sql->getError()); } @@ -119,24 +136,22 @@ public static function processUploadedMedia(rex_extension_point $ep): void * Check ob die EXIF-Daten sich geändert haben. * * @param string $new - * @param string|null $old + * @param string|int|null $old * @return bool */ - private static function exifHasChanged(string $new, ?string $old): bool + private static function exifHasChanged(string $new, string|int|null $old): bool { $newArray = json_decode($new, true); $oldArray = []; if ($old !== null) { - $oldArray = json_decode($old, true); + $oldArray = json_decode((string) $old, true); } /** * FileDateTime ändert sich immer. * Hieße: wir ändern das exif-Feld, obwohl sich nichts relevantes geändert hat. */ - unset( - $newArray['FileDateTime'], $oldArray['FileDateTime'] - ); + unset($newArray['FileDateTime'], $oldArray['FileDateTime']); $newString = json_encode($newArray); $oldString = json_encode($oldArray); @@ -147,11 +162,12 @@ private static function exifHasChanged(string $new, ?string $old): bool /** * Daten au der Datei holen * @param string $filename - * @return array + * @return array */ public static function getDataByFilename(string $filename): array { - if ($media = rex_media::get($filename)) { + $media = rex_media::get($filename); + if ($media) { if ($media->fileExists()) { return static::getData($media); } @@ -164,7 +180,11 @@ public static function getDataByFilename(string $filename): array * Daten aus der Datei verarbeiten * @param rex_media $media * @param string $key - * @return array + * @return array + * @SuppressWarnings(PHPMD.ElseExpression) + * -> zu tief verschachtelt.... vllt. Funktionsauslagerung? + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) */ public static function getData(rex_media $media, string $key = null): array { @@ -205,14 +225,14 @@ public static function getData(rex_media $media, string $key = null): array $return['title'] = preg_replace('/^(.*)\.[\w]{1,}$/', "$1", $return['title']); // Unterstriche zu Leerzeichen umwandeln - $return['title'] = preg_replace('/_/', " ", $return['title']); + $return['title'] = preg_replace('/_/', " ", (string) $return['title']); // Doppelte Leerzeichen entfernen - $return['title'] = trim(preg_replace('/ {2,}/', ' ', $return['title'])); + $return['title'] = trim((string) preg_replace('/ {2,}/', ' ', (string) $return['title'])); // Ersten Buchstaben gross schreiben - $return['title'] = ucfirst($return['title']); + $return['title'] = ucfirst((string) $return['title']); - $return['title'] = trim($return['title']); + $return['title'] = trim((string) $return['title']); if (empty($return['title'])) { unset($return['title']); @@ -240,7 +260,7 @@ protected static function isExifFile(rex_media $media): bool /** * EXIF-Daten aus dem rex_media-Objekt holen. * @param rex_media $media - * @return array + * @return array */ protected static function getExifData(rex_media $media): array { @@ -259,8 +279,8 @@ protected static function getExifData(rex_media $media): array try { $coordinates = FormatInterface::get($exif, Geo::class)->format(); - $exif['GPSCoordinatesLat'] = $coordinates['lat']; - $exif['GPSCoordinatesLong'] = $coordinates['long']; + $exif['GPSCoordinatesLat'] = $coordinates['lat'] ?? 0; + $exif['GPSCoordinatesLong'] = $coordinates['long'] ?? 0; } catch (Exception $e) { //no GPS Data, nothing to to } @@ -274,7 +294,7 @@ protected static function getExifData(rex_media $media): array /** * IPTC-Daten holen * @param rex_media $media - * @return array + * @return array */ protected static function getIptcData(rex_media $media): array { @@ -288,7 +308,8 @@ protected static function getIptcData(rex_media $media): array $iptc = static::parseIptc($media); foreach (IptcDefinitions::cases() as $case) { if (!empty($iptc[$case->getCode()])) { - $return[$case->getLabel()] = count($iptc[$case->getCode()]) == 1 ? $iptc[$case->getCode()][0] : $iptc[$case->getCode()]; + $count = count($iptc[$case->getCode()]) == 1 ? $iptc[$case->getCode()][0] : $iptc[$case->getCode()]; + $return[$case->getLabel()] = $count; } } } catch (IptcException $e) { @@ -301,11 +322,12 @@ protected static function getIptcData(rex_media $media): array * IPTC-Daten parsen. * * @param rex_media $media - * @return array + * @return array * @throws IptcException */ private static function parseIptc(rex_media $media): array { + $iptc = []; $path = rex_path::media($media->getFileName()); $size = getimagesize($path, $info); if (!$size) { @@ -324,19 +346,19 @@ private static function parseIptc(rex_media $media): array /** * Seitenleiste für die Medienpool-Detailseite generieren - * @param rex_extension_point $ep + * @param rex_extension_point $exp * @return string */ - public static function mediapoolDetailOutput(rex_extension_point $ep): string + public static function mediapoolDetailOutput(rex_extension_point $exp): string { - $subject = $ep->getSubject(); + $subject = $exp->getSubject(); - $exifRaw = $ep->getParam('media')->getValue('exif'); + $exifRaw = $exp->getParam('media')->getValue('exif'); if ($exifRaw === null) { return $subject; } - $exif = json_decode($exifRaw, 1); + $exif = json_decode($exifRaw, true); if ($exif) { $lines = ''; //rekursiver Aufruf einer anonymen Funktion @@ -354,24 +376,24 @@ public static function mediapoolDetailOutput(rex_extension_point $ep): string /** * Einzelzeile der Medienpool-Detail-Ausgabe verarbeiten. - * @param array $exif + * @param array $exif * @return string */ protected static function mediapoolDetailOutputLine(array $exif): string { $lines = []; foreach ($exif as $key => $value) { + $line = [ + 'key' => $key, + 'value' => $value, + ]; if (is_array($value)) { - $lines[] = [ + $line = [ 'key' => $key, 'value' => self::mediapoolDetailOutputLine($value), ]; - } else { - $lines[] = [ - 'key' => $key, - 'value' => $value, - ]; } + $lines[] = $line; } $fragment = new rex_fragment([ @@ -390,13 +412,17 @@ protected static function mediapoolDetailOutputLine(array $exif): string * @param string $filename * @return void */ - public function readExifFromFile(string $filename): void + public static function readExifFromFile(string $filename): void { $subject = null; $params = [ 'filename' => $filename, ]; - $ep = new rex_extension_point('dummy', $subject, $params, false); - MediapoolExif::processUploadedMedia($ep); + + /** + * @var rex_extension_point $exp + */ + $exp = new rex_extension_point('dummy', $subject, $params, false); + MediapoolExif::processUploadedMedia($exp); } } diff --git a/package.yml b/package.yml index 36ac736..8807f75 100644 --- a/package.yml +++ b/package.yml @@ -1,5 +1,5 @@ package: mediapool_exif -version: '3.0.1' +version: '3.0.2' author: 'FriendsOfREDAXO' supportpage: https://github.com/FriendsOfREDAXO/mediapool_exif