ContactMethods   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 46
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 4 1
A accept() 0 4 1
A filterByResource() 0 13 4
A filterBlacklisted() 0 9 3
A isBlacklisted() 0 7 2
1
<?php
2
3
namespace Shrikeh\PagerDuty\Collection;
4
5
use FilterIterator;
6
use Shrikeh\PagerDuty\Collection;
7
use Shrikeh\PagerDuty\Entity\ContactMethod;
8
use Shrikeh\PagerDuty\Entity\ContactMethod\Resource\Blacklistable;
9
10
final class ContactMethods extends FilterIterator implements Collection
11
{
12
    use \Shrikeh\PagerDuty\Collection\Traits\ImmutableCollection;
13
14
    private function append(ContactMethod $method)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
15
    {
16
        $this->getStorage()->attach($method);
17
    }
18
19
    public function accept()
20
    {
21
        return (!$this->isBlacklisted($this->getStorage()->current()));
22
    }
23
24
    public function filterByResource($resource, $excludeBlacklisted = true)
25
    {
26
        $methods = [];
27
        foreach ($this->getStorage() as $contactMethod) {
28
            if ($contactMethod->method() == $resource) {
29
                $methods[] = $contactMethod;
30
            }
31
        }
32
        if ($excludeBlacklisted) {
33
            $methods = $this->filterBlacklisted($methods);
34
        }
35
        return static::fromArray($methods);
36
    }
37
38
    private function filterBlacklisted($methods) {
39
        $whitelist = [];
40
        foreach ($methods as $contactMethod) {
41
            if (!$this->isBlacklisted($contactMethod)) {
42
                $whitelist[] = $contactMethod;
43
            }
44
        }
45
        return $whitelist;
46
    }
47
48
    private function isBlacklisted(ContactMethod $contactMethod)
49
    {
50
        if ($contactMethod->resource() instanceof Blacklistable) {
51
            return (true === $contactMethod->resource()->blacklisted());
52
        }
53
        return false;
54
    }
55
}
56