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

FeedbackManager::getFeedback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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