Passed
Branch main (b6a268)
by Iain
04:11
created

TeamCreator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createForUser() 0 12 1
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\User\Team;
16
17
use Parthenon\User\Entity\MemberInterface;
18
use Parthenon\User\Entity\TeamInterface;
19
use Parthenon\User\Repository\TeamRepositoryInterface;
20
use Parthenon\User\Repository\UserRepositoryInterface;
21
22
final class TeamCreator implements TeamCreatorInterface
23
{
24
    private TeamRepositoryInterface $teamRepository;
25
    private TeamInterface $teamClass;
26
    private UserRepositoryInterface $userRepository;
27
28
    public function __construct(TeamRepositoryInterface $teamRepository, UserRepositoryInterface $userRepository, TeamInterface $teamClass)
29
    {
30
        $this->teamRepository = $teamRepository;
31
        $this->teamClass = $teamClass;
32
        $this->userRepository = $userRepository;
33
    }
34
35
    public function createForUser(MemberInterface $user): void
36
    {
37
        $className = get_class($this->teamClass);
38
        /** @var TeamInterface $team */
39
        $team = new $className();
40
        $team->setName(sprintf("%s's team", $user->getEmail()));
41
        $team->setCreatedAt(new \DateTime('now'));
42
        $team->addMember($user);
43
44
        $this->teamRepository->save($team);
45
        $user->setTeam($team);
46
        $this->userRepository->save($user);
47
    }
48
}
49