|
11 | 11 |
|
12 | 12 | \defined('_JEXEC') or die; |
13 | 13 |
|
14 | | -use Joomla\CMS\Event\AbstractEvent; |
| 14 | +use BadMethodCallException; |
| 15 | +use Joomla\CMS\Date\Date; |
| 16 | +use Joomla\CMS\Event\AbstractImmutableEvent; |
| 17 | +use Joomla\CMS\HTML\HTMLHelper; |
| 18 | +use Joomla\CMS\Language\Text; |
15 | 19 |
|
16 | 20 | /** |
17 | 21 | * Event object for fetch media file. |
18 | 22 | * |
19 | 23 | * @since __DEPLOY_VERSION__ |
20 | 24 | */ |
21 | | -class FetchMediaFileEvent extends AbstractEvent |
| 25 | +final class FetchMediaFileEvent extends AbstractImmutableEvent |
22 | 26 | { |
23 | | - /** |
24 | | - * @var \stdClass |
25 | | - * @since __DEPLOY_VERSION__ |
26 | | - */ |
27 | | - private $file; |
28 | | - |
29 | 27 | /** |
30 | 28 | * Constructor. |
31 | 29 | * |
32 | | - * @param string $name The event name. |
33 | | - * @param \stdClass $file The file. |
| 30 | + * @param string $name The event name. |
| 31 | + * @param array $arguments The event arguments. |
| 32 | + * |
| 33 | + * @throws BadMethodCallException |
34 | 34 | * |
35 | 35 | * @since __DEPLOY_VERSION__ |
36 | 36 | */ |
37 | | - public function __construct($name, \stdClass $file) |
| 37 | + public function __construct($name, array $arguments = array()) |
38 | 38 | { |
39 | | - parent::__construct($name, []); |
| 39 | + parent::__construct($name, $arguments); |
40 | 40 |
|
41 | | - $this->file = $file; |
| 41 | + // Check for required arguments |
| 42 | + if (!\array_key_exists('file', $arguments) || !is_object($arguments['file'])) |
| 43 | + { |
| 44 | + throw new BadMethodCallException("Argument 'file' of event $name is not of the expected type"); |
| 45 | + } |
42 | 46 | } |
43 | 47 |
|
44 | 48 | /** |
45 | | - * Returns the event file. |
| 49 | + * Validate $value to have all attributes with a valid type |
46 | 50 | * |
47 | | - * @return \stdClass |
| 51 | + * Validation based on \Joomla\Component\Media\Administrator\Adapter\AdapterInterface::getFile() |
48 | 52 | * |
49 | | - * @since __DEPLOY_VERSION__ |
50 | | - */ |
51 | | - public function getFile(): \stdClass |
52 | | - { |
53 | | - return $this->file; |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * Sets the event file. |
| 53 | + * Properties validated: |
| 54 | + * - type: The type can be file or dir |
| 55 | + * - name: The name of the file |
| 56 | + * - path: The relative path to the root |
| 57 | + * - extension: The file extension |
| 58 | + * - size: The size of the file |
| 59 | + * - create_date: The date created |
| 60 | + * - modified_date: The date modified |
| 61 | + * - mime_type: The mime type |
| 62 | + * - width: The width, when available |
| 63 | + * - height: The height, when available |
58 | 64 | * |
59 | | - * @param \stdClass $file The file. |
| 65 | + * Generation based on \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter::getPathInformation() |
60 | 66 | * |
61 | | - * @since __DEPLOY_VERSION__ |
| 67 | + * Properties generated: |
| 68 | + * - created_date_formatted: DATE_FORMAT_LC5 formatted string based on create_date |
| 69 | + * - modified_date_formatted: DATE_FORMAT_LC5 formatted string based on modified_date |
| 70 | + * |
| 71 | + * @param \stdClass $value The value to set |
| 72 | + * |
| 73 | + * @return \stdClass |
| 74 | + * |
| 75 | + * @since __DEPLOY_VERSION__ |
| 76 | + * |
| 77 | + * @throws BadMethodCallException |
62 | 78 | */ |
63 | | - public function setFile(\stdClass $file): void |
| 79 | + protected function setFile(\stdClass $value): \stdClass |
64 | 80 | { |
65 | | - $this->file = $file; |
| 81 | + // Make immutable object |
| 82 | + $value = clone $value; |
| 83 | + |
| 84 | + // Only "dir" or "file" is allowed |
| 85 | + if (!isset($value->type) || ($value->type !== 'dir' && $value->type !== 'file')) |
| 86 | + { |
| 87 | + throw new BadMethodCallException("Property 'type' of argument 'file' of event {$this->name} has a wrong value. Valid: 'dir' or 'file'"); |
| 88 | + } |
| 89 | + |
| 90 | + // Non empty string |
| 91 | + if (empty($value->name) || !is_string($value->name)) |
| 92 | + { |
| 93 | + throw new BadMethodCallException("Property 'name' of argument 'file' of event {$this->name} has a wrong value. Valid: non empty string"); |
| 94 | + } |
| 95 | + |
| 96 | + // Non empty string |
| 97 | + if (empty($value->path) || !is_string($value->path)) |
| 98 | + { |
| 99 | + throw new BadMethodCallException("Property 'path' of argument 'file' of event {$this->name} has a wrong value. Valid: non empty string"); |
| 100 | + } |
| 101 | + |
| 102 | + // A string |
| 103 | + if (!isset($value->extension) || !is_string($value->extension)) |
| 104 | + { |
| 105 | + throw new BadMethodCallException("Property 'extension' of argument 'file' of event {$this->name} has a wrong value. Valid: string"); |
| 106 | + } |
| 107 | + |
| 108 | + // An empty string or an integer |
| 109 | + if (!isset($value->size) || |
| 110 | + (!is_integer($value->size) && !is_string($value->size)) || |
| 111 | + (is_string($value->size) && $value->size !== '') |
| 112 | + ) |
| 113 | + { |
| 114 | + throw new BadMethodCallException("Property 'size' of argument 'file' of event {$this->name} has a wrong value. Valid: empty string or integer"); |
| 115 | + } |
| 116 | + |
| 117 | + // A string |
| 118 | + if (!isset($value->mime_type) || !is_string($value->mime_type)) |
| 119 | + { |
| 120 | + throw new BadMethodCallException("Property 'mime_type' of argument 'file' of event {$this->name} has a wrong value. Valid: string"); |
| 121 | + } |
| 122 | + |
| 123 | + // An integer |
| 124 | + if (!isset($value->width) || !is_integer($value->width)) |
| 125 | + { |
| 126 | + throw new BadMethodCallException("Property 'width' of argument 'file' of event {$this->name} has a wrong value. Valid: integer"); |
| 127 | + } |
| 128 | + |
| 129 | + // An integer |
| 130 | + if (!isset($value->height) || !is_integer($value->height)) |
| 131 | + { |
| 132 | + throw new BadMethodCallException("Property 'height' of argument 'file' of event {$this->name} has a wrong value. Valid: integer"); |
| 133 | + } |
| 134 | + |
| 135 | + // A ISO 8601 date string |
| 136 | + if (empty($value->create_date)) { |
| 137 | + throw new BadMethodCallException("Property 'create_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string"); |
| 138 | + } |
| 139 | + |
| 140 | + // Validate date format |
| 141 | + $date = Date::createFromFormat(\DATE_ISO8601, $value->create_date); |
| 142 | + if (!$date) { |
| 143 | + throw new BadMethodCallException("Property 'create_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string"); |
| 144 | + } |
| 145 | + |
| 146 | + // Create formated string based on \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter::getPathInformation() |
| 147 | + $value->create_date_formatted = HTMLHelper::_('date', $date, Text::_('DATE_FORMAT_LC5')); |
| 148 | + |
| 149 | + // A ISO 8601 date string |
| 150 | + if (empty($value->modified_date)) { |
| 151 | + throw new BadMethodCallException("Property 'modified_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string"); |
| 152 | + } |
| 153 | + |
| 154 | + // Validate date format |
| 155 | + $date = Date::createFromFormat(\DATE_ISO8601, $value->modified_date); |
| 156 | + if (!$date) { |
| 157 | + throw new BadMethodCallException("Property 'modified_date' of argument 'file' of event {$this->name} has a wrong value. Valid: ISO 8601 date string"); |
| 158 | + } |
| 159 | + |
| 160 | + // Create formated string based on \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter::getPathInformation() |
| 161 | + $value->modified_date_formatted = HTMLHelper::_('date', $date, Text::_('DATE_FORMAT_LC5')); |
| 162 | + |
| 163 | + return $value; |
66 | 164 | } |
67 | 165 | } |
0 commit comments