Passed
Push — master ( 5600bf...8e982a )
by Gabriel
05:21
created

NotificationFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 22
c 1
b 0
f 1
dl 0
loc 61
ccs 15
cts 25
cp 0.6
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRootNamespace() 0 6 4
A create() 0 5 1
A generateNotificationName() 0 9 1
A createFromRecipient() 0 12 2
1
<?php
2
3
namespace ByTIC\Notifier\Notifications;
4
5
use ByTIC\Notifications\Notification;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ByTIC\Notifier\Notifications\Notification. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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