Completed
Push — master ( b322d1...651cee )
by Axel
8s
created

FeedbackManager::getProjectFeedbacksByAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 5
crap 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 5
    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 5
        $this->em = $em;
31 5
        $this->slugger = $slugger;
32 5
        $this->feedbackClass = $feedbackClass;
33
    }
34
35
    /**
36
     * @param ProjectModel $project
37
     * @return array
38
     */
39 1
    public function getProjectFeedbacks(ProjectModel $project) {
40
        return $this->em->getRepository($this->feedbackClass)->findByProject($project);
41 1
    }
42
43
    /**
44
    * @param ProjectModel $project
45
    * @param UserInterface $author
46
    * @param array $orderBy
47
    * @param integer $limit
48
    * @param integer $offset
49
    * @return array
50
    */
51 1
    public function getProjectFeedbacksByAuthor(ProjectModel $project, UserInterface $author, $orderBy = null, $limit = null, $offset = null) {
52 1
        return $this->em->getRepository($this->feedbackClass)->findBy([
53
            'project' => $project,
54
            'author' => $author
55
        ], $orderBy, $limit, $offset);
56
    }
57
58
    /**
59
     * @param integer $id
60
     * @return FeedbackModel
61
     */
62 1
    public function getFeedback($id) {
63
        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...
64
            throw new NotFoundHttpException('Feedback not found');
65
        }
66 1
        return $feedback;
67
    }
68
69
    /**
70
     * @param ProjectModel $project
71
     * @param string $name
72
     * @param string $description
73
     * @param UserInterface $author
74
     * @return FeedbackModel
75
     */
76 1
    public function createFeedback(ProjectModel $project, $name, $description, UserInterface $author) {
77
        $feedback =
78
            (new $this->feedbackClass())
79 1
            ->setName($name)
80
            ->setSlug($this->slugger->slugify($name))
81 1
            ->setDescription($description)
82 1
            ->setProject($project)
83 1
            ->setAuthor($author)
84
            ->setStatus(FeedbackModel::STATUS_OPEN)
85 1
        ;
86
        $this->em->persist($feedback);
87
        $this->em->flush();
88 1
        return $feedback;
89 1
    }
90
}
91