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
154 changes: 126 additions & 28 deletions administrator/components/com_media/src/Event/FetchMediaFileEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,155 @@

\defined('_JEXEC') or die;

use Joomla\CMS\Event\AbstractEvent;
use BadMethodCallException;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Event\AbstractImmutableEvent;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;

/**
* Event object for fetch media file.
*
* @since __DEPLOY_VERSION__
*/
class FetchMediaFileEvent extends AbstractEvent
final class FetchMediaFileEvent extends AbstractImmutableEvent
{
/**
* @var \stdClass
* @since __DEPLOY_VERSION__
*/
private $file;

/**
* Constructor.
*
* @param string $name The event name.
* @param \stdClass $file The file.
* @param string $name The event name.
* @param array $arguments The event arguments.
*
* @throws BadMethodCallException
*
* @since __DEPLOY_VERSION__
*/
public function __construct($name, \stdClass $file)
public function __construct($name, array $arguments = array())
{
parent::__construct($name, []);
parent::__construct($name, $arguments);

$this->file = $file;
// Check for required arguments
if (!\array_key_exists('file', $arguments) || !is_object($arguments['file']))
{
throw new BadMethodCallException("Argument 'file' of event $name is not of the expected type");
}
}

/**
* Returns the event file.
* Validate $value to have all attributes with a valid type
*
* @return \stdClass
* Validation based on \Joomla\Component\Media\Administrator\Adapter\AdapterInterface::getFile()
*
* @since __DEPLOY_VERSION__
*/
public function getFile(): \stdClass
{
return $this->file;
}

/**
* Sets the event file.
* Properties validated:
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
* - extension: The file extension
* - size: The size of the file
* - create_date: The date created
* - modified_date: The date modified
* - mime_type: The mime type
* - width: The width, when available
* - height: The height, when available
*
* @param \stdClass $file The file.
* Generation based on \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter::getPathInformation()
*
* @since __DEPLOY_VERSION__
* Properties generated:
* - created_date_formatted: DATE_FORMAT_LC5 formatted string based on create_date
* - modified_date_formatted: DATE_FORMAT_LC5 formatted string based on modified_date
*
* @param \stdClass $value The value to set
*
* @return \stdClass
*
* @since __DEPLOY_VERSION__
*
* @throws BadMethodCallException
*/
public function setFile(\stdClass $file): void
protected function setFile(\stdClass $value): \stdClass
{
$this->file = $file;
// Make immutable object
$value = clone $value;

// Only "dir" or "file" is allowed
if (!isset($value->type) || ($value->type !== 'dir' && $value->type !== 'file'))
{
throw new BadMethodCallException("Property 'type' of argument 'file' of event {$this->name} has a wrong value. Valid: 'dir' or 'file'");
}

// Non empty string
if (empty($value->name) || !is_string($value->name))
{
throw new BadMethodCallException("Property 'name' of argument 'file' of event {$this->name} has a wrong value. Valid: non empty string");
}

// Non empty string
if (empty($value->path) || !is_string($value->path))
{
throw new BadMethodCallException("Property 'path' of argument 'file' of event {$this->name} has a wrong value. Valid: non empty string");
}

// A string
if (!isset($value->extension) || !is_string($value->extension))
{
throw new BadMethodCallException("Property 'extension' of argument 'file' of event {$this->name} has a wrong value. Valid: string");
}

// An empty string or an integer
if (!isset($value->size) ||
(!is_integer($value->size) && !is_string($value->size)) ||
(is_string($value->size) && $value->size !== '')
)
{
throw new BadMethodCallException("Property 'size' of argument 'file' of event {$this->name} has a wrong value. Valid: empty string or integer");
}

// A string
if (!isset($value->mime_type) || !is_string($value->mime_type))
{
throw new BadMethodCallException("Property 'mime_type' of argument 'file' of event {$this->name} has a wrong value. Valid: string");
}

// An integer
if (!isset($value->width) || !is_integer($value->width))
{
throw new BadMethodCallException("Property 'width' of argument 'file' of event {$this->name} has a wrong value. Valid: integer");
}

// An integer
if (!isset($value->height) || !is_integer($value->height))
{
throw new BadMethodCallException("Property 'height' of argument 'file' of event {$this->name} has a wrong value. Valid: integer");
}

// A ISO 8601 date string
if (empty($value->create_date)) {
throw new BadMethodCallException("Property 'create_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string");
}

// Validate date format
$date = Date::createFromFormat(\DATE_ISO8601, $value->create_date);
if (!$date) {
throw new BadMethodCallException("Property 'create_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string");
}

// Create formated string based on \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter::getPathInformation()
$value->create_date_formatted = HTMLHelper::_('date', $date, Text::_('DATE_FORMAT_LC5'));

// A ISO 8601 date string
if (empty($value->modified_date)) {
throw new BadMethodCallException("Property 'modified_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string");
}

// Validate date format
$date = Date::createFromFormat(\DATE_ISO8601, $value->modified_date);
if (!$date) {
throw new BadMethodCallException("Property 'modified_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string");
}

// Create formated string based on \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter::getPathInformation()
$value->modified_date_formatted = HTMLHelper::_('date', $date, Text::_('DATE_FORMAT_LC5'));

return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,94 @@

\defined('_JEXEC') or die;

use Joomla\CMS\Event\AbstractEvent;
use BadMethodCallException;
use Joomla\CMS\Event\AbstractImmutableEvent;

/**
* Event object to set an url.
*
* @since __DEPLOY_VERSION__
*/
class FetchMediaFileUrlEvent extends AbstractEvent
final class FetchMediaFileUrlEvent extends AbstractImmutableEvent
{
/**
* @var string
* @since __DEPLOY_VERSION__
*/
private $url;

/**
* Constructor.
*
* @param string $name The event name.
* @param string $url The url.
* @param string $name The event name.
* @param array $arguments The event arguments.
*
* @throws BadMethodCallException
*
* @since __DEPLOY_VERSION__
*/
public function __construct($name, string $url)
public function __construct($name, array $arguments = array())
{
parent::__construct($name, []);
// Check for required arguments
if (!\array_key_exists('adapter', $arguments) || !is_string($arguments['adapter']))
{
throw new BadMethodCallException("Argument 'adapter' of event $name is not of the expected type");
}

$this->arguments[$arguments['adapter']] = $arguments['adapter'];
unset($arguments['adapter']);

$this->url = $url;
// Check for required arguments
if (!\array_key_exists('path', $arguments) || !is_string($arguments['path']))
{
throw new BadMethodCallException("Argument 'path' of event $name is not of the expected type");
}

$this->arguments[$arguments['path']] = $arguments['path'];
unset($arguments['path']);

// Check for required arguments
if (!\array_key_exists('url', $arguments) || !is_string($arguments['url']))
{
throw new BadMethodCallException("Argument 'url' of event $name is not of the expected type");
}

parent::__construct($name, $arguments);
}

/**
* Returns the event url.
* Validate $value to be a string
*
* @return stdClass
* @param string $value The value to set
*
* @since __DEPLOY_VERSION__
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function getUrl(): string
protected function setUrl(string $value): string
{
return $this->url;
return $value;
}

/**
* Sets the event url.
* Forbid setting $path
*
* @param stdClass
* @param string $value The value to set
*
* @since __DEPLOY_VERSION__
* @since __DEPLOY_VERSION__
*
* @throws BadMethodCallException
*/
protected function setPath(string $value): string
{
throw new BadMethodCallException('Cannot set the argument "path" of the immutable event ' . $this->name . '.');
}

/**
* Forbid setting $path
*
* @param string $value The value to set
*
* @since __DEPLOY_VERSION__
*
* @throws BadMethodCallException
*/
public function setUrl(string $url): void
protected function setAdapter(string $value): string
{
$this->url = $url;
throw new BadMethodCallException('Cannot set the argument "adapter" of the immutable event ' . $this->name . '.');
}
}
Loading