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

SendGroup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 8
nc 1
nop 4
1
<?php
2
3
namespace Fenos\Notifynder\Senders;
4
5
use Fenos\Notifynder\Contracts\NotifynderCategory;
6
use Fenos\Notifynder\Contracts\NotifynderGroup;
7
use Fenos\Notifynder\Contracts\DefaultSender;
8
use Fenos\Notifynder\Contracts\StoreNotification;
9
use Fenos\Notifynder\NotifynderManager;
10
11
/**
12
 * Class SendGroup.
13
 */
14
class SendGroup implements DefaultSender
15
{
16
    /**
17
     * @var NotifynderManager
18
     */
19
    protected $notifynder;
20
21
    /**
22
     * @var string
23
     */
24
    protected $nameGroup;
25
26
    /**
27
     * @var array
28
     */
29
    protected $info;
30
31
    /**
32
     * @var NotifynderCategory
33
     */
34
    protected $notifynderCategory;
35
36
    /**
37
     * @param NotifynderGroup    $notifynderGroup
38
     * @param NotifynderCategory $notifynderCategory
39
     * @param string             $nameGroup
40
     * @param array | \Closure   $info
41
     */
42
    public function __construct(NotifynderGroup $notifynderGroup,
43
                         NotifynderCategory $notifynderCategory,
44
                         $nameGroup,
45
                         array $info)
46
    {
47
        $this->info = $info;
48
        $this->nameGroup = $nameGroup;
49
        $this->notifynderGroup = $notifynderGroup;
0 ignored issues
show
Bug introduced by
The property notifynderGroup does not seem to exist. Did you mean notifynder?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
        $this->notifynderCategory = $notifynderCategory;
51
    }
52
53
    /**
54
     * Send group notifications.
55
     *
56
     * @param  StoreNotification $sender
57
     * @return mixed
58
     */
59
    public function send(StoreNotification $sender)
60
    {
61
        // Get group
62
        $group = $this->notifynderGroup->findByName($this->nameGroup);
0 ignored issues
show
Bug introduced by
The property notifynderGroup does not seem to exist. Did you mean notifynder?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
64
        // Categories
65
        $categoriesAssociated = $group->categories;
66
67
        // Send a notification for each category
68
        foreach ($categoriesAssociated as $category) {
69
            // Category name
70
            $categoryModel = $this->notifynderCategory->findByName($category->name);
71
72
            $notification = array_merge(
73
                ['category_id' => $categoryModel->id],
74
                $this->info
75
            );
76
77
            $sender->storeSingle($notification);
78
        }
79
80
        return $group;
81
    }
82
}
83