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

AbstractIdNameMap::byName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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
}