Passed
Push — master ( ece3c1...a0e7b2 )
by Florian
02:27
created

EventRepository::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 18
rs 9.9
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Repository;
13
14
use App\Entity\Event;
15
use App\Helper\IdentifierHelper;
16
use App\Helper\RandomHelper;
17
use Doctrine\ORM\EntityRepository;
18
19
class EventRepository extends EntityRepository
20
{
21
    public function save(Event $event)
22
    {
23
        $identifierContent = $event->getOrganizer().'-'.$event->getName();
24
        $optimalIdentifier = IdentifierHelper::getHumanReadableIdentifier($identifierContent);
25
        $identifier = $optimalIdentifier;
26
27
        $number = 1;
28
        while ($this->findOneBy(['identifier' => $identifier])) {
29
            $identifier = $optimalIdentifier.$number++;
30
        }
31
32
        $random = RandomHelper::generateHumanReadableRandom(9, '-');
33
34
        $event->setIdentifiers($identifier, $random);
35
36
        $manager = $this->getEntityManager();
37
        $manager->persist($event);
38
        $manager->flush();
39
    }
40
}
41