Completed
Push — master ( 1b605f...95b7c3 )
by Dan
06:00
created

AbstractIdNameMap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
getId() 0 1 ?
getName() 0 1 ?
A update() 0 8 2
A byId() 0 8 2
A byName() 0 8 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 (isset($this->thingsById[$id])) {
28
            return $this->things[$this->thingsById[$id]];
29
        } else {
30
            return null;
31
        }
32
    }
33
34
    public function byName($name)
35
    {
36
        if (isset($this->thingsByName[$name])) {
37
            return $this->things[$this->thingsByName[$name]];
38
        } else {
39
            return null;
40
        }
41
    }
42
}