This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEventHelper.php
83 lines (71 loc) · 2.11 KB
/
EventHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Clip
*
* @copyright (c) Clip Team
* @link http://github.com/zikula-modules/clip/
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @package Clip
* @subpackage Event
*/
namespace Matheo\Clip;
use Matheo\Clip\Event\GenericEvent;
use Matheo_Clip_Model_Pubtype;
use Matheo\Clip\Doctrine\PubdataDoctrine;
use Matheo\Clip\Util;
use Zikula_View;
use InvalidArgumentException;
use EventUtil;
class EventHelper
{
const NAME_PATTERN = 'module.clip.%s';
/**
* Clip Event name resolver.
*
* @param string $name Name abbreviation (i.e. ui.display, data.list).
*
* @return string
*/
public static function getName($name)
{
return sprintf(self::NAME_PATTERN, $name);
}
/**
* Clip Event invoker.
*
* @param string $name Name abbreviation (i.e. ui.display, data.list).
* @param mixed $data Data to process.
* @param array $args Context arguments.
*
* @throws InvalidArgumentException When the subject cannot be resolved.
*
* @return boolean
*/
public static function notify(
$name,
$data,
$args = array()
) {
// format the name abbreviation
$name = self::getName($name);
// resolve the subject
if ($data instanceof Zikula_View) {
$pubtype = $data->getTplVar('pubtype');
if ($pubtype instanceof PubtypeModel) {
$subject = $pubtype;
}
} elseif ($data instanceof PubdataDoctrine) {
$subject = Util::getPubType($data['core_tid']);
} elseif ($args instanceof PubdataDoctrine) {
$subject = Util::getPubType($args['core_tid']);
} elseif (isset($args['tid'])) {
$subject = Util::getPubType($args['tid']);
}
if (empty($subject)) {
throw new InvalidArgumentException('Invalid event parameters. Unable to determine que subject.');
}
// TODO upgrade event handling to PublicationEvent
$event = new GenericEvent($name, $subject, $data, $args);
return EventUtil::notify($event);
}
}