Completed
Push — master ( 5b6d3c...e19d53 )
by Marcel
03:17
created

LinkStudentsHelper::getLinks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
1
<?php
2
3
namespace App\Link;
4
5
use App\Entity\User;
6
use App\Repository\UserRepositoryInterface;
7
8
class LinkStudentsHelper {
9
10
    public const StudentEduPerson = 'student';
11
12
    private $repository;
13
14 2
    public function __construct(UserRepositoryInterface $userRepository) {
15 2
        $this->repository = $userRepository;
16 2
    }
17
18
    /**
19
     * Establishes a link between the current user and a student user
20
     *
21
     * @param User $currentUser
22
     * @param User $student
23
     * @throws NotAStudentException
24
     */
25
    public function link(User $currentUser, User $student) {
26
        $studentEduPerson = $student->getType()->getEduPerson();
27
28
        if(!in_array(static::StudentEduPerson, $studentEduPerson)) {
29
            throw new NotAStudentException();
30
        }
31
32
        $ids = explode(',', $currentUser->getExternalId());
33
        $ids[] = $student->getExternalId();
34
        $currentUser->setExternalId(implode(',', $ids));
35
36
        $this->repository->persist($currentUser);
37
    }
38
39
    /**
40
     * Returns all linked student users
41
     *
42
     * @param User $user
43
     * @return User[]
44
     */
45 2
    public function getLinks(User $user): array {
46 2
        $ids = explode(',', $user->getExternalId());
47 2
        $users = [ ];
48
49 2
        foreach($ids as $id) {
50 2
            $user = $this->repository->findOneByExternalId($id);
51
52 2
            if($user !== null) {
53
                $users[] = $user;
54
            }
55
        }
56
57 2
        return $users;
58
    }
59
}