Completed
Pull Request — master (#2737)
by
unknown
08:55
created

GroupManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 3
cbo 3
dl 0
loc 67
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createGroup() 0 6 1
A deleteGroup() 0 5 1
A updateGroup() 0 7 2
A getClass() 0 4 1
A findGroups() 0 4 1
A findGroupByName() 0 4 1
A findGroupBy() 0 4 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
    protected $em;
15
16
    /**
17
     * @var string
18
     */
19
    protected $class;
20
21
    /**
22
     * @var ObjectRepository
23
     */
24
    protected $repository;
25
26
    public function __construct(EntityManagerInterface $em, string $class)
27
    {
28
        $this->em = $em;
29
        $this->repository = $em->getRepository($class);
30
31
        $metadata = $em->getClassMetadata($class);
32
        $this->class = $metadata->getName();
33
    }
34
35
    public function createGroup($name)
36
    {
37
        $class = $this->getClass();
38
39
        return new $class($name);
40
    }
41
42
    public function deleteGroup(GroupInterface $group)
43
    {
44
        $this->em->remove($group);
45
        $this->em->flush();
46
    }
47
48
    public function updateGroup(GroupInterface $group, $andFlush = true)
49
    {
50
        $this->em->persist($group);
51
        if ($andFlush) {
52
            $this->em->flush();
53
        }
54
    }
55
56
    public function getClass()
57
    {
58
        return $this->class;
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