HasInstitution   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 10
dl 0
loc 32
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstitution() 0 3 1
A setInstitution() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Traits;
6
7
use Application\Enum\Site;
8
use Application\Model\Institution;
9
use Application\Repository\InstitutionRepository;
10
use Doctrine\ORM\Mapping as ORM;
11
12
/**
13
 * Trait for all objects belonging to an institution.
14
 */
15
trait HasInstitution
16
{
17
    #[ORM\JoinColumn(onDelete: 'SET NULL')]
18
    #[ORM\ManyToOne(targetEntity: Institution::class)]
19
    private ?Institution $institution = null;
20
21
    abstract public function getSite(): Site;
22
23
    /**
24
     * Get the institution this object belongs to.
25
     */
26 7
    public function getInstitution(): ?Institution
27
    {
28 7
        return $this->institution;
29
    }
30
31
    /**
32
     * Set name of the institution this object belongs to.
33
     *
34
     * If the institution does not yet exist, it will be created automatically.
35
     */
36 3
    public function setInstitution(?string $institutionName): void
37
    {
38
        // Ignore change if it already is the same name
39 3
        $institution = $this->getInstitution();
40 3
        if ($institution && $institution->getName() === $institutionName) {
41 1
            return;
42
        }
43
44
        /** @var InstitutionRepository $institutionRepository */
45 3
        $institutionRepository = _em()->getRepository(Institution::class);
46 3
        $this->institution = $institutionRepository->getOrCreateByName($institutionName, $this->getSite());
47
    }
48
}
49