Completed
Pull Request — master (#2737)
by Jeroen
17:30 queued 02:36
created

GroupManager::createGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Service;
4
5
use Doctrine\Common\Persistence\ObjectRepository;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Kunstmaan\AdminBundle\Entity\GroupInterface;
8
9
class GroupManager
10
{
11
    /**
12
     * @var EntityManagerInterface
13
     */
14
    private $em;
15
16
    /**
17
     * @var string
18
     */
19
    private $class;
20
21
    /**
22
     * @var ObjectRepository
23
     */
24
    private $repository;
25
26
    public function __construct(EntityManagerInterface $em, string $class)
27
    {
28
        $this->em = $em;
29
        $this->repository = $em->getRepository($class);
30
        $this->class = $class;
31
    }
32
33
    public function createGroup($name)
34
    {
35
        $class = $this->getClass();
36
37
        return new $class($name);
38
    }
39
40
    public function deleteGroup(GroupInterface $group)
41
    {
42
        $this->em->remove($group);
43
        $this->em->flush();
44
    }
45
46
    public function updateGroup(GroupInterface $group, $andFlush = true)
47
    {
48
        $this->em->persist($group);
49
        if ($andFlush) {
50
            $this->em->flush();
51
        }
52
    }
53
54
    public function getClass()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
55
    {
56
        $metadata = $this->em->getClassMetadata($this->class);
57
58
        return $metadata->getName();
59
    }
60
61
    public function findGroups()
62
    {
63
        return $this->repository->findAll();
64
    }
65
66
    public function findGroupByName($name)
67
    {
68
        return $this->findGroupBy(['name' => $name]);
69
    }
70
71
    public function findGroupBy(array $criteria)
72
    {
73
        return $this->repository->findOneBy($criteria);
74
    }
75
}
76