Completed
Pull Request — master (#6)
by Dan
01:33
created

AbstractIdNameMap   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 48
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
getId() 0 1 ?
getName() 0 1 ?
A update() 0 8 2
A byId() 0 8 3
A byName() 0 8 3
A getMap() 0 10 2
1
<?php
2
3
namespace Nopolabs\Yabot\Slack;
4
5
6
abstract class AbstractIdNameMap
7
{
8
    private $things = [];
9
    private $thingsById = [];
10
    private $thingsByName = [];
11
12
    abstract protected function getId($thing) : string;
13
14
    abstract protected function getName($thing) : string;
15
16
    public function update(array $things)
17
    {
18
        $this->things = $things;
19
        foreach ($things as $index => $thing) {
20
            $this->thingsById[$this->getId($thing)] = $index;
21
            $this->thingsByName[$this->getName($thing)] = $index;
22
        }
23
    }
24
25
    public function byId($id)
26
    {
27
        if ($id && isset($this->thingsById[$id])) {
28
            return $this->things[$this->thingsById[$id]];
29
        }
30
31
        return null;
32
    }
33
34
    public function byName($name)
35
    {
36
        if ($name && isset($this->thingsByName[$name])) {
37
            return $this->things[$this->thingsByName[$name]];
38
        }
39
40
        return null;
41
    }
42
43
    public function getMap() : array
44
    {
45
        $map = [];
46
47
        foreach ($this->things as $thing) {
48
            $map[$this->getId($thing)] = $this->getName($thing);
49
        }
50
51
        return $map;
52
    }
53
}