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

LinkStudentsHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 50
ccs 10
cts 20
cp 0.5
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A link() 0 12 2
A __construct() 0 2 1
A getLinks() 0 13 3
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
}