Completed
Push — develop ( 651cee...38c493 )
by Axel
7s
created

FeedbackManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 90.48%

Importance

Changes 6
Bugs 0 Features 5
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 80
ccs 19
cts 21
cp 0.9048
rs 10
c 6
b 0
f 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getProjectFeedbacks() 0 3 1
A getProjectFeedbacksByAuthor() 0 6 1
A getFeedback() 0 6 2
A createFeedback() 0 14 1
A countFeedbacksPerStatus() 0 3 1
1
<?php
2
3
namespace Developtech\AgilityBundle\Manager;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Developtech\AgilityBundle\Model\ProjectModel;
8
use Developtech\AgilityBundle\Model\FeedbackModel;
9
10
use Developtech\AgilityBundle\Entity\Feedback;
11
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14
use Developtech\AgilityBundle\Utils\Slugger;
15
16
use Symfony\Component\Security\Core\User\UserInterface;
17
18
class FeedbackManager {
19
    /** @var EntityManager **/
20
    protected $em;
21
    /** @var Slugger **/
22
    protected $slugger;
23
24
    /**
25
     * @param EntityManager $em
26
     * @param Slugger $slugger
27
     */
28 6
    public function __construct(EntityManager $em, Slugger $slugger) {
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
29 6
        $this->em = $em;
30 6
        $this->slugger = $slugger;
31
    }
32
33
    /**
34
     * @param ProjectModel $project
35
     * @return array
36
     */
37 1
    public function getProjectFeedbacks(ProjectModel $project) {
38
        return $this->em->getRepository(Feedback::class)->findByProject($project);
39 1
    }
40
41
    /**
42
    * @param ProjectModel $project
43
    * @param UserInterface $author
44
    * @param array $orderBy
45
    * @param integer $limit
46
    * @param integer $offset
47
    * @return array
48
    */
49 1
    public function getProjectFeedbacksByAuthor(ProjectModel $project, UserInterface $author, $orderBy = null, $limit = null, $offset = null) {
50 1
        return $this->em->getRepository(Feedback::class)->findBy([
51
            'project' => $project,
52
            'author' => $author
53
        ], $orderBy, $limit, $offset);
54
    }
55
56
    /**
57
     * @param integer $id
58
     * @return FeedbackModel
59
     */
60 1
    public function getFeedback($id) {
61
        if(($feedback = $this->em->getRepository(Feedback::class)->find($id)) === null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
62
            throw new NotFoundHttpException('Feedback not found');
63
        }
64 1
        return $feedback;
65
    }
66
67
    /**
68
     * @param ProjectModel $project
69
     * @param string $name
70
     * @param string $description
71
     * @param UserInterface $author
72
     * @return FeedbackModel
73
     */
74 1
    public function createFeedback(ProjectModel $project, $name, $description, UserInterface $author) {
75
        $feedback =
76
            (new Feedback())
77 1
            ->setName($name)
78
            ->setSlug($this->slugger->slugify($name))
79 1
            ->setDescription($description)
80 1
            ->setProject($project)
81 1
            ->setAuthor($author)
82
            ->setStatus(FeedbackModel::STATUS_OPEN)
83 1
        ;
84
        $this->em->persist($feedback);
85
        $this->em->flush();
86 1
        return $feedback;
87 1
    }
88
89
    /**
90
     * @param ProjectModel $project
91
     * @param integer $status
92
     * @return integer
93
     */
94 1
    public function countFeedbacksPerStatus(ProjectModel $project, $status) {
95
        return $this->em->getRepository(Feedback::class)->countPerStatus($project, $status);
96 1
    }
97
}
98