Completed
Pull Request — master (#17)
by Oliver
09:46
created

ClientCollection::addClient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the MobileNotifBundle package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace LinkValue\MobileNotifBundle\Client;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use LinkValue\MobileNotif\Client\ClientInterface;
14
15
/**
16
 * ClientCollection.
17
 *
18
 * @package MobileNotifBundle
19
 * @author  Jamal Youssefi <[email protected]>
20
 * @author  Valentin Coulon <[email protected]>
21
 */
22
class ClientCollection extends ArrayCollection
23
{
24
    /**
25
     * Add $client referenced by $key to the collection.
26
     *
27
     * @param string          $key    key to store the client
28
     * @param ClientInterface $client
29
     *
30
     * @return self
31
     *
32
     * @throws \InvalidArgumentException if the key is not a string
33
     * @throws \RuntimeException if the key already exists
34
     */
35
    public function addClient($key, ClientInterface $client)
36
    {
37
        if (!is_string($key)) {
38
            throw new \InvalidArgumentException('The client key must be a string.');
39
        }
40
41
        if ($this->containsKey($key)) {
42
            throw new \RuntimeException(sprintf('A client key "%s" already exists.', $key));
43
        }
44
45
        $this->set($key, $client);
46
47
        return $this;
48
    }
49
50
    /**
51
     * Retrieve all ApnsClient of a ClientCollection.
52
     *
53
     * @return ClientCollection
54
     */
55
    public function getApnsClients()
56
    {
57
        return $this->filter(function (ClientInterface $client) {
58
            return $client instanceof ApnsClient;
59
        });
60
    }
61
62
    /**
63
     * Retrieve all GcmClient of a ClientCollection.
64
     *
65
     * @return ClientCollection
66
     */
67
    public function getGcmClients()
68
    {
69
        return $this->filter(function (ClientInterface $client) {
70
            return $client instanceof GcmClient;
71
        });
72
    }
73
}
74