GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ff3008...d4aa5e )
by Nicholas
03:20
created

Database::getByCodepoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace UCD;
4
5
use UCD\Unicode\AggregatorRelay;
6
use UCD\Unicode\Character;
7
use UCD\Unicode\Character\Collection;
8
use UCD\Unicode\Character\Properties\General\Block;
9
use UCD\Unicode\Character\Repository;
10
use UCD\Unicode\Character\Repository\CharacterNotFoundException;
11
use UCD\Unicode\Codepoint;
12
use UCD\Unicode\Codepoint\Aggregator\Factory;
13
use UCD\Unicode\CodepointAssigned;
14
use UCD\Unicode\NonCharacter;
15
use UCD\Unicode\Surrogate;
16
17
use UCD\Exception\InvalidArgumentException;
18
use UCD\Exception\OutOfRangeException;
19
20
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\KeyGenerator\BlockKeyGenerator;
21
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\RangeFile\PHPRangeFileDirectory;
22
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\Serializer\PHPSerializer;
23
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository;
24
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\Property;
25
26
class Database
27
{
28
    /**
29
     * @var Repository
30
     */
31
    private $sourceRepository;
32
33
    /**
34
     * @param Repository $sourceRepository
35
     */
36
    public function __construct(Repository $sourceRepository)
37
    {
38
        $this->sourceRepository = $sourceRepository;
39
    }
40
41
    /**
42
     * @return static
43
     */
44
    public static function fromDisk()
45
    {
46
        return new static(
47
            self::createFileRepository()
48
        );
49
    }
50
51
    /**
52
     * @param Codepoint $codepoint
53
     * @return CodepointAssigned
54
     * @throws CharacterNotFoundException
55
     * @throws InvalidArgumentException
56
     * @throws OutOfRangeException
57
     */
58
    public function getByCodepoint(Codepoint $codepoint)
59
    {
60
        return $this->sourceRepository->getByCodepoint($codepoint);
61
    }
62
63
    /**
64
     * @param Codepoint\Collection $codepoints
65
     * @return Character\Collection|CodepointAssigned[]
66
     */
67
    public function getByCodepoints(Codepoint\Collection $codepoints)
68
    {
69
        return $this->sourceRepository->getByCodepoints($codepoints);
70
    }
71
72
    /**
73
     * @param Codepoint $codepoint
74
     * @return Character
75
     * @throws CharacterNotFoundException
76
     */
77
    public function getCharacterByCodepoint(Codepoint $codepoint)
78
    {
79
        $assigned = $this->getByCodepoint($codepoint);
80
81
        if ($assigned instanceof Character) {
82
            return $assigned;
83
        }
84
85
        throw CharacterNotFoundException::withCodepoint($codepoint);
86
    }
87
88
    /**
89
     * @return Collection
90
     */
91
    public function all()
92
    {
93
        return $this->sourceRepository->getAll();
94
    }
95
96
    /**
97
     * @return Collection
98
     */
99
    public function onlyCharacters()
100
    {
101
        return $this->filterWith(function (CodepointAssigned $assigned) {
102
            return $assigned instanceof Character;
103
        });
104
    }
105
106
    /**
107
     * @return Collection
108
     */
109
    public function onlyNonCharacters()
110
    {
111
        return $this->filterWith(function (CodepointAssigned $assigned) {
112
            return $assigned instanceof NonCharacter;
113
        });
114
    }
115
116
    /**
117
     * @return Collection
118
     */
119
    public function onlySurrogates()
120
    {
121
        return $this->filterWith(function (CodepointAssigned $assigned) {
122
            return $assigned instanceof Surrogate;
123
        });
124
    }
125
126
    /**
127
     * @param callable $filter
128
     * @return Collection
129
     */
130
    private function filterWith(callable $filter)
131
    {
132
        return $this->all()
133
            ->filterWith($filter);
134
    }
135
136
    /**
137
     * @param Block $block
138
     * @throws Repository\BlockNotFoundException
139
     * @return Codepoint\Range\Collection
140
     */
141
    public function getCodepointsByBlock(Block $block)
142
    {
143
        return $this->sourceRepository->getCodepointsByBlock($block);
144
    }
145
146
    /**
147
     * @return int
148
     */
149
    public function getSize()
150
    {
151
        return count($this->sourceRepository);
152
    }
153
154
    /**
155
     * @return Repository
156
     */
157
    private static function createFileRepository()
158
    {
159
        $dbPathInfo = new \SplFileInfo(sprintf('%s/../../resources/generated/ucd', __DIR__));
160
        $charactersDirectory = PHPRangeFileDirectory::fromPath($dbPathInfo);
161
        $propsPathInfo = new \SplFileInfo(sprintf('%s/../../resources/generated/props', __DIR__));
162
        $propertiesDirectory = FileRepository\PropertyFile\PHPPropertyFileDirectory::fromPath($propsPathInfo);
163
        $aggregators = new FileRepository\PropertyAggregators();
164
        $block = new AggregatorRelay(new BlockKeyGenerator(), new Factory());
165
        $aggregators->registerAggregatorRelay(Property::ofType(Property::BLOCK), $block);
166
        $serializer = new PHPSerializer();
167
168
        return new FileRepository($charactersDirectory, $propertiesDirectory, $aggregators, $serializer);
169
    }
170
}