Completed
Push — master ( fa5688...b322d1 )
by Axel
05:31 queued 02:55
created

FeedbackManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 5
c 3
b 0
f 3
lcom 1
cbo 4
dl 0
loc 60
ccs 23
cts 23
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProjectFeedbacks() 0 3 1
A getFeedback() 0 6 2
A createFeedback() 0 14 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 Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
use Developtech\AgilityBundle\Utils\Slugger;
13
14
use Symfony\Component\Security\Core\User\UserInterface;
15
16
class FeedbackManager {
17
    /** @var EntityManager **/
18
    protected $em;
19
    /** @var Slugger **/
20
    protected $slugger;
21
    /** @var string **/
22
    protected $feedbackClass;
23
24
    /**
25
     * @param EntityManager $em
26
     * @param Slugger $slugger
27
     * @param string $feedbackClass
28
     */
29 4
    public function __construct(EntityManager $em, Slugger $slugger, $feedbackClass) {
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...
30 4
        $this->em = $em;
31 4
        $this->slugger = $slugger;
32 4
        $this->feedbackClass = $feedbackClass;
33 4
    }
34
35
    /**
36
     * @param ProjectModel $project
37
     * @return array
38
     */
39 1
    public function getProjectFeedbacks(ProjectModel $project) {
40 1
        return $this->em->getRepository($this->feedbackClass)->findByProject($project);
41
    }
42
43
    /**
44
     * @param integer $id
45
     * @return FeedbackModel
46
     */
47 2
    public function getFeedback($id) {
48 2
        if(($feedback = $this->em->getRepository($this->feedbackClass)->find($id)) === null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
49 1
            throw new NotFoundHttpException('Feedback not found');
50
        }
51 1
        return $feedback;
52
    }
53
54
    /**
55
     * @param ProjectModel $project
56
     * @param string $name
57
     * @param string $description
58
     * @param UserInterface $author
59
     * @return FeedbackModel
60
     */
61 1
    public function createFeedback(ProjectModel $project, $name, $description, UserInterface $author) {
62
        $feedback =
63 1
            (new $this->feedbackClass())
64 1
            ->setName($name)
65 1
            ->setSlug($this->slugger->slugify($name))
66 1
            ->setDescription($description)
67 1
            ->setProject($project)
68 1
            ->setAuthor($author)
69 1
            ->setStatus(FeedbackModel::STATUS_OPEN)
70 1
        ;
71 1
        $this->em->persist($feedback);
72 1
        $this->em->flush();
73 1
        return $feedback;
74
    }
75
}
76