Passed
Push — master ( 3ee5c1...809568 )
by
unknown
05:05
created

Person::displayChamber()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 12
rs 9.6111

1 Method

Rating   Name   Duplication   Size   Complexity  
A Person::getCategoryFromId() 0 7 3
1
<?php
2
/**
3
 * Mirrors pydantic model for deseralisation in a PHP context.
4
 * For adding display related helper functions.
5
 * @package TheyWorkForYou
6
 */
7
8
declare(strict_types=1);
9
10
namespace MySociety\TheyWorkForYou\DataClass\Regmem;
11
12
use MySociety\TheyWorkForYou\DataClass\BaseModel;
13
14
use InvalidArgumentException;
15
16
class Person extends BaseModel {
17
    use HasChamber;
18
19
    public string $chamber;
20
    public string $language = "en";
21
    public string $person_id;
22
    public string $person_name;
23
    public string $published_date;
24
    public CategoryList $categories;
25
26
27
    public function intId(): int {
28
        // extract the last part of the person_id, which is the integer id
29
        $parts = explode('/', $this->person_id);
30
        return (int) end($parts);
31
    }
32
33
    public function allEntryIds(): array {
34
        $entryIds = [];
35
        foreach ($this->categories as $category) {
36
            foreach ($category->entries as $entry) {
37
                $entryIds[] = $entry->id;
38
                foreach ($entry->sub_entries as $subEntry) {
39
                    $entryIds[] = $subEntry->id;
40
                }
41
            }
42
        }
43
        return $entryIds;
44
    }
45
46
    public function getCategoryFromId(string $categoryId): Category {
47
        foreach ($this->categories as $category) {
48
            if ($category->category_id === $categoryId) {
49
                return $category;
50
            }
51
        }
52
        throw new InvalidArgumentException("Category $categoryId not found in register");
53
    }
54
}
55