PersonRepository::ofUsername()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
crap 2.032
1
<?php declare(strict_types = 1);
2
3
namespace Simplex\Quickstart\Module\Demo\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Simplex\Exception\ResourceNotFoundException;
7
use Simplex\Quickstart\Module\Demo\Model\Person;
8
9
final class PersonRepository extends EntityRepository
10
{
11
    const EMAIL_FIELD = 'email';
12
13 1
    public function ofId(string $id): Person
14
    {
15 1
        $person = $this->find($id);
16
17 1
        if (!$person instanceof Person) {
18
            throw new ResourceNotFoundException();
19
        }
20
21 1
        return $person;
22
    }
23
24 1
    public function ofUsername(string $username): Person
25
    {
26 1
        $person = $this->findOneBy([self::EMAIL_FIELD => $username]);
27
28 1
        if (!$person instanceof Person) {
29
            throw new ResourceNotFoundException();
30
        }
31
32 1
        return $person;
33
    }
34
35
    public function save(Person $person): void
36
    {
37
        $this->getEntityManager()->persist($person);
38
        $this->getEntityManager()->flush($person);
39
    }
40
}
41