1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Notifier\Notifications; |
4
|
|
|
|
5
|
|
|
use ByTIC\Notifications\Notification; |
|
|
|
|
6
|
|
|
use ByTIC\Notifier\Models\Recipients\RecipientTrait; |
7
|
|
|
use Nip\Container\Container; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class NotificationFactory |
11
|
|
|
* @package ByTIC\Notifier\Notifications |
12
|
|
|
*/ |
13
|
|
|
class NotificationFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param RecipientTrait $recipient |
17
|
|
|
* @param array $params |
18
|
|
|
* @return Notification |
19
|
|
|
*/ |
20
|
|
|
public static function createFromRecipient($recipient, $params = []) |
21
|
|
|
{ |
22
|
|
|
$notification = static::create( |
23
|
|
|
$recipient->getTopic()->getTarget(), |
24
|
|
|
$recipient->getTopic()->getTrigger(), |
25
|
|
|
$recipient->getRecipient(), |
26
|
|
|
$params |
27
|
|
|
); |
28
|
|
|
if (method_exists($notification, 'setRecipient')) { |
29
|
|
|
$notification->setRecipient($recipient); |
30
|
|
|
} |
31
|
|
|
return $notification; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $target |
36
|
|
|
* @param $trigger |
37
|
|
|
* @param $recipient |
38
|
|
|
* @param array $params |
39
|
|
|
* @return Notification |
40
|
|
|
*/ |
41
|
1 |
|
public static function create($target, $trigger, $recipient, $params = []) |
42
|
|
|
{ |
43
|
1 |
|
$class = static::generateNotificationName($target, $trigger, $recipient); |
44
|
1 |
|
$notification = new $class(...$params); |
45
|
1 |
|
return $notification; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $recipient |
50
|
|
|
* @param $target |
51
|
|
|
* @param $trigger |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
2 |
|
public static function generateNotificationName($target, $trigger, $recipient) |
55
|
|
|
{ |
56
|
2 |
|
$name = trim(self::getRootNamespace(), '\\'); |
57
|
2 |
|
$name .= '\Notifications\\'; |
58
|
2 |
|
$name .= inflector()->pluralize(inflector()->classify($target)) . '\\'; |
59
|
2 |
|
$name .= ucfirst($trigger) . '\\'; |
60
|
2 |
|
$name .= inflector()->pluralize(inflector()->classify($recipient)); |
61
|
2 |
|
$name .= 'Notification'; |
62
|
2 |
|
return $name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
2 |
|
public static function getRootNamespace() |
69
|
|
|
{ |
70
|
2 |
|
if (function_exists('app') && app() instanceof Container && app()->has('app')) { |
71
|
2 |
|
return app('app')->getRootNamespace(); |
72
|
|
|
} |
73
|
|
|
return 'App\\'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: