Skip to content

Commit c3a4902

Browse files
authored
Validate events (#17)
* Revert commit ec8b4c8 ccd02cb and 9a2a119 * Validate and immutable events
1 parent 9a2a119 commit c3a4902

File tree

4 files changed

+348
-84
lines changed

4 files changed

+348
-84
lines changed

administrator/components/com_media/src/Event/FetchMediaFileEvent.php

Lines changed: 126 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,155 @@
1111

1212
\defined('_JEXEC') or die;
1313

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;
1519

1620
/**
1721
* Event object for fetch media file.
1822
*
1923
* @since __DEPLOY_VERSION__
2024
*/
21-
class FetchMediaFileEvent extends AbstractEvent
25+
final class FetchMediaFileEvent extends AbstractImmutableEvent
2226
{
23-
/**
24-
* @var \stdClass
25-
* @since __DEPLOY_VERSION__
26-
*/
27-
private $file;
28-
2927
/**
3028
* Constructor.
3129
*
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
3434
*
3535
* @since __DEPLOY_VERSION__
3636
*/
37-
public function __construct($name, \stdClass $file)
37+
public function __construct($name, array $arguments = array())
3838
{
39-
parent::__construct($name, []);
39+
parent::__construct($name, $arguments);
4040

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+
}
4246
}
4347

4448
/**
45-
* Returns the event file.
49+
* Validate $value to have all attributes with a valid type
4650
*
47-
* @return \stdClass
51+
* Validation based on \Joomla\Component\Media\Administrator\Adapter\AdapterInterface::getFile()
4852
*
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
5864
*
59-
* @param \stdClass $file The file.
65+
* Generation based on \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter::getPathInformation()
6066
*
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
6278
*/
63-
public function setFile(\stdClass $file): void
79+
protected function setFile(\stdClass $value): \stdClass
6480
{
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;
66164
}
67165
}

administrator/components/com_media/src/Event/FetchMediaFileUrlEvent.php

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,94 @@
1111

1212
\defined('_JEXEC') or die;
1313

14-
use Joomla\CMS\Event\AbstractEvent;
14+
use BadMethodCallException;
15+
use Joomla\CMS\Event\AbstractImmutableEvent;
1516

1617
/**
1718
* Event object to set an url.
1819
*
1920
* @since __DEPLOY_VERSION__
2021
*/
21-
class FetchMediaFileUrlEvent extends AbstractEvent
22+
final class FetchMediaFileUrlEvent extends AbstractImmutableEvent
2223
{
23-
/**
24-
* @var string
25-
* @since __DEPLOY_VERSION__
26-
*/
27-
private $url;
28-
2924
/**
3025
* Constructor.
3126
*
32-
* @param string $name The event name.
33-
* @param string $url The url.
27+
* @param string $name The event name.
28+
* @param array $arguments The event arguments.
29+
*
30+
* @throws BadMethodCallException
3431
*
3532
* @since __DEPLOY_VERSION__
3633
*/
37-
public function __construct($name, string $url)
34+
public function __construct($name, array $arguments = array())
3835
{
39-
parent::__construct($name, []);
36+
// Check for required arguments
37+
if (!\array_key_exists('adapter', $arguments) || !is_string($arguments['adapter']))
38+
{
39+
throw new BadMethodCallException("Argument 'adapter' of event $name is not of the expected type");
40+
}
41+
42+
$this->arguments[$arguments['adapter']] = $arguments['adapter'];
43+
unset($arguments['adapter']);
4044

41-
$this->url = $url;
45+
// Check for required arguments
46+
if (!\array_key_exists('path', $arguments) || !is_string($arguments['path']))
47+
{
48+
throw new BadMethodCallException("Argument 'path' of event $name is not of the expected type");
49+
}
50+
51+
$this->arguments[$arguments['path']] = $arguments['path'];
52+
unset($arguments['path']);
53+
54+
// Check for required arguments
55+
if (!\array_key_exists('url', $arguments) || !is_string($arguments['url']))
56+
{
57+
throw new BadMethodCallException("Argument 'url' of event $name is not of the expected type");
58+
}
59+
60+
parent::__construct($name, $arguments);
4261
}
4362

4463
/**
45-
* Returns the event url.
64+
* Validate $value to be a string
4665
*
47-
* @return stdClass
66+
* @param string $value The value to set
4867
*
49-
* @since __DEPLOY_VERSION__
68+
* @return string
69+
*
70+
* @since __DEPLOY_VERSION__
5071
*/
51-
public function getUrl(): string
72+
protected function setUrl(string $value): string
5273
{
53-
return $this->url;
74+
return $value;
5475
}
5576

5677
/**
57-
* Sets the event url.
78+
* Forbid setting $path
5879
*
59-
* @param stdClass
80+
* @param string $value The value to set
6081
*
61-
* @since __DEPLOY_VERSION__
82+
* @since __DEPLOY_VERSION__
83+
*
84+
* @throws BadMethodCallException
85+
*/
86+
protected function setPath(string $value): string
87+
{
88+
throw new BadMethodCallException('Cannot set the argument "path" of the immutable event ' . $this->name . '.');
89+
}
90+
91+
/**
92+
* Forbid setting $path
93+
*
94+
* @param string $value The value to set
95+
*
96+
* @since __DEPLOY_VERSION__
97+
*
98+
* @throws BadMethodCallException
6299
*/
63-
public function setUrl(string $url): void
100+
protected function setAdapter(string $value): string
64101
{
65-
$this->url = $url;
102+
throw new BadMethodCallException('Cannot set the argument "adapter" of the immutable event ' . $this->name . '.');
66103
}
67104
}

0 commit comments

Comments
 (0)