Completed
Push — master ( 613542...020c5c )
by Julián
03:39
created

Manager::getNotifications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Jgut\Tify\Service\AbstractService;
14
use Jgut\Tify\Exception\ServiceException;
15
use Jgut\Tify\Notification\AbstractNotification;
16
use Jgut\Tify\Service\FeedbackInterface;
17
18
/**
19
 * Notifications manager.
20
 */
21
class Manager
22
{
23
    /**
24
     * @var \Doctrine\Common\Collections\ArrayCollection
25
     */
26
    protected $notifications;
27
28
    /**
29
     * Manager constructor.
30
     */
31
    public function __construct()
32
    {
33
        $this->notifications = new ArrayCollection;
34
    }
35
36
    /**
37
     * Retrieve registered notifications.
38
     *
39
     * @return array
40
     */
41
    public function getNotifications()
42
    {
43
        return $this->notifications->toArray();
44
    }
45
46
    /**
47
     * Register notification.
48
     *
49
     * @param \Jgut\Tify\Notification\AbstractNotification $notification
50
     *
51
     * @return $this
52
     */
53
    public function addNotification(AbstractNotification $notification)
54
    {
55
        $this->notifications->add($notification);
56
57
        return $this;
58
    }
59
60
    /**
61
     * Clear list of notifications.
62
     */
63
    public function clearNotifications()
64
    {
65
        $this->notifications->clear();
66
67
        return $this;
68
    }
69
70
    /**
71
     * Push notifications.
72
     *
73
     * @return \Jgut\Tify\Result[]
74
     */
75
    public function push()
76
    {
77
        $results = new ArrayCollection;
78
79
        foreach ($this->notifications as $notification) {
80
            $notification->setStatus(AbstractNotification::STATUS_PENDING);
81
82
            $notification->getService()->send($notification);
83
84
            $results->add($notification->getResults());
85
        }
86
87
        return $results->toArray();
88
    }
89
90
    /**
91
     * Get feedback from service.
92
     *
93
     * @param \Jgut\Tify\Service\AbstractService $service
94
     *
95
     * @throws \Jgut\Tify\Exception\ServiceException
96
     *
97
     * @return array
98
     */
99
    public function feedback(AbstractService $service)
100
    {
101
        if (!$service instanceof FeedbackInterface) {
102
            throw new ServiceException(sprintf('%s is not a feedback enabled service', get_class($service)));
103
        }
104
105
        return $service->feedback();
106
    }
107
}
108