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

Manager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 8
c 4
b 0
f 1
lcom 1
cbo 2
dl 0
loc 87
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getNotifications() 0 4 1
A addNotification() 0 6 1
A clearNotifications() 0 6 1
A push() 0 14 2
A feedback() 0 8 2
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