Passed
Push — develop ( 716d59...3a1959 )
by David
14:24
created

Institution::setName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\Repository\InstitutionRepository;
8
use Application\Traits\HasAddress;
9
use Doctrine\ORM\Mapping as ORM;
10
use Ecodev\Felix\Api\Exception;
11
12
/**
13
 * An institution.
14
 */
15
#[ORM\Index(name: 'institution_locality_idx', columns: ['locality'])]
16
#[ORM\Index(name: 'institution_area_idx', columns: ['area'])]
17
#[ORM\Index(name: 'FULLTEXT__INSTITUTION_NAME', flags: ['fulltext'], fields: ['name'])]
18
#[ORM\Index(name: 'FULLTEXT__INSTITUTION_LOCALITY', flags: ['fulltext'], fields: ['locality'])]
19
#[ORM\UniqueConstraint(name: 'unique_name', columns: ['name', 'site'])]
20
#[ORM\Entity(InstitutionRepository::class)]
21
class Institution extends Thesaurus
22
{
23
    use HasAddress;
24
25
    public function setName(string $name): void
26
    {
27
        /** @var InstitutionRepository $institutionRepository */
28
        $institutionRepository = _em()->getRepository(self::class);
29
30
        $exists = $institutionRepository->findOneBy(['name' => $name]);
31
        if ($exists && $exists->getId() !== $this->getId()) {
32
            throw new Exception('Le nom de cette institution existe déjà.');
33
        }
34
        parent::setName($name);
35
    }
36
}
37