Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

SenderFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 8.65 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 4
Bugs 3 Features 1
Metric Value
wmc 9
c 4
b 3
f 1
lcom 2
cbo 3
dl 9
loc 104
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSender() 0 15 3
A sendSingle() 0 4 1
A sendMultiple() 0 4 1
A sendGroup() 0 9 1
A isMultiArray() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Fenos\Notifynder\Senders;
4
5
/*
6
 * Class NotifynderSenderFactory
7
 *
8
 * @package Fenos\Notifynder
9
 */
10
use Fenos\Notifynder\Builder\NotifynderBuilder;
11
use Fenos\Notifynder\Contracts\NotifynderCategory;
12
use Fenos\Notifynder\Contracts\NotifynderGroup;
13
14
/**
15
 * Class SenderFactory.
16
 */
17
class SenderFactory
18
{
19
    /**
20
     * @var NotifynderGroup
21
     */
22
    protected $notifynderGroup;
23
24
    /**
25
     * @var NotifynderCategory
26
     */
27
    private $notifynderCategory;
28
29
    /**
30
     * @param NotifynderGroup    $notifynderGroup
31
     * @param NotifynderCategory $notifynderCategory
32
     */
33
    public function __construct(NotifynderGroup $notifynderGroup,
34
                         NotifynderCategory $notifynderCategory)
35
    {
36
        $this->notifynderGroup = $notifynderGroup;
37
        $this->notifynderCategory = $notifynderCategory;
38
    }
39
40
    /**
41
     * Get the right sender when the data is
42
     * passed.
43
     *
44
     * @param  array                $infoNotifications
45
     * @param                       $category
46
     * @return SendMultiple|SendOne
47
     */
48
    public function getSender($infoNotifications, $category = null)
49
    {
50
        if ($infoNotifications instanceof NotifynderBuilder) {
51
            $infoNotifications = $infoNotifications->toArray();
52
        }
53
54
        // if the array is multidimensional
55
        // it means that we want to send
56
        // multiple notifications
57
        if ($this->isMultiArray($infoNotifications)) {
58
            return $this->sendMultiple($infoNotifications);
59
        } else {
60
            return $this->sendSingle($infoNotifications, $category);
61
        }
62
    }
63
64
    /**
65
     * Send Single Notification Sender.
66
     *
67
     * @param  array   $infoNotifications
68
     * @param          $category
69
     * @return SendOne
70
     */
71
    public function sendSingle(array $infoNotifications, $category)
72
    {
73
        return new SendOne($infoNotifications, $category);
0 ignored issues
show
Unused Code introduced by
The call to SendOne::__construct() has too many arguments starting with $category.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
74
    }
75
76
    /**
77
     * Send Multiple Notification Sender.
78
     *
79
     * @param  array        $infoNotifications
80
     * @return SendMultiple
81
     */
82
    public function sendMultiple(array $infoNotifications)
83
    {
84
        return new SendMultiple($infoNotifications);
85
    }
86
87
    /**
88
     * Get the the send group instance.
89
     *
90
     * @param  string           $groupName
91
     * @param  array | \Closure $info
92
     * @return SendGroup
93
     */
94
    public function sendGroup($groupName, array $info)
95
    {
96
        return new SendGroup(
97
            $this->notifynderGroup,
98
            $this->notifynderCategory,
99
            $groupName,
100
            $info
101
        );
102
    }
103
104
    /**
105
     * Check if the array passed is
106
     * multidimensional.
107
     *
108
     * @param $arr
109
     * @return bool
110
     */
111 View Code Duplication
    protected function isMultiArray(array $arr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $rv = array_filter($arr, 'is_array');
114
        if (count($rv) > 0) {
115
            return true;
116
        }
117
118
        return false;
119
    }
120
}
121