Completed
Pull Request — master (#20)
by Kevin
02:45
created

ClientCollection::addClient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the JarvisBundle 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\JarvisBundle\Client;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use LinkValue\MobileNotif\Client\ClientInterface;
14
15
/**
16
 * ClientCollection.
17
 *
18
 * @package JarvisBundle
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 5
    public function addClient($key, ClientInterface $client)
36
    {
37 5
        if (!is_string($key)) {
38 1
            throw new \InvalidArgumentException('The client key must be a string.');
39
        }
40
41 4
        if ($this->containsKey($key)) {
42 1
            throw new \RuntimeException(sprintf('A client key "%s" already exists.', $key));
43
        }
44
45 4
        $this->set($key, $client);
46
47 4
        return $this;
48
    }
49
50
    /**
51
     * Retrieve all ApnsClient of a ClientCollection.
52
     *
53
     * @return ClientCollection
54
     */
55 1
    public function getApnsClients()
56
    {
57
        return $this->filter(function (ClientInterface $client) {
58 1
            return $client instanceof ApnsClient;
59 1
        });
60
    }
61
62
    /**
63
     * Retrieve all GcmClient of a ClientCollection.
64
     *
65
     * @return ClientCollection
66
     */
67
    public function getGcmClients()
68
    {
69 1
        return $this->filter(function (ClientInterface $client) {
70 1
            return $client instanceof GcmClient;
71 1
        });
72
    }
73
}
74